diff --git a/pocs/base.py b/pocs/base.py index 7395083ed..40de909dc 100644 --- a/pocs/base.py +++ b/pocs/base.py @@ -10,6 +10,16 @@ _config = None +def reset_global_config(): + """Reset the global _config to None. + + Globals such as _config make tests non-hermetic. Enable conftest.py to clear _config + in an explicit fashion. + """ + global _config + _config = None + + class PanBase(object): """ Base class for other classes within the PANOPTES ecosystem diff --git a/pocs/tests/conftest.py b/pocs/tests/conftest.py index 751ad1ade..3a5722c2f 100644 --- a/pocs/tests/conftest.py +++ b/pocs/tests/conftest.py @@ -1,10 +1,15 @@ +import copy import os import pytest +import pocs.base from pocs import hardware from pocs.utils.config import load_config from pocs.utils.database import PanMongo +# Global variable with the default config; we read it once, copy it each time it is needed. +_one_time_config = None + def pytest_addoption(parser): parser.addoption("--with-hardware", nargs='+', default=[], @@ -44,11 +49,16 @@ def pytest_collection_modifyitems(config, items): item.add_marker(skip) -@pytest.fixture +@pytest.fixture(scope='function') def config(): - config = load_config(ignore_local=True, simulator=['all']) - config['db']['name'] = 'panoptes_testing' - return config + pocs.base.reset_global_config() + + global _one_time_config + if not _one_time_config: + _one_time_config = load_config(ignore_local=True, simulator=['all']) + _one_time_config['db']['name'] = 'panoptes_testing' + + return copy.deepcopy(_one_time_config) @pytest.fixture