Skip to content

Commit

Permalink
Merge pull request #2167 from fogo/parametrize-ids-silent-failure
Browse files Browse the repository at this point in the history
No longer silently ignore errors in parametrize callable ids
  • Loading branch information
RonnyPfannschmidt authored Jan 3, 2017
2 parents 964ccb9 + a9193a1 commit 8f14501
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ New Features
* Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files.
Thanks `@wheerd`_ for the PR (`#2101`_).

* pytest now warns when a callable ids raises in a parametrized test. Thanks `@fogo`_ for the PR.

*


Expand Down Expand Up @@ -39,6 +41,7 @@ Changes
.. _@fushi: https://github.com/fushi
.. _@mattduck: https://github.com/mattduck
.. _@wheerd: https://github.com/wheerd
.. _@fogo: https://github.com/fogo

.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
Expand Down
11 changes: 8 additions & 3 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,17 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect):

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

if config:
hook_id = config.hook.pytest_make_parametrize_id(config=config, val=val)
Expand Down
58 changes: 49 additions & 9 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,58 @@ def ids(val):
@pytest.mark.issue351
def test_idmaker_idfn_exception(self):
from _pytest.python import idmaker
from _pytest.recwarn import WarningsRecorder

class BadIdsException(Exception):
pass

def ids(val):
raise Exception("bad code")
raise BadIdsException("ids raised")

rec = WarningsRecorder()
with rec:
idmaker(("a", "b"), [(10.0, IndexError()),
(20, KeyError()),
("three", [1, 2, 3]),
], idfn=ids)

assert [str(i.message) for i in rec.list] == [
"Raised while trying to determine id of parameter a at position 0."
"\nUpdate your code as this will raise an error in pytest-4.0.",
"Raised while trying to determine id of parameter b at position 0."
"\nUpdate your code as this will raise an error in pytest-4.0.",
"Raised while trying to determine id of parameter a at position 1."
"\nUpdate your code as this will raise an error in pytest-4.0.",
"Raised while trying to determine id of parameter b at position 1."
"\nUpdate your code as this will raise an error in pytest-4.0.",
"Raised while trying to determine id of parameter a at position 2."
"\nUpdate your code as this will raise an error in pytest-4.0.",
"Raised while trying to determine id of parameter b at position 2."
"\nUpdate your code as this will raise an error in pytest-4.0.",
]

result = idmaker(("a", "b"), [(10.0, IndexError()),
(20, KeyError()),
("three", [1, 2, 3]),
], idfn=ids)
assert result == ["10.0-b0",
"20-b1",
"three-b2",
]

def test_parametrize_ids_exception(self, testdir):
"""
:param testdir: the instance of Testdir class, a temporary
test directory.
"""
testdir.makepyfile("""
import pytest
def ids(arg):
raise Exception("bad ids")
@pytest.mark.parametrize("arg", ["a", "b"], ids=ids)
def test_foo(arg):
pass
""")
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines([
"<Module 'test_parametrize_ids_exception.py'>",
" <Function 'test_foo[a]'>",
" <Function 'test_foo[b]'>",
])

def test_idmaker_with_ids(self):
from _pytest.python import idmaker
Expand Down

0 comments on commit 8f14501

Please sign in to comment.