-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Fixture teardown is called during setup of the next test #9287
Comments
There are currently two places which teardown fixtures:
As you mention, (2) is definitely not how it should be done, since it has a few problems:
Fixing this will be a great improvement and will probably fix at least dozen open issues, but will require some research and work -- this is a pretty complex and subtle part of pytest. |
Sure, but the only think that helps issues get fixed is someone volunteering to fix them 🙂 |
@Zac-HD Until it is fixed - is there a workaround for this issue ? |
It's not a bug per se, it's the expected interaction between parametrization and fixture setup/teardown as currently defined Making it happen properly requires major changes to fixture definition, setup state and runtestloop Most of those are pending since the 2016 sprint and at best daunting massive undertakings |
@RonnyPfannschmidt The problem is that Pytest logs the next teardown as part of next test which means that the logs and the runtime of the teardown are treated as part of next test. |
pytest caches the fixture value according to the scope of the fixture, see my comment here. In general, pytest does not know in advance when exactly parametrized fixtures will need to be recreated, it only knows that the value will be destroyed within the scope of the fixture. If it's important to limit teardown of the parametrized fixture to function scope, then you can do so: @pytest.mark.parametrize(
"fix1, fix2",
[
pytest.param(1, 2)
],
scope="function", # <=====
indirect=True,
)
def test1(fix1, fix2):
LOGGER.info("in test") Overall, I think the issue should be closed, and the documentation should be improved. |
i'd like to eventually transfer this issue to a project wrt setupstate in general back in the 2016 sprint @nicoddemus and a few others discussed replacing the idea of switching from the current mechanism of setup/teardown in runtestprotocol to something more powerful like a execution plan in order to enable better choice in teardown as mentioned earlier the required refactoring have so far not been completed however a good amount of work in the underlying tools has been completed |
When having 2 tests which call module-scoped yield fixtures and using parametrize, teardown is done between tests, during the setup of the 2nd test (pytest_runtest_setup) and not during pytest_runtest_teardown.
Running with pytest 6.2.5
Is there a way to achieve the desired flow (i.e teardown is done when exiting the fixture, as described below in the flow without parametrize) while using parameterized?
Example test:
Output:
When running without parametrize:
Result:
The text was updated successfully, but these errors were encountered: