pytest: Generate an error when a mark is applied to a fixture

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.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 23
  • Comments: 28 (18 by maintainers)

Commits related to this issue

Most upvoted comments

I don’t understand why applying the usefixtures marker to a fixture should result in an error. Fixtures can already use other fixtures by declaring them in the signature:

@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 usefixtures?

@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 exec could be easily avoided if it was possible to mark a fixture with the usefixtures marker.

EDIT: I made a typo that changed the polarity of the sentence “What is the reason of not supporting the equivalent way with usefixtures?”

Just to mention that I stubbed my toe on this very problem today. My tests miraculously were failing when applying a fixture using mark but when used directly it was fine. It took a while to debug the cause and find this thread. Think it would be great to have at least the warning merged a.s.a.p. and even better would be to actually support this behavior. I would have helped out with a PR but haven’t developed pytest yet, maybe in the future though.

@nicoddemus I am starting working on this.