Merge pull request #4740 from asottile/bugfix_4739
Fix `parametrize(... ids=<function>)` when the function returns non-strings
This commit is contained in:
commit
ea732464aa
|
@ -0,0 +1 @@
|
||||||
|
Fix ``parametrize(... ids=<function>)`` when the function returns non-strings.
|
|
@ -1144,9 +1144,10 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect):
|
||||||
|
|
||||||
def _idval(val, argname, idx, idfn, item, config):
|
def _idval(val, argname, idx, idfn, item, config):
|
||||||
if idfn:
|
if idfn:
|
||||||
s = None
|
|
||||||
try:
|
try:
|
||||||
s = idfn(val)
|
generated_id = idfn(val)
|
||||||
|
if generated_id is not None:
|
||||||
|
val = generated_id
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# See issue https://github.com/pytest-dev/pytest/issues/2169
|
# See issue https://github.com/pytest-dev/pytest/issues/2169
|
||||||
msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
|
msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
|
||||||
|
@ -1154,10 +1155,7 @@ def _idval(val, argname, idx, idfn, item, config):
|
||||||
# we only append the exception type and message because on Python 2 reraise does nothing
|
# we only append the exception type and message because on Python 2 reraise does nothing
|
||||||
msg += " {}: {}\n".format(type(e).__name__, e)
|
msg += " {}: {}\n".format(type(e).__name__, e)
|
||||||
six.raise_from(ValueError(msg), e)
|
six.raise_from(ValueError(msg), e)
|
||||||
if s:
|
elif config:
|
||||||
return ascii_escaped(s)
|
|
||||||
|
|
||||||
if config:
|
|
||||||
hook_id = config.hook.pytest_make_parametrize_id(
|
hook_id = config.hook.pytest_make_parametrize_id(
|
||||||
config=config, val=val, argname=argname
|
config=config, val=val, argname=argname
|
||||||
)
|
)
|
||||||
|
|
|
@ -418,6 +418,21 @@ class TestMetafunc(object):
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_parametrize_ids_returns_non_string(self, testdir):
|
||||||
|
testdir.makepyfile(
|
||||||
|
"""\
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
def ids(d):
|
||||||
|
return d
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("arg", ({1: 2}, {3, 4}), ids=ids)
|
||||||
|
def test(arg):
|
||||||
|
assert arg
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
assert testdir.runpytest().ret == 0
|
||||||
|
|
||||||
def test_idmaker_with_ids(self):
|
def test_idmaker_with_ids(self):
|
||||||
from _pytest.python import idmaker
|
from _pytest.python import idmaker
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue