Skip to content

Commit 924fbf5

Browse files
author
Sergey Chipiga
committed
parametrize with marked callable objects
1 parent b5dc7d9 commit 924fbf5

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

_pytest/python.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import types
88
import sys
99
import pytest
10-
from _pytest.mark import MarkDecorator, MarkerError
10+
from _pytest.mark import MarkDecorator, MarkerError, MarkInfo
1111
from py._code.code import TerminalRepr
1212

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

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

testing/python/collect.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ def test_archival_to_version(key, value):
384384
rec = testdir.inline_run()
385385
rec.assertoutcome(passed=2)
386386

387-
388387
def test_parametrize_with_non_hashable_values_indirect(self, testdir):
389388
"""Test parametrization with non-hashable values with indirect parametrization."""
390389
testdir.makepyfile("""
@@ -412,7 +411,6 @@ def test_archival_to_version(key, value):
412411
rec = testdir.inline_run()
413412
rec.assertoutcome(passed=2)
414413

415-
416414
def test_parametrize_overrides_fixture(self, testdir):
417415
"""Test parametrization when parameter overrides existing fixture with same name."""
418416
testdir.makepyfile("""
@@ -440,7 +438,6 @@ def test_overridden_via_multiparam(other, value):
440438
rec = testdir.inline_run()
441439
rec.assertoutcome(passed=3)
442440

443-
444441
def test_parametrize_overrides_parametrized_fixture(self, testdir):
445442
"""Test parametrization when parameter overrides existing parametrized fixture with same name."""
446443
testdir.makepyfile("""
@@ -527,6 +524,32 @@ def test2(self, x, y):
527524
assert colitems[2].name == 'test2[a-c]'
528525
assert colitems[3].name == 'test2[b-c]'
529526

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

531554
class TestSorting:
532555
def test_check_equality(self, testdir):

0 commit comments

Comments
 (0)