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 8d2bd5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
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 8d2bd5f

Please sign in to comment.