Created
March 7, 2018 23:20
-
-
Save nicoddemus/98681852492997a383fec5969168df3e to your computer and use it in GitHub Desktop.
Obtain parametrized names and values of a failed parametrized test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pytest .tmp\elyezer\test_foo.py -q --tb=no | |
.. | |
test_foo.py::test_foo[15-1500] failed, parametrize args: {'x': 15, 'y': 1500} | |
F |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much!