Skip to content

Commit

Permalink
Improve error message when passing non-string ids to pytest.mark.para…
Browse files Browse the repository at this point in the history
…metrize

Fix pytest-dev#1857
  • Loading branch information
nicoddemus committed Aug 24, 2016
1 parent ea0feba commit 21a23f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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 ints (regression in 3.0, #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

0 comments on commit 21a23f5

Please sign in to comment.