Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====

Expand Down
12 changes: 9 additions & 3 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
"""
from _pytest.fixtures import scopes
from _pytest.mark import extract_argvalue
from py.io import saferepr

unwrapped_argvalues = []
newkeywords = []
Expand Down Expand Up @@ -831,9 +832,14 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
if callable(ids):
idfn = ids
ids = None
if ids and len(ids) != len(argvalues):
raise ValueError('%d tests specified with %d ids' %(
len(argvalues), len(ids)))
if ids:
if len(ids) != len(argvalues):
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)
newcalls = []
for callspec in self._calls or [CallSpec2(self)]:
Expand Down
12 changes: 12 additions & 0 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,18 @@ def test_temp(temp):
result = testdir.runpytest()
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):
testdir.makepyfile("""
import pytest
Expand Down