Fixture with scope=class and autouse=True is not executed from mother's test class
Hi all, I'm using inheritance in my tests and base class have fixture(scope="class", autouse=True). As a result I expecting that this fixture will be executed for each child class.
import pytest
class BaseTestClass:
test_func_scope_set = None
test_class_scope_set = None
@pytest.fixture(scope='class', autouse=True)
def dummy_class_fixture(self):
self.test_class_scope_set = True
@pytest.fixture(scope='function', autouse=True)
def dummy_func_fixture(self):
self.test_func_scope_set = True
class TestDummy(BaseTestClass):
def test_dummy(self):
assert self.test_func_scope_set is True
assert self.test_class_scope_set is True
But it was not executed(value of self.test_class_scope_set is None, but expecting True):
$ pytest main.py --disable-warnings
==================================================================================== test session starts =====================================================================================
platform linux -- Python 3.8.4, pytest-6.2.1, py-1.11.0, pluggy-0.13.1
rootdir: /home/shamray/src/tmp
plugins: forked-1.4.0, dynamicrerun-1.1.1, metadata-2.0.2, json-report-1.2.1, repeat-0.9.1, xdist-2.5.0, pytest_check-0.3.9
collected 1 item
main.py F [100%]
========================================================================================== FAILURES ==========================================================================================
____________________________________________________________________________________ TestDummy.test_dummy ____________________________________________________________________________________
self = <main.TestDummy object at 0x7f45392b9100>
def test_dummy(self):
assert self.test_func_scope_set is True
> assert self.test_class_scope_set is True
E assert None is True
E + where None = <main.TestDummy object at 0x7f45392b9100>.test_class_scope_set
main.py:21: AssertionError
================================================================================== Dynamically rerun tests ===================================================================================
================================================================================== short test summary info ===================================================================================
FAILED main.py::TestDummy::test_dummy - assert None is True
================================================================================ 1 failed, 1 warning in 0.07s ================================================================================
Hello, I'm an undergraduate student trying to learn how to navigate open source libraries. I wanted to try an attempt at understanding this situation. Is there any other information (ie. version of pytest, etc)? Is this still an issue?
Thank you!
Hello @michmli, @IShamraI
- When I see an well written issue like this one, the first step to navigate it to attempt understanding the situation is to run the provided script.
- Then I'll try to understand if the issue is reaally an issue, consider also that the finding described could be just a side effect of another thing. You'll be surprised how many times a simple looking issue actually has deep roots, think of it as the tip of an iceberg.
- If the issue is real, and you feel the need to resolve it, follow the traceback or search code that looks like it could be related. As you did on #10931 . That usually ends up in the right place, even if you can't solve it... it will provide a better description of the issue so someone else could have enough data to figure a solution.
You might not always hit the right nail tho... Let me share my thought process here:
- I did step 1 and I was able to reproduce the issue
- I went to step 2 and I started wondering... is that class scoped fixtured not being executed? What's an easy way to check that? In my case I modified the fixture with an easy to find error:
@pytest.fixture(scope='class', autouse=True)
def dummy_class_fixture(self):
assert False
self.test_class_scope_set = True
And run the test, here is the output:
________________________________ ERROR at setup of TestDummy.test_dummy _________________________________
self = <test_a.TestDummy object at 0x7fb3dcb36cb0>
@pytest.fixture(scope='class', autouse=True)
def dummy_class_fixture(self):
> assert False
E assert False
That get us to the conclusion that the issue is not what was described. The class scoped fixture is being executed.
But there is something wrong anyways, isn't it?
Let's try another approach to your fixture:
@pytest.fixture(scope='class', autouse=True)
def dummy_class_fixture(self):
self.__class__.test_class_scope_set = True
And that works!!! but why? We have to go back to the python docs, there we find this explanation
Basically, that means that what is being modified is a variable of an instance of the class, and not the class level variable.
There is still one missing piece of info, that I will not chase: Why is pytest using a one instance of the class to execute the fixture and another one to run the test?
I think we need to deprecate non classmethod class scope fixtures
you are right, that was the other path I found.
@classmethod
@pytest.fixture(scope='class', autouse=True)
def dummy_class_fixture(cls):
cls.test_class_scope_set = True
also works, and I agree with @RonnyPfannschmidt that it will be the preferred way
I got stuck on this for a few days. I think the pytest documentation is pretty misleading. It claims:
Something to be aware of when grouping tests inside classes is that each test has a unique instance of the class. Having each test share the same class instance would be very detrimental to test isolation and would promote poor test practices. This is outlined below:
And then demonstrates that an int class variable is reset between test function invocations. I think it should probably have a warning around mutable data structures (like lists) are not re-created for each test function.
Edit: it appears to have a note following that block that I missed:
Note that attributes added at class level are class attributes, so they will be shared between tests.
The fact that int and list and handled differently looks like a behavior of all Python classes, not just pytest classes, and is explained well in this StackOverflow post.
I tried to solve this similar to how OP did, but couldn't get @dajose 's solution to work.
In case anyone else doesn't need class-level fields and follows the same rabbit hole I did, I eventually solve this using the setup_method from pytest to recreate all parent class instance fields for every test invocation:
class TestBase:
def setup_method(self):
self.x = 1
self.l = [1]
class TestOne(TestBase):
# I tried this setup style with fixtures but could never get it to work.
# Using `setUp` from unittest.TestCase conflicted with my async tests in the same file that relied on the parent class
def setup_method(self):
super().setup_method()
self.z = [self.x]
def test_something(self):
assert self.z == [1]
def test_something2(self):
self.l.append(2)
assert(self.l == [1, 2])
def test_something3(self):
assert(self.l == [1])