Skip to content

Commit cc38ce0

Browse files
committed
No longer silently ignore errors in parametrize callable ids
1 parent 964ccb9 commit cc38ce0

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ 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.
13+
1114
*
1215

1316

@@ -39,6 +42,7 @@ Changes
3942
.. _@fushi: https://github.com/fushi
4043
.. _@mattduck: https://github.com/mattduck
4144
.. _@wheerd: https://github.com/wheerd
45+
.. _@fogo: https://github.com/fogo
4246

4347
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
4448
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874

_pytest/python.py

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

929929
def _idval(val, argname, idx, idfn, config=None):
930930
if idfn:
931-
try:
932-
s = idfn(val)
933-
if s:
934-
return _escape_strings(s)
935-
except Exception:
936-
pass
931+
s = idfn(val)
932+
if s:
933+
return _escape_strings(s)
937934

938935
if config:
939936
hook_id = config.hook.pytest_make_parametrize_id(config=config, val=val)

testing/python/metafunc.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,41 @@ def ids(val):
297297
def test_idmaker_idfn_exception(self):
298298
from _pytest.python import idmaker
299299

300+
class BadIdsException(Exception):
301+
pass
302+
300303
def ids(val):
301-
raise Exception("bad code")
304+
raise BadIdsException("ids raised")
302305

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-
]
306+
with pytest.raises(BadIdsException) as e:
307+
idmaker(("a", "b"), [(10.0, IndexError()),
308+
(20, KeyError()),
309+
("three", [1, 2, 3]),
310+
], idfn=ids)
311+
312+
assert str(e.value) == "ids raised"
313+
314+
def test_parametrize_ids_exception(self, testdir):
315+
"""
316+
:param testdir: the instance of Testdir class, a temporary
317+
test directory.
318+
"""
319+
testdir.makepyfile("""
320+
import pytest
321+
322+
def ids(arg):
323+
raise Exception("bad ids")
324+
325+
@pytest.mark.parametrize("arg", ["a", "b"], ids=ids)
326+
def test_foo(arg):
327+
pass
328+
""")
329+
result = testdir.runpytest("--collect-only")
330+
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\")",
334+
])
311335

312336
def test_idmaker_with_ids(self):
313337
from _pytest.python import idmaker

0 commit comments

Comments
 (0)