def pytest_runtest_teardown(item, nextitem):
"""Pytest hook to dispatch destructive scenarios."""
do_revert = True
# Prevent reverting after skipped tests
if getattr(item, SKIPPED, False):
do_revert = False
# Revert only destructive tests
if not item.get_marker(DESTRUCTIVE):
do_revert = False
snapshot_name = item.session.config.option.snapshot_name
# Prevent reverting if no snapshot_name passed
if snapshot_name is None:
do_revert = False
if do_revert:
destructor = item._request.getfixturevalue('os_faults_client')
# reject finalizers of all fixture scopes
for finalizers in item.session._setupstate._finalizers.values():
for finalizer in finalizers:
# There are finalizers in the form of lambda function without
# name. That looks as internal pytest specifics. We should skip
# them.
try:
fixture_def = six.get_method_self(finalizer)
except AttributeError:
continue
if not hasattr(fixture_def.func, INDESTRUCTIBLE):
LOG.debug('Clear {} finalizers'.format(fixture_def))
fixture_def._finalizer[:] = []
# Clear fixture cached result to force fixture with any
# scope to restart in next test.
if hasattr(fixture_def, "cached_result"):
LOG.debug('Clear {} cache'.format(fixture_def))
del fixture_def.cached_result
outcome = yield
# Prevent reverting after last test
if nextitem is None or item.session.shouldstop:
do_revert = False
# Prevent reverting after KeyboardInterrupt
if outcome.excinfo is not None and outcome.excinfo[0] is KeyboardInterrupt:
do_revert = False
if do_revert and destructor:
revert_environment(destructor, snapshot_name)
time.sleep(item.session.config.option.revert_timeout * 60)
评论列表
文章目录