Skip to content

Commit 7992742

Browse files
committed
Added pytest_make_parametrize_id hook
1 parent 52babba commit 7992742

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

_pytest/hookspec.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ def pytest_deselected(items):
133133
def pytest_make_collect_report(collector):
134134
""" perform ``collector.collect()`` and return a CollectReport. """
135135

136+
@hookspec(firstresult=True)
137+
def pytest_make_parametrize_id(val):
138+
"""Return a user-friendly string representation of the given ``val`` that will be used
139+
by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
140+
"""
141+
136142
# -------------------------------------------------------------------------
137143
# Python test function related hooks
138144
# -------------------------------------------------------------------------

_pytest/python.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ def pytest_pycollect_makeitem(collector, name, obj):
342342
res = list(collector._genfunctions(name, obj))
343343
outcome.force_result(res)
344344

345+
def pytest_make_parametrize_id(val):
346+
return None
347+
345348
def is_generator(func):
346349
try:
347350
return _pytest._code.getrawcode(func).co_flags & 32 # generator function
@@ -1030,7 +1033,7 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
10301033
if ids and len(ids) != len(argvalues):
10311034
raise ValueError('%d tests specified with %d ids' %(
10321035
len(argvalues), len(ids)))
1033-
ids = idmaker(argnames, argvalues, idfn, ids)
1036+
ids = idmaker(argnames, argvalues, idfn, ids, self.config)
10341037
newcalls = []
10351038
for callspec in self._calls or [CallSpec2(self)]:
10361039
for param_index, valset in enumerate(argvalues):
@@ -1130,7 +1133,7 @@ def _escape_strings(val):
11301133
return val.encode('unicode-escape')
11311134

11321135

1133-
def _idval(val, argname, idx, idfn):
1136+
def _idval(val, argname, idx, idfn, config):
11341137
if idfn:
11351138
try:
11361139
s = idfn(val)
@@ -1139,6 +1142,11 @@ def _idval(val, argname, idx, idfn):
11391142
except Exception:
11401143
pass
11411144

1145+
if config:
1146+
hook_id = config.hook.pytest_make_parametrize_id(val=val)
1147+
if hook_id:
1148+
return hook_id
1149+
11421150
if isinstance(val, (bytes, str)) or (_PY2 and isinstance(val, unicode)):
11431151
return _escape_strings(val)
11441152
elif isinstance(val, (float, int, bool, NoneType)):
@@ -1151,16 +1159,16 @@ def _idval(val, argname, idx, idfn):
11511159
return val.__name__
11521160
return str(argname)+str(idx)
11531161

1154-
def _idvalset(idx, valset, argnames, idfn, ids):
1162+
def _idvalset(idx, valset, argnames, idfn, ids, config):
11551163
if ids is None or ids[idx] is None:
1156-
this_id = [_idval(val, argname, idx, idfn)
1164+
this_id = [_idval(val, argname, idx, idfn, config)
11571165
for val, argname in zip(valset, argnames)]
11581166
return "-".join(this_id)
11591167
else:
11601168
return _escape_strings(ids[idx])
11611169

1162-
def idmaker(argnames, argvalues, idfn=None, ids=None):
1163-
ids = [_idvalset(valindex, valset, argnames, idfn, ids)
1170+
def idmaker(argnames, argvalues, idfn=None, ids=None, config=None):
1171+
ids = [_idvalset(valindex, valset, argnames, idfn, ids, config)
11641172
for valindex, valset in enumerate(argvalues)]
11651173
if len(set(ids)) != len(ids):
11661174
# The ids are not unique

0 commit comments

Comments
 (0)