Skip to content

Commit 218b70a

Browse files
author
Sergey Chipiga
committed
parametrize with marked callable objects
1 parent 2e04771 commit 218b70a

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

_pytest/python.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import py
1010
import pytest
1111
from _pytest._code.code import TerminalRepr
12-
from _pytest.mark import MarkDecorator, MarkerError
12+
from _pytest.mark import MarkDecorator, MarkerError, MarkInfo
1313

1414
try:
1515
import enum
@@ -982,6 +982,28 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
982982
newmarks[newmark.markname] = newmark
983983
argval = argval.args[-1]
984984
unwrapped_argvalues.append(argval)
985+
986+
if inspect.isclass(argval):
987+
pytestmark = getattr(argval, 'pytestmark', None)
988+
989+
if pytestmark:
990+
if not isinstance(pytestmark, list):
991+
pytestmark = [pytestmark]
992+
993+
for mark in pytestmark:
994+
newkeywords.setdefault(i, {}).setdefault(mark.markname,
995+
mark)
996+
997+
if inspect.isfunction(argval):
998+
for attr_name in dir(argval):
999+
if attr_name.startswith('_'):
1000+
continue
1001+
1002+
attr = getattr(argval, attr_name)
1003+
if isinstance(attr, MarkInfo):
1004+
newkeywords.setdefault(i, {}).setdefault(attr.name,
1005+
attr)
1006+
9851007
argvalues = unwrapped_argvalues
9861008

9871009
if not isinstance(argnames, (tuple, list)):

testing/python/collect.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ def test_archival_to_version(key, value):
387387
rec = testdir.inline_run()
388388
rec.assertoutcome(passed=2)
389389

390-
391390
def test_parametrize_with_non_hashable_values_indirect(self, testdir):
392391
"""Test parametrization with non-hashable values with indirect parametrization."""
393392
testdir.makepyfile("""
@@ -415,7 +414,6 @@ def test_archival_to_version(key, value):
415414
rec = testdir.inline_run()
416415
rec.assertoutcome(passed=2)
417416

418-
419417
def test_parametrize_overrides_fixture(self, testdir):
420418
"""Test parametrization when parameter overrides existing fixture with same name."""
421419
testdir.makepyfile("""
@@ -443,7 +441,6 @@ def test_overridden_via_multiparam(other, value):
443441
rec = testdir.inline_run()
444442
rec.assertoutcome(passed=3)
445443

446-
447444
def test_parametrize_overrides_parametrized_fixture(self, testdir):
448445
"""Test parametrization when parameter overrides existing parametrized fixture with same name."""
449446
testdir.makepyfile("""
@@ -530,6 +527,32 @@ def test2(self, x, y):
530527
assert colitems[2].name == 'test2[a-c]'
531528
assert colitems[3].name == 'test2[b-c]'
532529

530+
def test_parametrize_with_marked_class(self, testdir):
531+
testdir.makepyfile("""
532+
import pytest
533+
534+
class A(object): pass
535+
536+
@pytest.mark.parametrize('a', [pytest.mark.xfail(A)])
537+
def test_function(a):
538+
assert False
539+
""")
540+
reprec = testdir.inline_run()
541+
reprec.assertoutcome(skipped=1)
542+
543+
def test_parametrize_with_marked_function(self, testdir):
544+
testdir.makepyfile("""
545+
import pytest
546+
547+
def func(): pass
548+
549+
@pytest.mark.parametrize('a', [pytest.mark.xfail(func)])
550+
def test_function(a):
551+
assert False
552+
""")
553+
reprec = testdir.inline_run()
554+
reprec.assertoutcome(skipped=1)
555+
533556

534557
class TestSorting:
535558
def test_check_equality(self, testdir):

0 commit comments

Comments
 (0)