Skip to content

Commit d7f6112

Browse files
author
Sylvain MARIE
committed
Fixed issue when parametrize_with_cases was used on case functions themselves (nesting/recursion). This was due to a lack of support of the place_as magic pytest attribute. Fixes #179
1 parent 3da0e40 commit d7f6112

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

pytest_cases/case_parametrizer_new.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,20 @@ def _of_interest(x): # noqa
672672
cases_dct[gen_line_nb] = _m_item
673673

674674
elif is_case_function(m, prefix=case_fun_prefix):
675-
co_firstlineno = get_code_first_line(m)
675+
try:
676+
# read pytest magic attribute "place_as" to make sure this is placed correctly
677+
m_for_placing = m.place_as
678+
except AttributeError:
679+
# nominal: get the first line of code
680+
co_firstlineno = get_code_first_line(m)
681+
else:
682+
# currently we only support replacing inside the same module
683+
if m_for_placing.__module__ != m.__module__:
684+
raise ValueError("Unsupported value for 'place_as' special pytest attribute on case function %s: %s"
685+
". Virtual placing in another module is not supported yet by pytest-cases."
686+
% (m, m_for_placing))
687+
co_firstlineno = get_code_first_line(m_for_placing)
688+
676689
if cls is not None:
677690
if isinstance(cls.__dict__[m_name], (staticmethod, classmethod)):
678691
# nothing to do: no need to partialize a 'self' argument
@@ -693,6 +706,9 @@ def _of_interest(x): # noqa
693706

694707
if _case_param_factory is None:
695708
# Nominal usage: put the case in the dictionary
709+
if co_firstlineno in cases_dct:
710+
raise ValueError("Error collecting case functions, line number used by %r is already used by %r !"
711+
% (m, cases_dct[co_firstlineno]))
696712
cases_dct[co_firstlineno] = m
697713
else:
698714
# Legacy usage where the cases generators were expanded here and inserted with a virtual line no
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest_cases as pytest
2+
3+
4+
@pytest.fixture
5+
def db_dep():
6+
return None
7+
8+
9+
class CaseX:
10+
def case_one(self, db_dep):
11+
return 1
12+
13+
def case_two(self, db_dep):
14+
return 2
15+
16+
17+
class CaseY:
18+
@pytest.parametrize_with_cases("x", cases=CaseX, debug=True)
19+
def case_x_one(self,db_dep,x):
20+
return x, 1
21+
22+
@pytest.parametrize_with_cases("x", cases=CaseX, debug=True)
23+
def case_x_two(self,db_dep,x):
24+
return x, 1
25+
26+
27+
@pytest.parametrize_with_cases("x,y", cases=CaseY, debug=True)
28+
def test_nested_parametrize(x, y):
29+
pass
30+
31+
32+
def test_synthesis(module_results_dct):
33+
assert list(module_results_dct) == [
34+
'test_nested_parametrize[x_one-one]',
35+
'test_nested_parametrize[x_one-two]',
36+
'test_nested_parametrize[x_two-one]',
37+
'test_nested_parametrize[x_two-two]'
38+
]

0 commit comments

Comments
 (0)