-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Generate an error when a mark is applied to a fixture #3664
Comments
@nicoddemus I am starting working on this. |
Great, thanks @avirlrma! |
@nicoddemus I'm having trouble with code navigation, need some help with it. My initial guess was
|
Hi @avirlrma, Actually I believe you need to look at where marks are applied to functions; at that point we need to identify if the function where the mark will be applied is already a fixture (possibly by checking one of the attributes which are attached to the function by the (Sorry for the brevity as I'm short on time) |
the pytest fixture parser should raise an error if either the fuction or the wrapped function has markers applied |
@RonnyPfannschmidt @nicoddemus Where should we catch the error? I mean when fixture is parsed or when the marks are applied to function? Also,
As far as I understand decorators, mark will applied first, but since then the function is not a fixture, this should work, but it doesn't. Please help me with this as well. |
You mean raise the error? We don't need to catch the error, only raise it to warn the user.
Probably at both places, because as you correctly point out, marks and fixtures can be applied in different order. Btw, perhaps we should issue a warning instead of an error right away? |
Ah yes, I meant raise the error.
How do we do that? |
we should check both, fixture, and at fixture parsing time, as we currently still need to catch stuff like @pytest.mark.usefixtures('client')
@pytest.fixture
def user_tom():
print('user jessie') |
ok, so we have to check both. For now I'm starting with the mark first and then fixture case i.e.:
This can be raised when the fixture is parsed, so the necessary changes will be done in |
Sounds good, we can discuss over a PR.
I mean to issue a warning instead of an error, something like: warnings.warn(pytest.PytestWarning('marks cannot...'), stacklevel=2) |
got it! I'm working on the PR |
I don't understand why applying the @pytest.fixture
def setup_for_bar():
# setup something....
pass
@pytest.fixture
def bar(setup_for_bar):
return 43 What is the reason of not supporting the equivalent way with @pytest.fixture
def setup_for_bar():
# setup something....
pass
@pytest.mark.usefixtures('setup_for_bar')
@pytest.fixture
def bar():
return 43 The reason I am asking this is that in pytest-factoryboy we have to call exec in order to generate a fixture that requires all the relevant fixtures. The use of EDIT: I made a typo that changed the polarity of the sentence "What is the reason of not supporting the equivalent way with |
Hi @youtux,
Mostly because it doesn't do anything currently.
This issue is about raising an error if a mark is aplied to a fixture, not to support |
Sorry, I meant to say "What is the reason of not supporting the equivalent way with usefixtures?". So I am all about supporting it, not the other way around 😅. |
Oh OK! 😁 I think it makes sense actually; this task is more of a stop gap in order for people to stop using it and it doing nothing I think. It is easier to remove this when we eventually do support marks in fixtures. |
So if I understand correctly this is just about making it resulting into an error, but the maintainers are not against this feature per-se, correct? |
I don't think so; previously it was a problem technically because of how marks worked internally, but since we have refactor and improved that aspect I think it should be fine to do this now. |
Ok, I'll give it a look then. |
Thanks! |
Fixtures cannot be currently included into other fixtures by using @pytest.mark.fixtures('fixture_name') They have to be included as parameters instead. See bug: pytest-dev/pytest#3664 Also increase the scope of needs_root to the highest, i.e. session, so that it can be used by any kind of fixture. Signed-off-by: Joseph Nuthalapati <[email protected]>
If anyone is still interested, I actually figured out a workaround for this. You can find it here, in this function that dynamically generates fixture functions: https://github.com/pytest-dev/pytest-factoryboy/blob/62474a25a80de3400d862d68d929c0a42a650f7c/pytest_factoryboy/fixturegen.py Basically I replace the function signature with one that requires the fixtures I need, and just discard them. |
Just to mention that I stubbed my toe on this very problem today. My tests miraculously were failing when applying a fixture using |
what's the progress on this? |
Any progress on this? |
Here is a way you can warn about marks being applied to fixtures (and of course you could get more specific what exactly you would like to warn about & how). I created this proof of concept using
def pytest_fixture_setup(fixturedef, request):
marks = getattr(fixturedef.func, 'pytestmark', [])
assert (
not marks
), f"{fixturedef} has marks, which is probably an error: {marks}"
import pytest
@pytest.fixture
@pytest.mark.parametrize("x", (1, 2)) # this will trigger the assertion error
def has_mark():
return "has mark"
@pytest.fixture
def no_mark():
return "no mark"
def test_demo(has_mark, no_mark):
pass |
When did this ship? |
@Redoubts not released yet. |
Follow up from #1014.
We should generate an error if a
@pytest.mark
is applied to a fixture.There is a warning in
doc/en/fixture.rst
about this problem which should be updated once this issue is dealt with.The text was updated successfully, but these errors were encountered: