Skip to content

Commit 6bee356

Browse files
author
Sylvain MARIE
committed
Fixed issue with @parametrize_with_cases when two cases with the same id and both requiring a fixture were to be created. Fixed #117
1 parent bb993eb commit 6bee356

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

pytest_cases/case_parametrizer_new.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import re
88
from warnings import warn
99

10-
from pytest_cases import fixture
11-
1210
try:
1311
from typing import Union, Callable, Iterable, Any, Type, List, Tuple # noqa
1412
except ImportError:
@@ -20,9 +18,12 @@
2018
from .common_pytest_lazy_values import lazy_value
2119
from .common_pytest import safe_isclass, MiniMetafunc
2220

21+
from . import fixture
2322
from .case_funcs_new import matches_tag_query, is_case_function, is_case_class, CaseInfo, CASE_PREFIX_FUN
23+
from .fixture__creation import check_name_available, CHANGE
2424
from .fixture_parametrize_plus import fixture_ref, _parametrize_plus
2525

26+
2627
THIS_MODULE = object()
2728
"""Singleton that can be used instead of a module name to indicate that the module is the current one"""
2829

@@ -280,19 +281,19 @@ def case_to_argvalues(case_fun, # type: Callable
280281

281282
host_module = import_module(case_fun.__module__)
282283

283-
# create a new fixture and place it on the host (note: if already done, no need to recreate it)
284-
existing_fix = getattr(host_cls or host_module, case_id, None)
285-
if existing_fix is None:
286-
# if meta.is_parametrized:
287-
# nothing to do, the parametrization marks are already there
288-
new_fix = fixture(name=case_id)(case_fun)
289-
setattr(host_cls or host_module, case_id, new_fix)
290-
else:
291-
raise NotImplementedError("We should check if this is the same or another and generate a new name in that "
292-
"case")
284+
# create a new fixture and place it on the host
285+
# we have to create a unique fixture name if the fixture already exists.
286+
def name_changer(name, i):
287+
return name + '_' * i
288+
new_fix_name = check_name_available(host_cls or host_module, name=case_id, if_name_exists=CHANGE,
289+
name_changer=name_changer)
290+
# if meta.is_parametrized:
291+
# nothing to do, the parametrization marks are already there
292+
new_fix = fixture(name=new_fix_name)(case_fun)
293+
setattr(host_cls or host_module, new_fix_name, new_fix)
293294

294295
# now reference the new or existing fixture
295-
argvalues_tuple = (fixture_ref(case_id),)
296+
argvalues_tuple = (fixture_ref(new_fix_name),)
296297
return make_marked_parameter_value(argvalues_tuple, marks=case_marks) if case_marks else argvalues_tuple
297298

298299

pytest_cases/fixture__creation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __str__(self):
3333
def check_name_available(module,
3434
name, # type: str
3535
if_name_exists=RAISE, # type: int
36+
name_changer=None, # type: Callable
3637
caller=None, # type: Callable[[Any], Any]
3738
):
3839
"""
@@ -41,9 +42,15 @@ def check_name_available(module,
4142
:param module:
4243
:param name:
4344
:param if_name_exists:
45+
:param name_changer:
4446
:param caller:
4547
:return: a name that might be different if policy was CHANGE
4648
"""
49+
if name_changer is None:
50+
# default style for name changing. i starts with 1
51+
def name_changer(name, i):
52+
return name + '_%s' % i
53+
4754
if name in dir(module):
4855
if caller is None:
4956
caller = ''
@@ -58,10 +65,11 @@ def check_name_available(module,
5865
elif if_name_exists is CHANGE:
5966
# find a non-used name in that module
6067
i = 1
61-
name2 = name + '_%s' % i
68+
name2 = name_changer(name, i)
6269
while name2 in dir(module):
6370
i += 1
64-
name2 = name + '_%s' % i
71+
name2 = name_changer(name, i)
72+
6573
name = name2
6674
else:
6775
raise ValueError("invalid value for `if_name_exists`: %s" % if_name_exists)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pytest_cases import parametrize_with_cases
2+
3+
4+
def a_case(monkeypatch):
5+
return True
6+
7+
8+
def b_case(request):
9+
return True
10+
11+
12+
@parametrize_with_cases("a", cases=".", prefix="a_")
13+
@parametrize_with_cases("b", cases=".", prefix="b_")
14+
def test_something(a, b):
15+
pass

0 commit comments

Comments
 (0)