Skip to content

Commit ca91bb4

Browse files
author
Sylvain MARIE
committed
Fixed support for pytest marks on specific parameters for old versions of pytest ( < 3).
1 parent e374551 commit ca91bb4

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pytest_cases/main.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ def get_marks(self):
127127
try:
128128
return self.f.pytestmark
129129
except AttributeError:
130-
return []
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 []
131136

132137
def get(self, *args, **kwargs):
133138
# type: (...) -> Union[CaseData, Any]
@@ -498,6 +503,22 @@ def datasets_decorator(test_func):
498503
return datasets_decorator
499504

500505

506+
# Compatibility for the way we put marks on single parameters in the list passed to @pytest.mark.parametrize
507+
try:
508+
_ = pytest.param
509+
def mark_unitary_case(c):
510+
return pytest.param(c, marks=c.get_marks())
511+
except AttributeError:
512+
def mark_unitary_case(c):
513+
marks = c.get_marks()
514+
if len(marks) > 1:
515+
raise ValueError("Multiple marks on parameters not supported for old versions of pytest")
516+
else:
517+
markinfo = marks[0]
518+
markinfodecorator = getattr(pytest.mark, markinfo.name)
519+
return markinfodecorator(*markinfo.args)(c)
520+
521+
501522
def get_all_cases(cases=None, # type: Union[Callable[[Any], Any], Iterable[Callable[[Any], Any]]]
502523
module=None, # type: Union[ModuleType, Iterable[ModuleType]]
503524
this_module_object=None, # type: Any
@@ -508,6 +529,9 @@ def get_all_cases(cases=None, # type: Union[Callable[[Any], Any],
508529
"""
509530
Lists all desired cases from the user inputs. This function may be convenient for debugging purposes.
510531
532+
Note: at the end of execution this function modifies the list of cases so that the pytest marks are applied
533+
correctly for usage of the result as the parameter in a `@pytest.mark.parametrize`.
534+
511535
:param cases: a single case or a hardcoded list of cases to use. Only one of `cases` and `module` should be set.
512536
:param module: a module or a hardcoded list of modules to use. You may use `THIS_MODULE` to indicate that the
513537
module is the current one. Only one of `cases` and `module` should be set.
@@ -543,7 +567,7 @@ def get_all_cases(cases=None, # type: Union[Callable[[Any], Any],
543567
_cases = extract_cases_from_module(m, has_tag=has_tag, filter=filter)
544568

545569
# create the pytest parameters to handle pytest marks
546-
_cases = [c if len(c.get_marks()) == 0 else pytest.param(c, marks=c.get_marks()) for c in _cases]
570+
_cases = [c if len(c.get_marks()) == 0 else mark_unitary_case(c) for c in _cases]
547571

548572
return _cases
549573

0 commit comments

Comments
 (0)