From d169d77a3fdcd00e2e8334c18bda518eac4aeb06 Mon Sep 17 00:00:00 2001 From: Roberto Polli Date: Sat, 23 Jul 2016 16:54:40 +0200 Subject: [PATCH] feature: default behavior now is to ignore duplicate paths specified from the command line. Use --keep-duplicates to retain duplicate paths. --- AUTHORS | 1 + CHANGELOG.rst | 6 +++++- _pytest/config.py | 1 + _pytest/main.py | 23 +++++++++++++++++++++- testing/python/collect.py | 40 ++++++++++++++++++++++++++++++++++++++ testing/python/metafunc.py | 2 +- 6 files changed, 70 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 1f842780f54..44638b4d521 100644 --- a/AUTHORS +++ b/AUTHORS @@ -94,6 +94,7 @@ Punyashloka Biswal Quentin Pradet Ralf Schmitt Raphael Pierzina +Roberto Polli Roman Bolshakov Ronny Pfannschmidt Ross Lawley diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 522d63aa275..3bf0e301639 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -116,8 +116,10 @@ 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; + + ``--skip-duplicates``: ignore duplicate tests, taking into account only + the first one `#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"``'. @@ -514,6 +516,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 @@ -539,6 +542,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 ===== diff --git a/_pytest/config.py b/_pytest/config.py index 46c53ce68d5..f179e56613b 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -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) diff --git a/_pytest/main.py b/_pytest/main.py index 6119fcb0328..f378f22dc64 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -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") @@ -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): diff --git a/testing/python/collect.py b/testing/python/collect.py index c4b9259bddb..0ffd7f52df9 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -1213,3 +1213,43 @@ 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*', + ]) + + + diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index ac8e512d489..5338fafc0b8 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -939,7 +939,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) result.assert_outcomes(passed=3) def test_generate_same_function_names_issue403(self, testdir):