Skip to content

Commit

Permalink
adjust test_parametrized_ids_invalid_type, create list to convert tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Nov 13, 2019
1 parent ea2b62f commit 1ebe078
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ def _resolve_arg_ids(self, argnames, ids, parameters, item):

def _validate_ids(self, ids, parameters, func_name):
try:
len_ids = len(ids)
len(ids)
except TypeError:
try:
it = iter(ids)
Expand All @@ -1039,13 +1039,13 @@ def _validate_ids(self, ids, parameters, func_name):
else:
import itertools

ids = list(itertools.islice(it, len(parameters)))
len_ids = len(ids)
new_ids = list(itertools.islice(it, len(parameters)))
else:
new_ids = list(ids)

if len_ids != len(parameters):
if len(new_ids) != len(parameters):
msg = "In {}: {} parameter sets specified, with different number of ids: {}"
fail(msg.format(func_name, len(parameters), len(ids)), pytrace=False)
new_ids = ids[:]
for idx, id_value in enumerate(new_ids):
if id_value is not None:
if isinstance(id_value, int):
Expand Down
7 changes: 4 additions & 3 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,20 +1186,21 @@ def test_temp(temp):
result.stdout.fnmatch_lines(["* 1 skipped *"])

def test_parametrized_ids_invalid_type(self, testdir):
"""Tests parametrized with ids as non-strings (#1857)."""
"""Test error with non-strings/non-ints, without generator (#1857)."""
testdir.makepyfile(
"""
import pytest
@pytest.mark.parametrize("x, expected", [(10, 20), (40, 80)], ids=(None, 2))
@pytest.mark.parametrize("x, expected", [(1, 2), (3, 4), (5, 6)], ids=(None, 2, type))
def test_ids_numbers(x,expected):
assert x * 2 == expected
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*In test_ids_numbers: ids must be list of strings, found: 2 (type: *'int'>)*"
"In test_ids_numbers: ids must be list of strings, "
"found: <class 'type'> (type: <class 'type'>) at index 2"
]
)

Expand Down

0 comments on commit 1ebe078

Please sign in to comment.