Skip to content

Commit 6dd3fff

Browse files
authored
Merge pull request #13022 from eitanwass/bugfix/parametrize-over-staticmethod
Fix parametrized mark over staticmethod decorator
2 parents 28e1e25 + 37cf659 commit 6dd3fff

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Eric Yuan
150150
Erik Aronesty
151151
Erik Hasse
152152
Erik M. Bray
153+
Ethan Wass
153154
Evan Kepner
154155
Evgeny Seliverstov
155156
Fabian Sturm

changelog/12863.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix applying markers, including :ref:`pytest.mark.parametrize <pytest.mark.parametrize ref>` when placed above `@staticmethod` or `@classmethod`.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ disable = [
334334

335335
[tool.codespell]
336336
ignore-words-list = "afile,asend,asser,assertio,feld,hove,ned,noes,notin,paramete,parth,socio-economic,tesults,varius,wil"
337-
skip = "*/plugin_list.rst"
337+
skip = "AUTHORS,*/plugin_list.rst"
338338
write-changes = true
339339

340340
[tool.check-wheel-contents]

src/_pytest/mark/structures.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,13 @@ def __call__(self, *args: object, **kwargs: object):
349349
if args and not kwargs:
350350
func = args[0]
351351
is_class = inspect.isclass(func)
352-
if len(args) == 1 and (istestfunc(func) or is_class):
353-
store_mark(func, self.mark, stacklevel=3)
352+
# For staticmethods/classmethods, the marks are eventually fetched from the
353+
# function object, not the descriptor, so unwrap.
354+
unwrapped_func = func
355+
if isinstance(func, (staticmethod, classmethod)):
356+
unwrapped_func = func.__func__
357+
if len(args) == 1 and (istestfunc(unwrapped_func) or is_class):
358+
store_mark(unwrapped_func, self.mark, stacklevel=3)
354359
return func
355360
return self.with_args(*args, **kwargs)
356361

testing/test_mark.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,3 +1226,38 @@ def test_attrs(self):
12261226
)
12271227
result = pytester.runpytest(foo)
12281228
result.assert_outcomes(passed=1)
1229+
1230+
1231+
def test_mark_parametrize_over_staticmethod(pytester: Pytester) -> None:
1232+
"""Check that applying marks works as intended on classmethods and staticmethods.
1233+
1234+
Regression test for #12863.
1235+
"""
1236+
pytester.makepyfile(
1237+
"""
1238+
import pytest
1239+
1240+
class TestClass:
1241+
@pytest.mark.parametrize("value", [1, 2])
1242+
@classmethod
1243+
def test_classmethod_wrapper(cls, value: int):
1244+
assert value in [1, 2]
1245+
1246+
@classmethod
1247+
@pytest.mark.parametrize("value", [1, 2])
1248+
def test_classmethod_wrapper_on_top(cls, value: int):
1249+
assert value in [1, 2]
1250+
1251+
@pytest.mark.parametrize("value", [1, 2])
1252+
@staticmethod
1253+
def test_staticmethod_wrapper(value: int):
1254+
assert value in [1, 2]
1255+
1256+
@staticmethod
1257+
@pytest.mark.parametrize("value", [1, 2])
1258+
def test_staticmethod_wrapper_on_top(value: int):
1259+
assert value in [1, 2]
1260+
"""
1261+
)
1262+
result = pytester.runpytest()
1263+
result.assert_outcomes(passed=8)

0 commit comments

Comments
 (0)