Skip to content

Commit 51912f9

Browse files
committed
No longer silently ignore errors in parametrize callable ids
1 parent 7592c5b commit 51912f9

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
*
55

6+
* pytest no longer ignores errors when a callable ids raises in a parametrized
7+
test. Thanks `@fogo`_ for the PR.
8+
69
* pytest no longer recognizes coroutine functions as yield tests (`#2129`_).
710
Thanks to `@malinoff`_ for the PR.
811

@@ -16,6 +19,7 @@
1619

1720
*
1821

22+
.. _@fogo: https://github.com/fogo
1923
.. _@lesteve: https://github.com/lesteve
2024
.. _@malinoff: https://github.com/malinoff
2125
.. _@pelme: https://github.com/pelme

_pytest/python.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,9 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect):
925925

926926
def _idval(val, argname, idx, idfn, config=None):
927927
if idfn:
928-
try:
929-
s = idfn(val)
930-
if s:
931-
return _escape_strings(s)
932-
except Exception:
933-
pass
928+
s = idfn(val)
929+
if s:
930+
return _escape_strings(s)
934931

935932
if config:
936933
hook_id = config.hook.pytest_make_parametrize_id(config=config, val=val)

testing/python/metafunc.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,35 @@ def test_idmaker_idfn_exception(self):
300300
def ids(val):
301301
raise Exception("bad code")
302302

303-
result = idmaker(("a", "b"), [(10.0, IndexError()),
304-
(20, KeyError()),
305-
("three", [1, 2, 3]),
306-
], idfn=ids)
307-
assert result == ["10.0-b0",
308-
"20-b1",
309-
"three-b2",
310-
]
303+
with pytest.raises(Exception) as e:
304+
idmaker(("a", "b"), [(10.0, IndexError()),
305+
(20, KeyError()),
306+
("three", [1, 2, 3]),
307+
], idfn=ids)
308+
309+
assert str(e.value) == "bad code"
310+
311+
def test_parametrize_ids_exception(self, testdir):
312+
"""
313+
:param testdir: the instance of Testdir class, a temporary
314+
test directory.
315+
"""
316+
testdir.makepyfile("""
317+
import pytest
318+
319+
def ids(arg):
320+
raise Exception("bad ids")
321+
322+
@pytest.mark.parametrize("arg", ["a", "b"], ids=ids)
323+
def test_foo(arg):
324+
pass
325+
""")
326+
result = testdir.runpytest("--collect-only")
327+
result.stdout.fnmatch_lines([
328+
"*ERROR collecting test_parametrize_ids_exception.py*",
329+
"test_parametrize_ids_exception.py:*: in ids",
330+
"*raise Exception(\"bad ids\")",
331+
])
311332

312333
def test_idmaker_with_ids(self):
313334
from _pytest.python import idmaker

0 commit comments

Comments
 (0)