Make test context managers more reliable

Sometimes, when a test using one of the set_config[_values] context managers
failed or raised an exception it would cause the context to not be able
to revert its config changes, resulting in a 'spooky action at a
distance' where random tests would start to fail for non-obvious reasons.
This commit is contained in:
Athanasios Voutsadakis
2018-03-27 17:41:05 +01:00
parent 463f1eefaf
commit 6f1e4c76d5

View File

@@ -141,8 +141,10 @@ def pytest_generate_tests(metafunc):
def set_config(app, name, value):
old_val = app.config.get(name)
app.config[name] = value
yield
app.config[name] = old_val
try:
yield
finally:
app.config[name] = old_val
@contextmanager
@@ -153,7 +155,8 @@ def set_config_values(app, dict):
old_values[key] = app.config.get(key)
app.config[key] = dict[key]
yield
for key in dict:
app.config[key] = old_values[key]
try:
yield
finally:
for key in dict:
app.config[key] = old_values[key]