Skip to content

Drop duplicated tests by default #1609 #1756

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

Merged
merged 1 commit into from
Jul 25, 2016
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Punyashloka Biswal
Quentin Pradet
Ralf Schmitt
Raphael Pierzina
Roberto Polli
Romain Dorgueil
Roman Bolshakov
Ronny Pfannschmidt
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ time or change existing behaviors in order to make them less surprising/more use
fixtures and reports them;
+ ``--setup-show``: performs normal test execution and additionally shows
setup and teardown of fixtures;
+ ``--keep-duplicates``: default behavior is now to ignore duplicate paths, you can
retain the old behavior and running multiple times the tests
using the ``--keep-duplicates`` cli argument `#1609`_;

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

* New CLI flag ``--override-ini``/``-o``: overrides values from the ini file.
For example: ``"-o xfail_strict=True"``'.
Expand Down Expand Up @@ -524,6 +527,7 @@ time or change existing behaviors in order to make them less surprising/more use

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

.. _#1609: https://github.com/pytest-dev/pytest/issues/1609
.. _#1422: https://github.com/pytest-dev/pytest/issues/1422
.. _#1379: https://github.com/pytest-dev/pytest/issues/1379
.. _#1366: https://github.com/pytest-dev/pytest/issues/1366
Expand All @@ -549,6 +553,7 @@ time or change existing behaviors in order to make them less surprising/more use
.. _@rabbbit: https://github.com/rabbbit
.. _@hackebrot: https://github.com/hackebrot
.. _@pquentin: https://github.com/pquentin
.. _@ioggstream: https://github.com/ioggstream

2.8.7
=====
Expand Down
1 change: 1 addition & 0 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def __init__(self):
self._conftestpath2mod = {}
self._confcutdir = None
self._noconftest = False
self._duplicatepaths = set()

self.add_hookspecs(_pytest.hookspec)
self.register(self)
Expand Down
23 changes: 22 additions & 1 deletion _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def pytest_addoption(parser):
group.addoption('--noconftest', action="store_true",
dest="noconftest", default=False,
help="Don't load any conftest.py files.")
group.addoption('--keepduplicates', '--keep-duplicates', action="store_true",
dest="keepduplicates", default=False,
help="Skip duplicate tests.")

group = parser.getgroup("debugconfig",
"test session debugging and configuration")
Expand Down Expand Up @@ -154,7 +157,25 @@ def pytest_ignore_collect(path, config):
excludeopt = config.getoption("ignore")
if excludeopt:
ignore_paths.extend([py.path.local(x) for x in excludeopt])
return path in ignore_paths

if path in ignore_paths:
return True

# Skip duplicate paths.
# TODO: is this called when specifying direct filenames
# from command lines, eg.
# py.test test_a.py test_b.py
keepduplicates = config.getoption("keepduplicates")
duplicate_paths = config.pluginmanager._duplicatepaths
if not keepduplicates:
if path in duplicate_paths:
# TODO should we log this?
return True
else:
duplicate_paths.add(path)

return False


class FSHookProxy:
def __init__(self, fspath, pm, remove_mods):
Expand Down
34 changes: 34 additions & 0 deletions doc/en/example/pythoncollection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ you will see that ``pytest`` only collects test-modules, which do not match the
======= 5 passed in 0.02 seconds =======


Keeping duplicate paths specified from command line
----------------------------------------------------

Default behavior of ``pytest`` is to ignore duplicate paths specified from the command line.
Example::

py.test path_a path_a

...
collected 1 item
...

Just collect tests once.

To collect duplicate tests, use the ``--keep-duplicates`` option on the cli.
Example::

py.test --keep-duplicates path_a path_a

...
collected 2 items
...

As the collector just works on directories, if you specify twice a single test file, ``pytest`` will
still collect it twice, no matter if the ``--keep-duplicates`` is not specified.
Example::

py.test test_a.py test_a.py

...
collected 2 items
...


Changing directory recursion
-----------------------------------------------------

Expand Down
37 changes: 37 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,3 +1213,40 @@ def test_syntax_error_with_non_ascii_chars(testdir):
'*SyntaxError*',
'*1 error in*',
])


def test_skip_duplicates_by_default(testdir):
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)

Ignore duplicate directories.
"""
a = testdir.mkdir("a")
fh = a.join("test_a.py")
fh.write(_pytest._code.Source("""
import pytest
def test_real():
pass
"""))
result = testdir.runpytest(a.strpath, a.strpath)
result.stdout.fnmatch_lines([
'*collected 1 item*',
])



def test_keep_duplicates(testdir):
"""Test for issue https://github.com/pytest-dev/pytest/issues/1609 (#1609)

Use --keep-duplicates to collect tests from duplicate directories.
"""
a = testdir.mkdir("a")
fh = a.join("test_a.py")
fh.write(_pytest._code.Source("""
import pytest
def test_real():
pass
"""))
result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath)
result.stdout.fnmatch_lines([
'*collected 2 item*',
])
2 changes: 1 addition & 1 deletion testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ def pytest_generate_tests(metafunc):
"""))
sub1.join("test_in_sub1.py").write("def test_1(): pass")
sub2.join("test_in_sub2.py").write("def test_2(): pass")
result = testdir.runpytest("-v", "-s", sub1, sub2, sub1)
result = testdir.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicoddemus @tomviner I had to change this test because the patch changed the default behavior.

result.assert_outcomes(passed=3)

def test_generate_same_function_names_issue403(self, testdir):
Expand Down