pytest
pytest copied to clipboard
`request.addfinalizer` order of execution is undocumented
This is very important if you are creating fixtures which interact with others which were not written with yield, and is extra confusing when writing tests which themselves do use yield.
Hi @fish-face,
Thanks for the report. Would you like to contribute a PR with that? I think a single paragraph to the docs of request.addfinalizer would do.
The problem is I still don't really know what the behaviour is, even after examining the code! I could write it but I would be guessing :/
Oh I see, sorry didn't realize that.
This is easy to verify by yourself, no need to look at the code:
import pytest
from functools import partial
@pytest.fixture
def fix(request):
request.addfinalizer(partial(print, "finalizer1"))
request.addfinalizer(partial(print, "finalizer2"))
def test_foo(fix):
pass
λ pytest .tmp\test_foo.py -s --no-header -q
.finalizer2
finalizer1
So they are executed in First-In-Last-Out order.
I can break this down in the docs if you'd like. Looks like a good first issue for me.
Yeah, sounds great, thanks. I think just a paragraph stating that they are executed in first-in-last-out order is enough. 👍
@nicoddemus on it, thank you.
Yeah, sounds great, thanks. I think just a paragraph stating that they are executed in first-in-last-out order is enough. +1
The complexity is how this interacts with the finalizers which are added implicitly through yield, and the different possible scopes.
is it done ?
Not yet.
Hey eveyrone! I haven't seen progress on this issue for some time, so I took the liberty to try and address it on https://github.com/pytest-dev/pytest/pull/10171.
I'd love to hear your thoughts on it :pray: