@@ -42,21 +42,30 @@ class UnionIdMakers(object):
42
42
The enum defining all possible id styles for union fixture parameters ("alternatives")
43
43
"""
44
44
@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 )
47
50
48
51
@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 ))
51
57
52
58
@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 )
55
64
56
65
@classmethod
57
66
def get (cls , style # type: str
58
67
):
59
- # type: (...) -> Callable[[Any ], str]
68
+ # type: (...) -> Callable[[UnionFixtureAlternative ], str]
60
69
"""
61
70
Returns a function that one can use as the `ids` argument in parametrize, applying the given id style.
62
71
See https://github.com/smarie/python-pytest-cases/issues/41
@@ -73,14 +82,30 @@ def get(cls, style # type: str
73
82
74
83
class UnionFixtureAlternative (object ):
75
84
"""Defines an "alternative", used to parametrize a fixture union"""
76
- __slots__ = 'union_name' , 'alternative_name'
85
+ __slots__ = 'union_name' , 'alternative_name' , 'alternative_index'
77
86
78
87
def __init__ (self ,
79
- union_name ,
80
- alternative_name ,
88
+ union_name , # type: str
89
+ alternative_name , # type: str
90
+ alternative_index # type: int
81
91
):
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
+ """
82
99
self .union_name = union_name
83
100
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
84
109
85
110
# def __str__(self):
86
111
# # although this would be great to have a default id directly, it may be
@@ -89,7 +114,8 @@ def __init__(self,
89
114
# return self.alternative_name
90
115
91
116
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 )
93
119
94
120
@staticmethod
95
121
def to_list_of_fixture_names (alternatives_lst # type: List[UnionFixtureAlternative]
@@ -260,9 +286,9 @@ def fixture_union(name, # type: str
260
286
# create all alternatives and reapply the marks on them
261
287
fix_alternatives = []
262
288
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 ) ):
264
290
# 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 )
266
292
267
293
# remove duplicates in the fixture arguments: each is required only once by the union fixture to create
268
294
if _name in f_names_args :
0 commit comments