diff --git a/conftest.py b/conftest.py index e4dc6640c0..0d2a4a8fc4 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,4 @@ +import contextlib import gc import os import pytest @@ -25,42 +26,42 @@ def pytest_runtest_call(item): yield -@pytest.fixture(scope="function", autouse=True) -def openpilot_function_fixture(request): +@contextlib.contextmanager +def clean_env(): starting_env = dict(os.environ) + yield + os.environ.clear() + os.environ.update(starting_env) - random.seed(0) - # setup a clean environment for each test - with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix: - prefix = os.environ["OPENPILOT_PREFIX"] +@pytest.fixture(scope="function", autouse=True) +def openpilot_function_fixture(request): + random.seed(0) - yield + with clean_env(): + # setup a clean environment for each test + with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix: + prefix = os.environ["OPENPILOT_PREFIX"] - # ensure the test doesn't change the prefix - assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"] + yield - os.environ.clear() - os.environ.update(starting_env) + # ensure the test doesn't change the prefix + assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"] - # cleanup any started processes - manager.manager_cleanup() + # cleanup any started processes + manager.manager_cleanup() - # some processes disable gc for performance, re-enable here - if not gc.isenabled(): - gc.enable() - gc.collect() + # some processes disable gc for performance, re-enable here + if not gc.isenabled(): + gc.enable() + gc.collect() # If you use setUpClass, the environment variables won't be cleared properly, # so we need to hook both the function and class pytest fixtures @pytest.fixture(scope="class", autouse=True) def openpilot_class_fixture(): - starting_env = dict(os.environ) - - yield - - os.environ.clear() - os.environ.update(starting_env) + with clean_env(): + yield @pytest.fixture(scope="function")