Merge pull request #4556 from nicoddemus/idfunc-failure

Errors in parametrize id functions now propagate the error instead of issuing a warning
This commit is contained in:
Bruno Oliveira 2018-12-17 10:36:15 -02:00 committed by GitHub
commit 75e93e5168
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 14 deletions

View File

@ -0,0 +1 @@
``pytest.mark.parametrize``: in previous versions, errors raised by id functions were suppressed and changed into warnings. Now the exceptions are propagated, along with a pytest message informing the node, parameter value and index where the exception occurred.

View File

@ -45,7 +45,6 @@ from _pytest.mark.structures import transfer_markers
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.pathlib import parts from _pytest.pathlib import parts
from _pytest.warning_types import PytestWarning from _pytest.warning_types import PytestWarning
from _pytest.warning_types import RemovedInPytest4Warning
def pyobj_property(name): def pyobj_property(name):
@ -1059,13 +1058,11 @@ def _idval(val, argname, idx, idfn, item, config):
s = idfn(val) s = idfn(val)
except Exception as e: except Exception as e:
# See issue https://github.com/pytest-dev/pytest/issues/2169 # See issue https://github.com/pytest-dev/pytest/issues/2169
msg = ( msg = "{}: error raised while trying to determine id of parameter '{}' at position {}\n"
"While trying to determine id of parameter {} at position " msg = msg.format(item.nodeid, argname, idx)
"{} the following exception was raised:\n".format(argname, idx) # we only append the exception type and message because on Python 2 reraise does nothing
)
msg += " {}: {}\n".format(type(e).__name__, e) msg += " {}: {}\n".format(type(e).__name__, e)
msg += "This warning will be an error error in pytest-4.0." six.raise_from(ValueError(msg), e)
item.warn(RemovedInPytest4Warning(msg))
if s: if s:
return ascii_escaped(s) return ascii_escaped(s)

View File

@ -393,7 +393,6 @@ class TestMetafunc(object):
) )
assert result == ["a-a0", "a-a1", "a-a2"] assert result == ["a-a0", "a-a1", "a-a2"]
@pytest.mark.filterwarnings("default")
def test_parametrize_ids_exception(self, testdir): def test_parametrize_ids_exception(self, testdir):
""" """
:param testdir: the instance of Testdir class, a temporary :param testdir: the instance of Testdir class, a temporary
@ -411,14 +410,11 @@ class TestMetafunc(object):
pass pass
""" """
) )
result = testdir.runpytest("--collect-only", SHOW_PYTEST_WARNINGS_ARG) result = testdir.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"<Module test_parametrize_ids_exception.py>", "*test_foo: error raised while trying to determine id of parameter 'arg' at position 0",
" <Function test_foo[a]>", "*Exception: bad ids",
" <Function test_foo[b]>",
"*test_parametrize_ids_exception.py:6: *parameter arg at position 0*",
"*test_parametrize_ids_exception.py:6: *parameter arg at position 1*",
] ]
) )