Skip to content

Commit fed89ef

Browse files
committed
Merge pull request #1474 from palaviv/improve-idmaker-duplicate-names
Improve idmaker name selection in case of duplicate ids in parametrize
2 parents da10451 + 3ffce6a commit fed89ef

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,26 @@
2525

2626
* parametrize ids can accept None as specific test id. The
2727
automatically generated id for that argument will be used.
28+
Thanks `@palaviv`_ for the complete PR (`#1468`_).
2829

29-
*
30+
* improved idmaker name selection in case of duplicate ids in
31+
parametrize.
32+
Thanks `@palaviv`_ for the complete PR (`#1474`_).
3033

3134
*
3235

3336
.. _@milliams: https://github.com/milliams
3437
.. _@novas0x2a: https://github.com/novas0x2a
3538
.. _@kalekundert: https://github.com/kalekundert
3639
.. _@tareqalayan: https://github.com/tareqalayan
40+
.. _@palaviv: https://github.com/palaviv
3741

3842
.. _#1428: https://github.com/pytest-dev/pytest/pull/1428
3943
.. _#1444: https://github.com/pytest-dev/pytest/pull/1444
4044
.. _#1441: https://github.com/pytest-dev/pytest/pull/1441
4145
.. _#1454: https://github.com/pytest-dev/pytest/pull/1454
46+
.. _#1468: https://github.com/pytest-dev/pytest/pull/1468
47+
.. _#1474: https://github.com/pytest-dev/pytest/pull/1474
4248

4349

4450
2.9.2.dev1

_pytest/python.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import types
88
import sys
99
import math
10+
import collections
1011

1112
import py
1213
import pytest
@@ -1152,9 +1153,14 @@ def _idvalset(idx, valset, argnames, idfn, ids):
11521153
def idmaker(argnames, argvalues, idfn=None, ids=None):
11531154
ids = [_idvalset(valindex, valset, argnames, idfn, ids)
11541155
for valindex, valset in enumerate(argvalues)]
1155-
if len(set(ids)) < len(ids):
1156-
# user may have provided a bad idfn which means the ids are not unique
1157-
ids = [str(i) + testid for i, testid in enumerate(ids)]
1156+
if len(set(ids)) != len(ids):
1157+
# The ids are not unique
1158+
duplicates = [testid for testid in ids if ids.count(testid) > 1]
1159+
counters = collections.defaultdict(lambda: 0)
1160+
for index, testid in enumerate(ids):
1161+
if testid in duplicates:
1162+
ids[index] = testid + str(counters[testid])
1163+
counters[testid] += 1
11581164
return ids
11591165

11601166
def showfixtures(config):

testing/python/metafunc.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ def ids(val):
238238
(20, KeyError()),
239239
("three", [1, 2, 3]),
240240
], idfn=ids)
241-
assert result == ["0a-a",
242-
"1a-a",
243-
"2a-a",
241+
assert result == ["a-a0",
242+
"a-a1",
243+
"a-a2",
244244
]
245245

246246
@pytest.mark.issue351
@@ -267,10 +267,9 @@ def test_idmaker_with_ids(self):
267267

268268
def test_idmaker_with_ids_unique_names(self):
269269
from _pytest.python import idmaker
270-
result = idmaker(("a", "b"), [(1, 2),
271-
(3, 4)],
272-
ids=["a", "a"])
273-
assert result == ["0a", "1a"]
270+
result = idmaker(("a"), [1,2,3,4,5],
271+
ids=["a", "a", "b", "c", "b"])
272+
assert result == ["a0", "a1", "b0", "c", "b1"]
274273

275274
def test_addcall_and_parametrize(self):
276275
def func(x, y): pass
@@ -834,8 +833,8 @@ def test_function(a, b):
834833
result = testdir.runpytest("-v")
835834
assert result.ret == 1
836835
result.stdout.fnmatch_lines_random([
837-
"*test_function*0a*PASSED",
838-
"*test_function*1a*FAILED"
836+
"*test_function*a0*PASSED",
837+
"*test_function*a1*FAILED"
839838
])
840839

841840
@pytest.mark.parametrize(("scope", "length"),

0 commit comments

Comments
 (0)