pytest icon indicating copy to clipboard operation
pytest copied to clipboard

`request.addfinalizer` order of execution is undocumented

Open fish-face opened this issue 3 years ago • 10 comments

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.

fish-face avatar Jun 06 '22 21:06 fish-face

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.

nicoddemus avatar Jun 06 '22 22:06 nicoddemus

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 :/

fish-face avatar Jun 06 '22 23:06 fish-face

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.

nicoddemus avatar Jun 07 '22 11:06 nicoddemus

I can break this down in the docs if you'd like. Looks like a good first issue for me.

dougmellon avatar Jun 07 '22 18:06 dougmellon

Yeah, sounds great, thanks. I think just a paragraph stating that they are executed in first-in-last-out order is enough. 👍

nicoddemus avatar Jun 07 '22 21:06 nicoddemus

@nicoddemus on it, thank you.

dougmellon avatar Jun 08 '22 00:06 dougmellon

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.

fish-face avatar Jun 08 '22 17:06 fish-face

is it done ?

ImShivraj avatar Jul 01 '22 05:07 ImShivraj

Not yet.

nicoddemus avatar Jul 01 '22 15:07 nicoddemus

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:

aizpurua23a avatar Jul 26 '22 19:07 aizpurua23a