-
Notifications
You must be signed in to change notification settings - Fork 70
Open
Labels
Description
While working on #942, I found that the following test fixture can be helpful in allowing us to re-use a module name. (Before, each test would need to use a different file name for test bots, which would make parametrised tests harder.)
@pytest.fixture
def cleanup_test_modules(request):
# Use a marker like
# @pytest.mark.cleanup_test_modules(["test-team-a", "test-team-b"])
# to override the modules that are cleaned up. Otherwise it defaults to ["test-team"].
marker = request.node.get_closest_marker("cleanup_test_modules")
if marker is None:
modules = ['test-team']
else:
modules = marker.args[0]
for module in modules:
if module in sys.modules:
raise RuntimeError(f"Test module {module} is already in sys.modules.")
yield
for module in modules:
del sys.modules[module]
A test can then just add cleanup_test_modules as a parameter and be sure that test-team is not in sys.modules and that it is automatically removed afterwards.
def test_player_import_name(cleanup_test_modules):
...
load_team('test-team.py')
...
I’m planning on rewriting the tests to use this, which potentially makes a bunch of tests shorter. This issue is in case there are objections agains this hack.