Fix `parametrize(... ids=<function>)` when the function returns non-strings.

This commit is contained in:
Anthony Sottile 2019-02-07 11:07:20 -08:00
parent 526f4a95cc
commit 4c7ddb8d9b
3 changed files with 20 additions and 6 deletions

View File

@ -0,0 +1 @@
Fix ``parametrize(... ids=<function>)`` when the function returns non-strings.

View File

@ -1144,9 +1144,10 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect):
def _idval(val, argname, idx, idfn, item, config):
if idfn:
s = None
try:
s = idfn(val)
generated_id = idfn(val)
if generated_id is not None:
val = generated_id
except Exception as e:
# See issue https://github.com/pytest-dev/pytest/issues/2169
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
msg += " {}: {}\n".format(type(e).__name__, e)
six.raise_from(ValueError(msg), e)
if s:
return ascii_escaped(s)
if config:
elif config:
hook_id = config.hook.pytest_make_parametrize_id(
config=config, val=val, argname=argname
)

View File

@ -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):
from _pytest.python import idmaker