Open megachweng opened 5 years ago
I can confirm this behaviour, and I do agree it seems a little bit strange.
The "class" level fixture is being reused both for the test in the top level class as well as the first test in the nested class (but not the second one?)
This is also the first time I've seen nested test classes (!) I didn't even know that was a thing!
I believe the problem lies in the code for request.node
(FixtureRequest.node
property). What it does for class
scope is just look up the node tree until it finds a Class
node, and returns it. However, because classes can be nested, this is not correct; it needs to find its own class.
Another collection node which can be nested is Package
; that however does try to handle the nested aspect based on fixturedef.baseid
. It currently does this incorrectly (ref #10993), but that's another matter. After fixing #10993 for packages, we should switch classes to use the same kind of code as package.
Test case:
import pytest
class TestTop:
@pytest.fixture(scope='class')
def fix_top(self, request):
# Currently gives TestNested
assert isinstance(request.node.obj, TestTop)
class TestNested:
def test_it(self, fix_top):
assert False
While the previous comment is correct, it is actually not the cause of the issue. The real cause is described in the technical note in #11205.
Details
fixture
name
invoked and teardown every subclass method as I expect.but fixture
name
invoked inTestClassTop.test_method_sub
but teardown inTestClassTop.TestSubClassA.test_sub_cls_method
And If I move
TestClassTop.test_method_sub
to the bottom, the result seems right.Environment
Python 3.6.8 Mac OS High Sierra 10.13.6