-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mark.usefixtures registered fixtures of all test class subclasses invoked despite -k #2806
Comments
Hi @rmfitzpatrick, You expected this output:
Correct? (Btw, in the future try to add your expectation explicitly, it helps us maintainers sometimes 😉) This is a long term problem with our marks system, marks are attached to the python object that decorates them and accumulated there, instead of attached to the collection item the python object represents. For example, when you decorate a Fixtures, on the other hand, are attached to the collection item where they were declared. For example, a fixture declared in a module will be attached to the Module item created by pytest to represent that module, not to the python module object itself. @RonnyPfannschmidt is bravely undergoing a crusade in order to fix this wart in our code-base. For know there's no known solution to this problem, other than stop using class TestOne(Base):
@pytest.fixture(autouse=True)
def __foo(self, one):
pass
class TestTwo(Base):
@pytest.fixture(autouse=True)
def __foo(self, two):
pass
class TestThree(Base):
@pytest.fixture(autouse=True)
def __foo(self, three):
pass This forces your classes to instantiate the appropriate fixtures using the fixtures mechanism itself, and not marks. With this I can obtain the desired output:
|
Thanks @nicoddemus for the suggested workaround, You are correct that I'd only expect
|
You can use a decorator to inject the def use_fixtures_workaround(*names):
def inner(cls):
@pytest.fixture(autouse=True)
def __usefixtures_workaround(self, request):
for name in names:
request.getfixturevalue(name)
cls.__usefixtures_workaround = __usefixtures_workaround
return cls
return inner
@use_fixtures_workaround('one')
class TestOne(Base):
def test_one(self):
assert 1
@use_fixtures_workaround('two')
class TestTwo(Base):
def test_two(self):
assert 1
@use_fixtures_workaround('three')
class TestThree(Base):
def test_three(self):
assert 1 |
Wow @nicoddemus ... that's some quite scary advanced pytest foo 🥇 |
Hehehe! At its heart, it is just a class decorator which injects a function, which happens to be decorated with |
Einstein must have said something similar about his theory of general relativity 😁 |
Closed by #3317. |
If multiple test classes all subclass a class with shared tests and use the
pytest.mark.usefixtures()
, runs using-k SingleClass
will still invoke excluded test class sourced fixtures.reproduce.py
The text was updated successfully, but these errors were encountered: