Skip to content

Commit 8be9684

Browse files
authored
Optimized renaming of test parameter ids (#6350)
Optimized renaming of test parameter ids
2 parents a176ff7 + d4879c7 commit 8be9684

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ Samuele Pedroni
235235
Sankt Petersbug
236236
Segev Finer
237237
Serhii Mozghovyi
238+
Seth Junot
238239
Simon Gomizelj
239240
Skylar Downes
240241
Srinivas Reddy Thatiparthy

changelog/6350.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimized automatic renaming of test parameter IDs.

src/_pytest/python.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import warnings
88
from collections import Counter
9+
from collections import defaultdict
910
from collections.abc import Sequence
1011
from functools import partial
1112
from textwrap import dedent
@@ -1190,14 +1191,23 @@ def idmaker(argnames, parametersets, idfn=None, ids=None, config=None, item=None
11901191
_idvalset(valindex, parameterset, argnames, idfn, ids, config=config, item=item)
11911192
for valindex, parameterset in enumerate(parametersets)
11921193
]
1193-
if len(set(ids)) != len(ids):
1194-
# The ids are not unique
1195-
duplicates = [testid for testid in ids if ids.count(testid) > 1]
1196-
counters = Counter()
1197-
for index, testid in enumerate(ids):
1198-
if testid in duplicates:
1199-
ids[index] = testid + str(counters[testid])
1200-
counters[testid] += 1
1194+
1195+
# All IDs must be unique!
1196+
unique_ids = set(ids)
1197+
if len(unique_ids) != len(ids):
1198+
1199+
# Record the number of occurrences of each test ID
1200+
test_id_counts = Counter(ids)
1201+
1202+
# Map the test ID to its next suffix
1203+
test_id_suffixes = defaultdict(int)
1204+
1205+
# Suffix non-unique IDs to make them unique
1206+
for index, test_id in enumerate(ids):
1207+
if test_id_counts[test_id] > 1:
1208+
ids[index] = "{}{}".format(test_id, test_id_suffixes[test_id])
1209+
test_id_suffixes[test_id] += 1
1210+
12011211
return ids
12021212

12031213

0 commit comments

Comments
 (0)