Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve idmaker name selection in case of duplicate ids in parametrize #1474

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@

* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a "thanks palaviv" and mention #1474. You might want to do the same for the item just above this one. 😉

parametrize.
Thanks `@palaviv`_ for the complete PR (`#1474`_).

*

.. _@milliams: https://github.com/milliams
.. _@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
Expand Down
12 changes: 9 additions & 3 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import types
import sys
import math
import collections

import py
import pytest
Expand Down Expand Up @@ -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)]
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:
ids[index] = testid + str(counters[testid])
counters[testid] += 1
return ids

def showfixtures(config):
Expand Down
17 changes: 8 additions & 9 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"),
Expand Down