Skip to content

Commit bfe2c2f

Browse files
author
Sylvain MARIE
committed
Case functions that are staticmethod and classmethod are now supported as well. Fixes #168
1 parent ceadb9a commit bfe2c2f

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

pytest_cases/case_parametrizer_new.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -675,20 +675,21 @@ def _of_interest(x): # noqa
675675
co_firstlineno = get_code_first_line(m)
676676
if cls is not None:
677677
if isinstance(cls.__dict__[m_name], (staticmethod, classmethod)):
678-
# skip it
679-
continue
680-
# partialize the function to get one without the 'self' argument
681-
new_m = functools.partial(m, cls())
682-
# remember the class
683-
setattr(new_m, _HOST_CLS_ATTR, cls)
684-
# we have to recopy all metadata concerning the case function
685-
new_m.__name__ = m.__name__
686-
copy_case_info(m, new_m)
687-
copy_pytest_marks(m, new_m, override=True)
688-
# also recopy all marks from the holding class to the function
689-
copy_pytest_marks(cls, new_m, override=False)
690-
m = new_m
691-
del new_m
678+
# nothing to do: no need to partialize a 'self' argument
679+
pass
680+
else:
681+
# partialize the function to get one without the 'self' argument
682+
new_m = functools.partial(m, cls())
683+
# remember the class
684+
setattr(new_m, _HOST_CLS_ATTR, cls)
685+
# we have to recopy all metadata concerning the case function
686+
new_m.__name__ = m.__name__
687+
copy_case_info(m, new_m)
688+
copy_pytest_marks(m, new_m, override=True)
689+
# also recopy all marks from the holding class to the function
690+
copy_pytest_marks(cls, new_m, override=False)
691+
m = new_m
692+
del new_m
692693

693694
if _case_param_factory is None:
694695
# Nominal usage: put the case in the dictionary
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
3+
from pytest_cases import parametrize_with_cases, case, parametrize
4+
5+
6+
class FooCases:
7+
@staticmethod
8+
def case_static():
9+
return 1
10+
11+
@classmethod
12+
def case_class(cls):
13+
return 1
14+
15+
@staticmethod
16+
@case(id="foo")
17+
def case_static_custom_id():
18+
return 1
19+
20+
@staticmethod
21+
@pytest.mark.skip
22+
def case_static_skipped():
23+
assert False, "should be skipped"
24+
25+
@classmethod
26+
@parametrize("o", [1])
27+
def case_class_fix(cls, o):
28+
return o
29+
30+
31+
@parametrize_with_cases("a", cases='.')
32+
def test_foo(a):
33+
assert a == 1
34+
35+
36+
def test_synthesis(module_results_dct):
37+
assert list(module_results_dct) == [
38+
'test_foo[static]',
39+
'test_foo[class]',
40+
'test_foo[foo]',
41+
'test_foo[class_fix-1]'
42+
]

0 commit comments

Comments
 (0)