Skip to content

Commit b363f51

Browse files
committed
fixup! No longer silently ignore errors in parametrize callable ids
1 parent cc38ce0 commit b363f51

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ New Features
88
* Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files.
99
Thanks `@wheerd`_ for the PR (`#2101`_).
1010

11-
* pytest no longer ignores errors when a callable ids raises in a parametrized
12-
test. Thanks `@fogo`_ for the PR.
11+
* pytest now warns when a callable ids raises in a parametrized test. Thanks `@fogo`_ for the PR.
1312

1413
*
1514

_pytest/python.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,12 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect):
928928

929929
def _idval(val, argname, idx, idfn, config=None):
930930
if idfn:
931-
s = idfn(val)
931+
s = None
932+
try:
933+
s = idfn(val)
934+
except Exception:
935+
import warnings
936+
warnings.warn("Raised while trying to determine id of parameter %s at position %d" % (argname, idx))
932937
if s:
933938
return _escape_strings(s)
934939

testing/python/metafunc.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,30 @@ def ids(val):
296296
@pytest.mark.issue351
297297
def test_idmaker_idfn_exception(self):
298298
from _pytest.python import idmaker
299+
from _pytest.recwarn import WarningsRecorder
299300

300301
class BadIdsException(Exception):
301302
pass
302303

303304
def ids(val):
304305
raise BadIdsException("ids raised")
305306

306-
with pytest.raises(BadIdsException) as e:
307+
rec = WarningsRecorder()
308+
with rec:
307309
idmaker(("a", "b"), [(10.0, IndexError()),
308310
(20, KeyError()),
309311
("three", [1, 2, 3]),
310312
], idfn=ids)
311313

312-
assert str(e.value) == "ids raised"
314+
assert [str(i.message) for i in rec.list] == [
315+
"Raised while trying to determine id of parameter a at position 0",
316+
"Raised while trying to determine id of parameter b at position 0",
317+
"Raised while trying to determine id of parameter a at position 1",
318+
"Raised while trying to determine id of parameter b at position 1",
319+
"Raised while trying to determine id of parameter a at position 2",
320+
"Raised while trying to determine id of parameter b at position 2",
321+
]
322+
313323

314324
def test_parametrize_ids_exception(self, testdir):
315325
"""
@@ -328,9 +338,9 @@ def test_foo(arg):
328338
""")
329339
result = testdir.runpytest("--collect-only")
330340
result.stdout.fnmatch_lines([
331-
"*ERROR collecting test_parametrize_ids_exception.py*",
332-
"test_parametrize_ids_exception.py:*: in ids",
333-
"*raise Exception(\"bad ids\")",
341+
"<Module 'test_parametrize_ids_exception.py'>",
342+
" <Function 'test_foo[a]'>",
343+
" <Function 'test_foo[b]'>",
334344
])
335345

336346
def test_idmaker_with_ids(self):

0 commit comments

Comments
 (0)