Merge pull request #2198 from unsignedint/make-parametrize-enhancement
Enhancement to `make_parametrize_id`hook function
This commit is contained in:
commit
34e98bce0a
1
AUTHORS
1
AUTHORS
|
@ -120,6 +120,7 @@ Quentin Pradet
|
||||||
Ralf Schmitt
|
Ralf Schmitt
|
||||||
Raphael Pierzina
|
Raphael Pierzina
|
||||||
Raquel Alegre
|
Raquel Alegre
|
||||||
|
Ravi Chandra
|
||||||
Roberto Polli
|
Roberto Polli
|
||||||
Romain Dorgueil
|
Romain Dorgueil
|
||||||
Roman Bolshakov
|
Roman Bolshakov
|
||||||
|
|
|
@ -44,6 +44,10 @@ Changes
|
||||||
* fix `#2208`_: ensure a iteration limit for _pytest.compat.get_real_func.
|
* fix `#2208`_: ensure a iteration limit for _pytest.compat.get_real_func.
|
||||||
Thanks `@RonnyPfannschmidt`_ for the Report and PR
|
Thanks `@RonnyPfannschmidt`_ for the Report and PR
|
||||||
|
|
||||||
|
* Modify ``pytest_make_parametrize_id()`` hook to accept ``argname`` as an
|
||||||
|
additional parameter.
|
||||||
|
Thanks `@unsignedint`_ for the PR.
|
||||||
|
|
||||||
|
|
||||||
.. _@davidszotten: https://github.com/davidszotten
|
.. _@davidszotten: https://github.com/davidszotten
|
||||||
.. _@fushi: https://github.com/fushi
|
.. _@fushi: https://github.com/fushi
|
||||||
|
@ -52,6 +56,7 @@ Changes
|
||||||
.. _@fogo: https://github.com/fogo
|
.. _@fogo: https://github.com/fogo
|
||||||
.. _@lesteve: https://github.com/lesteve
|
.. _@lesteve: https://github.com/lesteve
|
||||||
.. _@mandeep: https://github.com/mandeep
|
.. _@mandeep: https://github.com/mandeep
|
||||||
|
.. _@unsignedint: https://github.com/unsignedint
|
||||||
|
|
||||||
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
|
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
|
||||||
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
|
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
|
||||||
|
|
|
@ -157,9 +157,10 @@ def pytest_generate_tests(metafunc):
|
||||||
""" generate (multiple) parametrized calls to a test function."""
|
""" generate (multiple) parametrized calls to a test function."""
|
||||||
|
|
||||||
@hookspec(firstresult=True)
|
@hookspec(firstresult=True)
|
||||||
def pytest_make_parametrize_id(config, val):
|
def pytest_make_parametrize_id(config, val, argname):
|
||||||
"""Return a user-friendly string representation of the given ``val`` that will be used
|
"""Return a user-friendly string representation of the given ``val`` that will be used
|
||||||
by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
|
by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
|
||||||
|
The parameter name is available as ``argname``, if required.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
|
@ -197,7 +197,7 @@ def pytest_pycollect_makeitem(collector, name, obj):
|
||||||
res = list(collector._genfunctions(name, obj))
|
res = list(collector._genfunctions(name, obj))
|
||||||
outcome.force_result(res)
|
outcome.force_result(res)
|
||||||
|
|
||||||
def pytest_make_parametrize_id(config, val):
|
def pytest_make_parametrize_id(config, val, argname=None):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -941,7 +941,8 @@ def _idval(val, argname, idx, idfn, config=None):
|
||||||
return _escape_strings(s)
|
return _escape_strings(s)
|
||||||
|
|
||||||
if config:
|
if config:
|
||||||
hook_id = config.hook.pytest_make_parametrize_id(config=config, val=val)
|
hook_id = config.hook.pytest_make_parametrize_id(
|
||||||
|
config=config, val=val, argname=argname)
|
||||||
if hook_id:
|
if hook_id:
|
||||||
return hook_id
|
return hook_id
|
||||||
|
|
||||||
|
|
|
@ -1455,3 +1455,26 @@ class TestMarkersWithParametrization:
|
||||||
"*test_func*0*PASS*",
|
"*test_func*0*PASS*",
|
||||||
"*test_func*2*PASS*",
|
"*test_func*2*PASS*",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
def test_pytest_make_parametrize_id_with_argname(self, testdir):
|
||||||
|
testdir.makeconftest("""
|
||||||
|
def pytest_make_parametrize_id(config, val, argname):
|
||||||
|
return str(val * 2 if argname == 'x' else val * 10)
|
||||||
|
""")
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("x", range(2))
|
||||||
|
def test_func_a(x):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("y", [1])
|
||||||
|
def test_func_b(y):
|
||||||
|
pass
|
||||||
|
""")
|
||||||
|
result = testdir.runpytest("-v")
|
||||||
|
result.stdout.fnmatch_lines([
|
||||||
|
"*test_func_a*0*PASS*",
|
||||||
|
"*test_func_a*2*PASS*",
|
||||||
|
"*test_func_b*10*PASS*",
|
||||||
|
])
|
||||||
|
|
Loading…
Reference in New Issue