Skip to content

Commit 77baea1

Browse files
author
Sylvain MARIE
committed
Major steps towards #154 :
- added an `alternative_index` attribute in `UnionFixtureAlternative` and uniformized the id making process for fixture unions and parametrize alternatives. - in particular added an `id` argument to `fixture_ref` and filled this `id` in `@parametrize_with_cases` so that the id is always the one from the case_id
1 parent 068257d commit 77baea1

File tree

4 files changed

+183
-89
lines changed

4 files changed

+183
-89
lines changed

pytest_cases/case_parametrizer_new.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ def case_to_argvalues(host_class_or_module, # type: Union[Type, ModuleType]
329329
# if meta.is_parametrized:
330330
# nothing to do, the parametrization marks are already there
331331

332-
# reference that case fixture
333-
argvalues_tuple = (fixture_ref(fix_name),)
332+
# reference that case fixture, and preserve the case id in the associated id whatever the generated fixture name
333+
argvalues_tuple = (fixture_ref(fix_name, id=case_id),)
334334
if debug:
335335
case_fun_str = qname(case_fun.func if isinstance(case_fun, functools.partial) else case_fun)
336336
print("Case function %s > fixture_ref(%r) with marks %s" % (case_fun_str, fix_name, case_marks))

pytest_cases/common_others.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,10 @@ def funcopy(f):
287287
# fun = functools.update_wrapper(fun, f)
288288
# fun.__kwdefaults__ = f.__kwdefaults__
289289
# return fun
290+
291+
292+
def robust_isinstance(o, cls):
293+
try:
294+
return isinstance(o, cls)
295+
except: # noqa
296+
return False

pytest_cases/fixture_core1_unions.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,30 @@ class UnionIdMakers(object):
4242
The enum defining all possible id styles for union fixture parameters ("alternatives")
4343
"""
4444
@classmethod
45-
def nostyle(cls, param):
46-
return param.alternative_name
45+
def nostyle(cls,
46+
param # type: UnionFixtureAlternative
47+
):
48+
""" ids are <fixture_name> """
49+
return param.get_id(prepend_index=False)
4750

4851
@classmethod
49-
def explicit(cls, param):
50-
return "%s_is_%s" % (param.union_name, param.alternative_name)
52+
def explicit(cls,
53+
param # type: UnionFixtureAlternative
54+
):
55+
""" ids are <union_name>_is_<fixture_name> """
56+
return "%s_is_%s" % (param.union_name, param.get_id(prepend_index=True))
5157

5258
@classmethod
53-
def compact(cls, param):
54-
return "U%s" % param.alternative_name
59+
def compact(cls,
60+
param # type: UnionFixtureAlternative
61+
):
62+
""" ids are U<index>/<fixture_name> """
63+
return param.get_id(prepend_index=True)
5564

5665
@classmethod
5766
def get(cls, style # type: str
5867
):
59-
# type: (...) -> Callable[[Any], str]
68+
# type: (...) -> Callable[[UnionFixtureAlternative], str]
6069
"""
6170
Returns a function that one can use as the `ids` argument in parametrize, applying the given id style.
6271
See https://github.com/smarie/python-pytest-cases/issues/41
@@ -73,14 +82,30 @@ def get(cls, style # type: str
7382

7483
class UnionFixtureAlternative(object):
7584
"""Defines an "alternative", used to parametrize a fixture union"""
76-
__slots__ = 'union_name', 'alternative_name'
85+
__slots__ = 'union_name', 'alternative_name', 'alternative_index'
7786

7887
def __init__(self,
79-
union_name,
80-
alternative_name,
88+
union_name, # type: str
89+
alternative_name, # type: str
90+
alternative_index # type: int
8191
):
92+
"""
93+
94+
:param union_name: the name of the union fixture
95+
:param alternative_name: the name of the fixture that will be used by the union fixture when this alternative
96+
is active
97+
:param alternative_index: the index of the alternative, used for ids generation
98+
"""
8299
self.union_name = union_name
83100
self.alternative_name = alternative_name
101+
self.alternative_index = alternative_index
102+
103+
def get_id(self, prepend_index=True):
104+
"""Used by the id makers to get the alternative id. Defaults to the alternative name"""
105+
if prepend_index:
106+
return "U%s/%s" % (self.alternative_index, self.alternative_name)
107+
else:
108+
return self.alternative_name
84109

85110
# def __str__(self):
86111
# # although this would be great to have a default id directly, it may be
@@ -89,7 +114,8 @@ def __init__(self,
89114
# return self.alternative_name
90115

91116
def __repr__(self):
92-
return "%s<%s=%s>" % (self.__class__.__name__, self.union_name, self.alternative_name)
117+
return "%s<%s=#%s=%s>" % (self.__class__.__name__, self.union_name, self.alternative_index,
118+
self.alternative_name)
93119

94120
@staticmethod
95121
def to_list_of_fixture_names(alternatives_lst # type: List[UnionFixtureAlternative]
@@ -260,9 +286,9 @@ def fixture_union(name, # type: str
260286
# create all alternatives and reapply the marks on them
261287
fix_alternatives = []
262288
f_names_args = []
263-
for _name, _id, _mark in zip(f_names, custom_pids, p_marks):
289+
for _idx, (_name, _id, _mark) in enumerate(zip(f_names, custom_pids, p_marks)):
264290
# create the alternative object
265-
alternative = UnionFixtureAlternative(union_name=name, alternative_name=_name)
291+
alternative = UnionFixtureAlternative(union_name=name, alternative_name=_name, alternative_index=_idx)
266292

267293
# remove duplicates in the fixture arguments: each is required only once by the union fixture to create
268294
if _name in f_names_args:

0 commit comments

Comments
 (0)