Skip to content

Commit a9193a1

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

3 files changed

Lines changed: 60 additions & 12 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ 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 now warns when a callable ids raises in a parametrized test. Thanks `@fogo`_ for the PR.
12+
1113
*
1214

1315

@@ -39,6 +41,7 @@ Changes
3941
.. _@fushi: https://github.com/fushi
4042
.. _@mattduck: https://github.com/mattduck
4143
.. _@wheerd: https://github.com/wheerd
44+
.. _@fogo: https://github.com/fogo
4245

4346
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
4447
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874

_pytest/python.py

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

929929
def _idval(val, argname, idx, idfn, config=None):
930930
if idfn:
931+
s = None
931932
try:
932933
s = idfn(val)
933-
if s:
934-
return _escape_strings(s)
935934
except Exception:
936-
pass
935+
# See issue https://github.com/pytest-dev/pytest/issues/2169
936+
import warnings
937+
msg = "Raised while trying to determine id of parameter %s at position %d." % (argname, idx)
938+
msg += '\nUpdate your code as this will raise an error in pytest-4.0.'
939+
warnings.warn(msg)
940+
if s:
941+
return _escape_strings(s)
937942

938943
if config:
939944
hook_id = config.hook.pytest_make_parametrize_id(config=config, val=val)

testing/python/metafunc.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,58 @@ 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
300+
301+
class BadIdsException(Exception):
302+
pass
299303

300304
def ids(val):
301-
raise Exception("bad code")
305+
raise BadIdsException("ids raised")
306+
307+
rec = WarningsRecorder()
308+
with rec:
309+
idmaker(("a", "b"), [(10.0, IndexError()),
310+
(20, KeyError()),
311+
("three", [1, 2, 3]),
312+
], idfn=ids)
313+
314+
assert [str(i.message) for i in rec.list] == [
315+
"Raised while trying to determine id of parameter a at position 0."
316+
"\nUpdate your code as this will raise an error in pytest-4.0.",
317+
"Raised while trying to determine id of parameter b at position 0."
318+
"\nUpdate your code as this will raise an error in pytest-4.0.",
319+
"Raised while trying to determine id of parameter a at position 1."
320+
"\nUpdate your code as this will raise an error in pytest-4.0.",
321+
"Raised while trying to determine id of parameter b at position 1."
322+
"\nUpdate your code as this will raise an error in pytest-4.0.",
323+
"Raised while trying to determine id of parameter a at position 2."
324+
"\nUpdate your code as this will raise an error in pytest-4.0.",
325+
"Raised while trying to determine id of parameter b at position 2."
326+
"\nUpdate your code as this will raise an error in pytest-4.0.",
327+
]
302328

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-
]
329+
330+
def test_parametrize_ids_exception(self, testdir):
331+
"""
332+
:param testdir: the instance of Testdir class, a temporary
333+
test directory.
334+
"""
335+
testdir.makepyfile("""
336+
import pytest
337+
338+
def ids(arg):
339+
raise Exception("bad ids")
340+
341+
@pytest.mark.parametrize("arg", ["a", "b"], ids=ids)
342+
def test_foo(arg):
343+
pass
344+
""")
345+
result = testdir.runpytest("--collect-only")
346+
result.stdout.fnmatch_lines([
347+
"<Module 'test_parametrize_ids_exception.py'>",
348+
" <Function 'test_foo[a]'>",
349+
" <Function 'test_foo[b]'>",
350+
])
311351

312352
def test_idmaker_with_ids(self):
313353
from _pytest.python import idmaker

0 commit comments

Comments
 (0)