diff --git a/src/_pytest/python.py b/src/_pytest/python.py index bf350ed643a..932a20799a6 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -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) @@ -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): diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 8a63ca77b8e..f23158ff4a7 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -1186,12 +1186,12 @@ 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 """ @@ -1199,7 +1199,8 @@ def test_ids_numbers(x,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: (type: ) at index 2" ] )