Added pytest_make_parametrize_id hook
This commit is contained in:
parent
52babba33e
commit
79927428d1
|
@ -133,6 +133,12 @@ def pytest_deselected(items):
|
||||||
def pytest_make_collect_report(collector):
|
def pytest_make_collect_report(collector):
|
||||||
""" perform ``collector.collect()`` and return a CollectReport. """
|
""" perform ``collector.collect()`` and return a CollectReport. """
|
||||||
|
|
||||||
|
@hookspec(firstresult=True)
|
||||||
|
def pytest_make_parametrize_id(val):
|
||||||
|
"""Return a user-friendly string representation of the given ``val`` that will be used
|
||||||
|
by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
|
||||||
|
"""
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# Python test function related hooks
|
# Python test function related hooks
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
|
@ -342,6 +342,9 @@ def pytest_pycollect_makeitem(collector, name, obj):
|
||||||
res = list(collector._genfunctions(name, obj))
|
res = list(collector._genfunctions(name, obj))
|
||||||
outcome.force_result(res)
|
outcome.force_result(res)
|
||||||
|
|
||||||
|
def pytest_make_parametrize_id(val):
|
||||||
|
return None
|
||||||
|
|
||||||
def is_generator(func):
|
def is_generator(func):
|
||||||
try:
|
try:
|
||||||
return _pytest._code.getrawcode(func).co_flags & 32 # generator function
|
return _pytest._code.getrawcode(func).co_flags & 32 # generator function
|
||||||
|
@ -1030,7 +1033,7 @@ class Metafunc(FuncargnamesCompatAttr):
|
||||||
if ids and len(ids) != len(argvalues):
|
if ids and len(ids) != len(argvalues):
|
||||||
raise ValueError('%d tests specified with %d ids' %(
|
raise ValueError('%d tests specified with %d ids' %(
|
||||||
len(argvalues), len(ids)))
|
len(argvalues), len(ids)))
|
||||||
ids = idmaker(argnames, argvalues, idfn, ids)
|
ids = idmaker(argnames, argvalues, idfn, ids, self.config)
|
||||||
newcalls = []
|
newcalls = []
|
||||||
for callspec in self._calls or [CallSpec2(self)]:
|
for callspec in self._calls or [CallSpec2(self)]:
|
||||||
for param_index, valset in enumerate(argvalues):
|
for param_index, valset in enumerate(argvalues):
|
||||||
|
@ -1130,7 +1133,7 @@ else:
|
||||||
return val.encode('unicode-escape')
|
return val.encode('unicode-escape')
|
||||||
|
|
||||||
|
|
||||||
def _idval(val, argname, idx, idfn):
|
def _idval(val, argname, idx, idfn, config):
|
||||||
if idfn:
|
if idfn:
|
||||||
try:
|
try:
|
||||||
s = idfn(val)
|
s = idfn(val)
|
||||||
|
@ -1139,6 +1142,11 @@ def _idval(val, argname, idx, idfn):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if config:
|
||||||
|
hook_id = config.hook.pytest_make_parametrize_id(val=val)
|
||||||
|
if hook_id:
|
||||||
|
return hook_id
|
||||||
|
|
||||||
if isinstance(val, (bytes, str)) or (_PY2 and isinstance(val, unicode)):
|
if isinstance(val, (bytes, str)) or (_PY2 and isinstance(val, unicode)):
|
||||||
return _escape_strings(val)
|
return _escape_strings(val)
|
||||||
elif isinstance(val, (float, int, bool, NoneType)):
|
elif isinstance(val, (float, int, bool, NoneType)):
|
||||||
|
@ -1151,16 +1159,16 @@ def _idval(val, argname, idx, idfn):
|
||||||
return val.__name__
|
return val.__name__
|
||||||
return str(argname)+str(idx)
|
return str(argname)+str(idx)
|
||||||
|
|
||||||
def _idvalset(idx, valset, argnames, idfn, ids):
|
def _idvalset(idx, valset, argnames, idfn, ids, config):
|
||||||
if ids is None or ids[idx] is None:
|
if ids is None or ids[idx] is None:
|
||||||
this_id = [_idval(val, argname, idx, idfn)
|
this_id = [_idval(val, argname, idx, idfn, config)
|
||||||
for val, argname in zip(valset, argnames)]
|
for val, argname in zip(valset, argnames)]
|
||||||
return "-".join(this_id)
|
return "-".join(this_id)
|
||||||
else:
|
else:
|
||||||
return _escape_strings(ids[idx])
|
return _escape_strings(ids[idx])
|
||||||
|
|
||||||
def idmaker(argnames, argvalues, idfn=None, ids=None):
|
def idmaker(argnames, argvalues, idfn=None, ids=None, config=None):
|
||||||
ids = [_idvalset(valindex, valset, argnames, idfn, ids)
|
ids = [_idvalset(valindex, valset, argnames, idfn, ids, config)
|
||||||
for valindex, valset in enumerate(argvalues)]
|
for valindex, valset in enumerate(argvalues)]
|
||||||
if len(set(ids)) != len(ids):
|
if len(set(ids)) != len(ids):
|
||||||
# The ids are not unique
|
# The ids are not unique
|
||||||
|
|
Loading…
Reference in New Issue