diff --git a/tests/modmail/conftest.py b/tests/modmail/conftest.py index 2e3b5139..ec3baa96 100644 --- a/tests/modmail/conftest.py +++ b/tests/modmail/conftest.py @@ -6,6 +6,9 @@ import pytest +_ORIG_ENVIRON = None + + def pytest_report_header(config) -> str: """Pytest headers.""" return "package: modmail" @@ -24,16 +27,16 @@ def _get_env(): def pytest_configure(): - """Check that the test specific env file exists, and cancel the run if it does not exist.""" + """Load the test specific environment file, exit if it does not exist.""" env = _get_env() if not env.is_file(): pytest.exit(f"Testing specific {env} does not exist. Cancelling test run.", 2) + os.environ.clear() + dotenv.load_dotenv(_get_env(), override=True) -@pytest.fixture(autouse=True, scope="package") -def standardize_environment(): - """Clear environment variables except for the test.env file.""" - env = _get_env() - with unittest.mock.patch.dict(os.environ, clear=True): - dotenv.load_dotenv(env) - yield +def pytest_unconfigure(): + """Reset os.environ to the original environment before the run.""" + if _ORIG_ENVIRON is not None: + os.environ.clear() + os.environ.update(**_ORIG_ENVIRON)