Skip to content

Commit

Permalink
Warn when a mark is applied to a fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Mar 10, 2021
1 parent bfbf733 commit 30f8e50
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/3664.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate applying a mark to a fixture
3 changes: 1 addition & 2 deletions doc/en/fixture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2308,8 +2308,7 @@ into an ini-file:
def my_fixture_that_sadly_wont_use_my_other_fixture():
...
Currently this will not generate any error or warning, but this is intended
to be handled by `#3664 <https://github.com/pytest-dev/pytest/issues/3664>`_.
Currently this will generate a deprecation warning.

.. _`override fixtures`:

Expand Down
2 changes: 2 additions & 0 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
"see https://docs.pytest.org/en/latest/deprecations.html#node-fspath-in-favor-of-pathlib-and-node-path",
)

MARKED_FIXTURE = PytestDeprecationWarning("Marks cannot be applied to fixtures")

# You want to make some `__init__` or function "private".
#
# def my_private_function(some, args):
Expand Down
4 changes: 4 additions & 0 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from _pytest.config.argparsing import Parser
from _pytest.deprecated import check_ispytest
from _pytest.deprecated import FILLFUNCARGS
from _pytest.deprecated import MARKED_FIXTURE
from _pytest.deprecated import NODE_FSPATH
from _pytest.deprecated import YIELD_FIXTURE
from _pytest.mark import Mark
Expand Down Expand Up @@ -1222,6 +1223,9 @@ def __call__(self, function: _FixtureFunction) -> _FixtureFunction:
"fixture is being applied more than once to the same function"
)

if hasattr(function, "pytestmark"):
warnings.warn(MARKED_FIXTURE, stacklevel=2)

function = wrap_function_to_error_out_if_called_directly(function, self)

name = self.name or function.__name__
Expand Down
4 changes: 4 additions & 0 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from ..compat import NotSetType
from _pytest.config import Config
from _pytest.deprecated import check_ispytest
from _pytest.deprecated import MARKED_FIXTURE
from _pytest.outcomes import fail
from _pytest.warning_types import PytestUnknownMarkWarning

Expand Down Expand Up @@ -399,6 +400,9 @@ def store_mark(obj, mark: Mark) -> None:
assert isinstance(mark, Mark), mark
# Always reassign name to avoid updating pytestmark in a reference that
# was only borrowed.
if hasattr(obj, "_pytestfixturefunction"):
warnings.warn(MARKED_FIXTURE, stacklevel=2)

obj.pytestmark = get_unpacked_marks(obj) + [mark]


Expand Down

0 comments on commit 30f8e50

Please sign in to comment.