Skip to content

Commit 10a3adb

Browse files
author
Sylvain MARIE
committed
Fixed support for pytest marks under python 3 + pytest 2. Added a test to easily detect these in the future
1 parent ca91bb4 commit 10a3adb

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

pytest_cases/main.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,7 @@ def get_marks(self):
124124
Overrides default implementation to return the marks that are on the case function
125125
:return:
126126
"""
127-
try:
128-
return self.f.pytestmark
129-
except AttributeError:
130-
try:
131-
# old pytest < 3: marks are set as fields on the function object
132-
# but they do not have a particulat type, their type is 'instance'...
133-
return [v for v in self.f.func_dict.values() if str(v).startswith("<MarkInfo '")]
134-
except AttributeError:
135-
return []
127+
return get_pytest_marks_on_case_func(self.f)
136128

137129
def get(self, *args, **kwargs):
138130
# type: (...) -> Union[CaseData, Any]
@@ -144,6 +136,24 @@ def get(self, *args, **kwargs):
144136
return self.f(*args, **kwargs)
145137

146138

139+
def get_pytest_marks_on_case_func(f):
140+
"""
141+
Utility to return all pytest marks applied on a case function
142+
143+
:param f:
144+
:return:
145+
"""
146+
try:
147+
return f.pytestmark
148+
except AttributeError:
149+
try:
150+
# old pytest < 3: marks are set as fields on the function object
151+
# but they do not have a particulat type, their type is 'instance'...
152+
return [v for v in vars(f).values() if str(v).startswith("<MarkInfo '")]
153+
except AttributeError:
154+
return []
155+
156+
147157
CASE_PREFIX = 'case_'
148158
"""Prefix used by default to identify case functions within a module"""
149159

@@ -506,11 +516,10 @@ def datasets_decorator(test_func):
506516
# Compatibility for the way we put marks on single parameters in the list passed to @pytest.mark.parametrize
507517
try:
508518
_ = pytest.param
509-
def mark_unitary_case(c):
510-
return pytest.param(c, marks=c.get_marks())
519+
def get_marked_parameter_for_case(c, marks):
520+
return pytest.param(c, marks=marks)
511521
except AttributeError:
512-
def mark_unitary_case(c):
513-
marks = c.get_marks()
522+
def get_marked_parameter_for_case(c, marks):
514523
if len(marks) > 1:
515524
raise ValueError("Multiple marks on parameters not supported for old versions of pytest")
516525
else:
@@ -567,7 +576,7 @@ def get_all_cases(cases=None, # type: Union[Callable[[Any], Any],
567576
_cases = extract_cases_from_module(m, has_tag=has_tag, filter=filter)
568577

569578
# create the pytest parameters to handle pytest marks
570-
_cases = [c if len(c.get_marks()) == 0 else mark_unitary_case(c) for c in _cases]
579+
_cases = [c if len(c.get_marks()) == 0 else get_marked_parameter_for_case(c, marks=c.get_marks()) for c in _cases]
571580

572581
return _cases
573582

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
from pytest_cases.main import get_pytest_marks_on_case_func, get_marked_parameter_for_case
4+
5+
6+
@pytest.mark.skipif(True, reason="why")
7+
def case_func():
8+
pass
9+
10+
11+
def test_get_pytest_marks():
12+
"""
13+
Tests that we are able to correctly retrieve the marks on case_func
14+
:return:
15+
"""
16+
# extract the marks from a case function
17+
marks = get_pytest_marks_on_case_func(case_func)
18+
assert len(marks) == 1
19+
20+
# transform a parameter into a marked parameter
21+
dummy_case = (1, 2, 3)
22+
marked_param = get_marked_parameter_for_case(dummy_case, marks=marks)

0 commit comments

Comments
 (0)