From b8c15a0215f4a8b6821f79404496d989bc898126 Mon Sep 17 00:00:00 2001 From: palaviv Date: Tue, 22 Mar 2016 23:58:28 +0200 Subject: [PATCH 1/4] improved idmaker name selection in case of duplicate ids --- _pytest/python.py | 12 +++++++++--- testing/python/metafunc.py | 17 ++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 7b4b0a62230..3020f4db605 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -7,6 +7,7 @@ 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): diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index f0f3dbd4cbd..e7ede1bf831 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -238,9 +238,9 @@ def ids(val): (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 @@ def test_idmaker_with_ids(self): 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 @@ def test_function(a, b): 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"), From 412042d9875916fd70edc2c69ca9d6c89982eecb Mon Sep 17 00:00:00 2001 From: palaviv Date: Wed, 23 Mar 2016 00:01:09 +0200 Subject: [PATCH 2/4] added entry to CHANGELOG --- CHANGELOG.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 159b3fae2c9..f35ee7c5e4a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,7 +26,8 @@ * parametrize ids can accept None as specific test id. The automatically generated id for that argument will be used. -* +* improved idmaker name selection in case of duplicate ids in + parametrize. * From c66aedfa651db5005dce0255db4071a5ec1ba5c1 Mon Sep 17 00:00:00 2001 From: palaviv Date: Wed, 23 Mar 2016 18:47:27 +0200 Subject: [PATCH 3/4] checking first there are duplciates ids before changing to unique names --- _pytest/python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 3020f4db605..e314eab47df 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1153,9 +1153,9 @@ 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)] - duplicates = [testid for testid in ids if ids.count(testid) > 1] - if duplicates: + if len(set(ids)) != len(ids): # The ids are not unique + duplicates = [testid for testid in ids if ids.count(testid) > 1] counters = collections.defaultdict(lambda: 0) for index, testid in enumerate(ids): if testid in duplicates: From 3ffce6ae4a8fc1ac4db13e505cd4d29948ddac1f Mon Sep 17 00:00:00 2001 From: palaviv Date: Wed, 23 Mar 2016 18:53:50 +0200 Subject: [PATCH 4/4] Added thanks and PR links in changlog --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f35ee7c5e4a..c4d5120d894 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,9 +25,11 @@ * parametrize ids can accept None as specific test id. The automatically generated id for that argument will be used. + Thanks `@palaviv`_ for the complete PR (`#1468`_). * improved idmaker name selection in case of duplicate ids in parametrize. + Thanks `@palaviv`_ for the complete PR (`#1474`_). * @@ -35,11 +37,14 @@ .. _@novas0x2a: https://github.com/novas0x2a .. _@kalekundert: https://github.com/kalekundert .. _@tareqalayan: https://github.com/tareqalayan +.. _@palaviv: https://github.com/palaviv .. _#1428: https://github.com/pytest-dev/pytest/pull/1428 .. _#1444: https://github.com/pytest-dev/pytest/pull/1444 .. _#1441: https://github.com/pytest-dev/pytest/pull/1441 .. _#1454: https://github.com/pytest-dev/pytest/pull/1454 +.. _#1468: https://github.com/pytest-dev/pytest/pull/1468 +.. _#1474: https://github.com/pytest-dev/pytest/pull/1474 2.9.2.dev1