-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track exceptions when setting up and finalizing fixtures #12163
Comments
Probably a more correct solution would be to transfer the completion of the fixtures to an independent hook pytest_fixture_teardown. |
I agree that the problem described in the issue is worth solving. To me the original idea of passing the exception(s) to
Therefore my preference would be to add the exception/ExceptionGroup to |
Therefore, I moved the execution of the finalizers function code to the Now I use this hook to track the status of the test object before executing the teardown function and to verify that nothing happened to this object during code execution. Because in case of some errors, further execution of other fixtures and tests is impossible. @pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(self, fixturedef: FixtureDef, request: pytest.FixtureRequest) -> Generator[None]:
# if fixture is not param
if hasattr(request, 'module') and fixturedef.func.__name__ != 'get_direct_param_fixture_func':
# check full stack state, before run setup function
self._check_stack(request.module)
# add markers to stdout output
self._mark_state_in_dev_stdout(fixturedef.argname, 'start fixture setup', request.module)
result = yield
# if we have ProtocolException after fixture setup, then check and restore stand state
if result.exception and isinstance(result.exception, ProtocolException):
self._check_stand_state(request.module)
@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_teardown(self, fixturedef: FixtureDef, request: pytest.FixtureRequest) -> Generator[None]:
# if fixture is not param and have finilizers
if fixturedef._finalizers and fixturedef.func.__name__ != 'get_direct_param_fixture_func' and hasattr(request, 'module'):
# check full stack state, before run teardown functions
self._check_stack(request.module)
# add markers to stdout output
self._mark_state_in_dev_stdout(fixturedef.argname, 'start fixture teardown', request.module)
result = yield
# if we have exeption after fixture teardown, then check and restore stand state
if result.exception:
try:
raise result.exception
except *ProtocolException:
self._check_stand_state(request.module)
except *BaseException:
pass |
I agree with this. Our use case is quite simple - we'd like to act upon start of the tear-down part of the fixture (set appropriate logging context for the external logging solution). |
@bluetech have you had any chance to revisit this, please? |
There is a task to track exceptions when setting up and finalizing fixtures.
When setting up, an exception can be obtained in the pytest_fixture_setup hook:
But at finalizing, you can't just get a list of exceptions of all finalizers that have been executed. Because there is no analog of the result as in the pytest_fixture_setup hook.
I propose to finalize the pytest_fixture_post_finalizer specification and an additional argument that will contain the exclusion(s) of all fixture finalizers.
src/_pytest/hookspec.py:
src/_pytest/fixtures.py::FixtureDef::finish
The text was updated successfully, but these errors were encountered: