Improve error message when passing non-string ids to pytest.mark.parametrize
Fix #1857
This commit is contained in:
parent
ea0febad28
commit
972a5fb5d5
|
@ -3,13 +3,17 @@
|
||||||
|
|
||||||
*
|
*
|
||||||
|
|
||||||
*
|
* Improve error message when passing non-string ids to ``pytest.mark.parametrize`` (`#1857`_).
|
||||||
|
Thanks `@okken`_ for the report and `@nicoddemus`_ for the PR.
|
||||||
|
|
||||||
*
|
*
|
||||||
|
|
||||||
*
|
*
|
||||||
|
|
||||||
|
|
||||||
|
.. _#1857: https://github.com/pytest-dev/pytest/issues/1857
|
||||||
|
|
||||||
|
|
||||||
3.0.1
|
3.0.1
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
|
|
@ -778,6 +778,7 @@ class Metafunc(fixtures.FuncargnamesCompatAttr):
|
||||||
"""
|
"""
|
||||||
from _pytest.fixtures import scopes
|
from _pytest.fixtures import scopes
|
||||||
from _pytest.mark import extract_argvalue
|
from _pytest.mark import extract_argvalue
|
||||||
|
from py.io import saferepr
|
||||||
|
|
||||||
unwrapped_argvalues = []
|
unwrapped_argvalues = []
|
||||||
newkeywords = []
|
newkeywords = []
|
||||||
|
@ -831,9 +832,14 @@ class Metafunc(fixtures.FuncargnamesCompatAttr):
|
||||||
if callable(ids):
|
if callable(ids):
|
||||||
idfn = ids
|
idfn = ids
|
||||||
ids = None
|
ids = None
|
||||||
if ids and len(ids) != len(argvalues):
|
if ids:
|
||||||
raise ValueError('%d tests specified with %d ids' %(
|
if len(ids) != len(argvalues):
|
||||||
len(argvalues), len(ids)))
|
raise ValueError('%d tests specified with %d ids' %(
|
||||||
|
len(argvalues), len(ids)))
|
||||||
|
for id_value in ids:
|
||||||
|
if id_value is not None and not isinstance(id_value, str):
|
||||||
|
msg = 'ids must be list of strings, found: %s (type: %s)'
|
||||||
|
raise ValueError(msg % (saferepr(id_value), type(id_value).__name__))
|
||||||
ids = idmaker(argnames, argvalues, idfn, ids, self.config)
|
ids = idmaker(argnames, argvalues, idfn, ids, self.config)
|
||||||
newcalls = []
|
newcalls = []
|
||||||
for callspec in self._calls or [CallSpec2(self)]:
|
for callspec in self._calls or [CallSpec2(self)]:
|
||||||
|
|
|
@ -916,6 +916,18 @@ class TestMetafuncFunctional:
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines(['* 1 skipped *'])
|
result.stdout.fnmatch_lines(['* 1 skipped *'])
|
||||||
|
|
||||||
|
def test_parametrized_ids_invalid_type(self, testdir):
|
||||||
|
"""Tests parametrized with ids as non-strings (#1857)."""
|
||||||
|
testdir.makepyfile('''
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("x, expected", [(10, 20), (40, 80)], ids=(None, 2))
|
||||||
|
def test_ids_numbers(x,expected):
|
||||||
|
assert x * 2 == expected
|
||||||
|
''')
|
||||||
|
result = testdir.runpytest()
|
||||||
|
result.stdout.fnmatch_lines(['*ids must be list of strings, found: 2 (type: int)*'])
|
||||||
|
|
||||||
def test_parametrize_with_identical_ids_get_unique_names(self, testdir):
|
def test_parametrize_with_identical_ids_get_unique_names(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
Loading…
Reference in New Issue