Merge pull request #6152 from grlee77/module_name_in_id

use __name__ attribute in the parametrize id for modules as well
This commit is contained in:
Bruno Oliveira 2019-11-08 14:16:34 -03:00 committed by GitHub
commit 245e1f10e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 1 deletions

View File

@ -103,6 +103,7 @@ George Kussumoto
Georgy Dyuldin Georgy Dyuldin
Graham Horler Graham Horler
Greg Price Greg Price
Gregory Lee
Grig Gheorghiu Grig Gheorghiu
Grigorii Eremeev (budulianin) Grigorii Eremeev (budulianin)
Guido Wesdorp Guido Wesdorp

View File

@ -0,0 +1 @@
Now parametrization will use the ``__name__`` attribute of any object for the id, if present. Previously it would only use ``__name__`` for functions and classes.

View File

@ -1162,7 +1162,8 @@ def _idval(val, argname, idx, idfn, item, config):
return ascii_escaped(val.pattern) return ascii_escaped(val.pattern)
elif isinstance(val, enum.Enum): elif isinstance(val, enum.Enum):
return str(val) return str(val)
elif (inspect.isclass(val) or inspect.isfunction(val)) and hasattr(val, "__name__"): elif hasattr(val, "__name__") and isinstance(val.__name__, str):
# name of a class, function, module, etc.
return val.__name__ return val.__name__
return str(argname) + str(idx) return str(argname) + str(idx)

View File

@ -314,6 +314,21 @@ def test_keyword_option_parametrize(spec, testdir):
assert list(passed) == list(passed_result) assert list(passed) == list(passed_result)
def test_parametrize_with_module(testdir):
testdir.makepyfile(
"""
import pytest
@pytest.mark.parametrize("arg", [pytest,])
def test_func(arg):
pass
"""
)
rec = testdir.inline_run()
passed, skipped, fail = rec.listoutcomes()
expected_id = "test_func[" + pytest.__name__ + "]"
assert passed[0].nodeid.split("::")[-1] == expected_id
@pytest.mark.parametrize( @pytest.mark.parametrize(
"spec", "spec",
[ [