I am experimenting with pytest and got stuck with some unobvious behavior for me. I have session-scope fixture and use it like this:
@pytest.mark.usefixtures("myfixt")
class TestWithMyFixt(object):
    @classmethod
    def setup_class(cls):
        ...
And when I run test I see that setup_class() comes before fixture myfixt() call. What is the purpose behind such a behaviour? For me, it should be run after fixture initialization because it uses fixture. How can I use setup_class() after session fixture initialization?
Thanks in advance!
I found this question while looking for similar issue, and here is what I came up with.
scope. Possible scopes are session, module, class and function.autouse=True flag.If we combine these, we will get this nice approach which doesn't use setup_class at all (in py.test, setup_class is considered an obsolete approach):
class TestWithMyFixt(object):
    @pytest.fixture(autouse=True, scope='class')
    def _prepare(self, myfixt):
        ...
With such implementation, _prepare will be started just once before first of tests within the class, and will be finalized after last test within the class.
At the time _prepare fixture is started, myfixt is already applied as a dependency of _prepare.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With