Skip to content

Commit 83fabcc

Browse files
committed
feature: use --skip-duplicates to ignore duplicate tests and take into account only the first one.
1 parent ae07985 commit 83fabcc

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Punyashloka Biswal
9494
Quentin Pradet
9595
Ralf Schmitt
9696
Raphael Pierzina
97+
Roberto Polli
9798
Roman Bolshakov
9899
Ronny Pfannschmidt
99100
Ross Lawley

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ time or change existing behaviors in order to make them less surprising/more use
116116
fixtures and reports them;
117117
+ ``--setup-show``: performs normal test execution and additionally shows
118118
setup and teardown of fixtures;
119+
+ ``--skip-duplicates``: ignore duplicate tests, taking into account only
120+
the first one `#1609`_;
119121

120-
Thanks `@d6e`_, `@kvas-it`_, `@sallner`_ and `@omarkohl`_ for the PRs.
122+
Thanks `@d6e`_, `@kvas-it`_, `@sallner`_, `@ioggstream`_ and `@omarkohl`_ for the PRs.
121123

122124
* New cli flag ``--override-ini``/``-o``: overrides values from the ini file.
123125
For example: ``"-o xfail_strict=True"``'.
@@ -514,6 +516,7 @@ time or change existing behaviors in order to make them less surprising/more use
514516

515517
.. _`traceback style docs`: https://pytest.org/latest/usage.html#modifying-python-traceback-printing
516518

519+
.. _#1609: https://github.com/pytest-dev/pytest/issues/1609
517520
.. _#1422: https://github.com/pytest-dev/pytest/issues/1422
518521
.. _#1379: https://github.com/pytest-dev/pytest/issues/1379
519522
.. _#1366: https://github.com/pytest-dev/pytest/issues/1366
@@ -539,6 +542,7 @@ time or change existing behaviors in order to make them less surprising/more use
539542
.. _@rabbbit: https://github.com/rabbbit
540543
.. _@hackebrot: https://github.com/hackebrot
541544
.. _@pquentin: https://github.com/pquentin
545+
.. _@ioggstream: https://github.com/ioggstream
542546

543547
2.8.7
544548
=====

_pytest/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def __init__(self):
155155
self._conftestpath2mod = {}
156156
self._confcutdir = None
157157
self._noconftest = False
158+
self._skipduplicates = set()
158159

159160
self.add_hookspecs(_pytest.hookspec)
160161
self.register(self)

_pytest/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def pytest_addoption(parser):
6363
group.addoption('--noconftest', action="store_true",
6464
dest="noconftest", default=False,
6565
help="Don't load any conftest.py files.")
66+
group.addoption('--skipduplicates', '--skip-duplicates', action="store_true",
67+
dest="skipduplicates", default=False,
68+
help="Skip duplicate tests.")
6669

6770
group = parser.getgroup("debugconfig",
6871
"test session debugging and configuration")
@@ -154,7 +157,22 @@ def pytest_ignore_collect(path, config):
154157
excludeopt = config.getoption("ignore")
155158
if excludeopt:
156159
ignore_paths.extend([py.path.local(x) for x in excludeopt])
157-
return path in ignore_paths
160+
161+
if path in ignore_paths:
162+
return True
163+
164+
# Skip duplicate paths.
165+
skipduplicates = config.getoption("skipduplicates")
166+
duplicate_paths = config.pluginmanager._skipduplicates
167+
if skipduplicates:
168+
if path.purebasename in duplicate_paths:
169+
# TODO should we log this?
170+
return True
171+
else:
172+
duplicate_paths.add(path.purebasename)
173+
174+
return False
175+
158176

159177
class FSHookProxy:
160178
def __init__(self, fspath, pm, remove_mods):

testing/python/collect.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,3 +1213,30 @@ def test_syntax_error_with_non_ascii_chars(testdir):
12131213
'*SyntaxError*',
12141214
'*1 error in*',
12151215
])
1216+
1217+
1218+
def test_skip_duplicates(testdir):
1219+
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)
1220+
1221+
Ignore duplicate test filenames.
1222+
"""
1223+
a = testdir.mkdir("a")
1224+
b = testdir.mkdir("b")
1225+
a.join("test_a.py").write(_pytest._code.Source("""
1226+
import pytest
1227+
def test_real():
1228+
pass
1229+
"""))
1230+
b.join("test_a.py").write(_pytest._code.Source("""
1231+
import pytest
1232+
def test_real():
1233+
pass
1234+
"""))
1235+
1236+
result = testdir.runpytest('--skip-duplicates')
1237+
result.stdout.fnmatch_lines([
1238+
'*collected 1 item*',
1239+
])
1240+
1241+
1242+

0 commit comments

Comments
 (0)