improved idmaker name selection in case of duplicate ids

This commit is contained in:
palaviv 2016-03-22 23:58:28 +02:00
parent da1045151f
commit b8c15a0215
2 changed files with 17 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import re
import types
import sys
import math
import collections
import py
import pytest
@ -1152,9 +1153,14 @@ def _idvalset(idx, valset, argnames, idfn, ids):
def idmaker(argnames, argvalues, idfn=None, ids=None):
ids = [_idvalset(valindex, valset, argnames, idfn, ids)
for valindex, valset in enumerate(argvalues)]
if len(set(ids)) < len(ids):
# user may have provided a bad idfn which means the ids are not unique
ids = [str(i) + testid for i, testid in enumerate(ids)]
duplicates = [testid for testid in ids if ids.count(testid) > 1]
if duplicates:
# The ids are not unique
counters = collections.defaultdict(lambda: 0)
for index, testid in enumerate(ids):
if testid in duplicates:
ids[index] = testid + str(counters[testid])
counters[testid] += 1
return ids
def showfixtures(config):

View File

@ -238,9 +238,9 @@ class TestMetafunc:
(20, KeyError()),
("three", [1, 2, 3]),
], idfn=ids)
assert result == ["0a-a",
"1a-a",
"2a-a",
assert result == ["a-a0",
"a-a1",
"a-a2",
]
@pytest.mark.issue351
@ -267,10 +267,9 @@ class TestMetafunc:
def test_idmaker_with_ids_unique_names(self):
from _pytest.python import idmaker
result = idmaker(("a", "b"), [(1, 2),
(3, 4)],
ids=["a", "a"])
assert result == ["0a", "1a"]
result = idmaker(("a"), [1,2,3,4,5],
ids=["a", "a", "b", "c", "b"])
assert result == ["a0", "a1", "b0", "c", "b1"]
def test_addcall_and_parametrize(self):
def func(x, y): pass
@ -834,8 +833,8 @@ class TestMetafuncFunctional:
result = testdir.runpytest("-v")
assert result.ret == 1
result.stdout.fnmatch_lines_random([
"*test_function*0a*PASSED",
"*test_function*1a*FAILED"
"*test_function*a0*PASSED",
"*test_function*a1*FAILED"
])
@pytest.mark.parametrize(("scope", "length"),