Skip to content

Instantly share code, notes, and snippets.

@nicoddemus
Created March 7, 2018 23:20
Show Gist options
  • Save nicoddemus/98681852492997a383fec5969168df3e to your computer and use it in GitHub Desktop.
Save nicoddemus/98681852492997a383fec5969168df3e to your computer and use it in GitHub Desktop.
Obtain parametrized names and values of a failed parametrized test
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item):
outcome = yield
report = outcome.get_result()
parametrize_mark = item.get_marker('parametrize')
if report.failed and parametrize_mark:
# @pytest.mark.parametrize can accept "x, y" or ["x", "y"] as argument names
args = parametrize_mark.args[0]
if isinstance(args, str):
args = [x.strip() for x in args.split(',')]
# obtain the value of each parametrized argument
request = item._request
param_values = {attr: request.getfixturevalue(attr) for attr in args}
print('\n', item.nodeid, 'failed, parametrize args:', param_values)
import pytest
@pytest.fixture
def z():
return 666
@pytest.mark.parametrize('x, y', [(0, 500), (5, 1000), (15, 1500)])
def test_foo(x, y, z):
if x > 10:
assert False
$ pytest .tmp\elyezer\test_foo.py -q --tb=no
..
test_foo.py::test_foo[15-1500] failed, parametrize args: {'x': 15, 'y': 1500}
F
@elyezer
Copy link

elyezer commented Mar 8, 2018

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment