From fb2c67ea0832ef75259db9209908508eab1e0a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danilo=20=C5=A0egan?= Date: Thu, 25 Mar 2021 15:53:16 +0100 Subject: [PATCH] Implement --cov-reset option that resets accumulated --cov directories to an empty list. --- docs/config.rst | 2 ++ src/pytest_cov/plugin.py | 2 ++ tests/test_pytest_cov.py | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/docs/config.rst b/docs/config.rst index 0dfb17a0..6aa0e411 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -66,6 +66,8 @@ The complete list of command line options is: False --no-cov Disable coverage report completely (useful for debuggers). Default: False + --cov-reset Reset cov sources accumulated in options so far. + Mostly useful for scripts and configuration files. --cov-fail-under=MIN Fail if the total coverage is less than MIN. --cov-append Do not delete coverage but append to current. Default: False diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index b875f409..3662cfbb 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -70,6 +70,8 @@ def pytest_addoption(parser): nargs='?', const=True, dest='cov_source', help='Path or package name to measure during execution (multi-allowed). ' 'Use --cov= to not do any source filtering and record everything.') + group.addoption('--cov-reset', action='store_const', const=[], dest='cov_source', + help='Reset cov sources accumulated in options so far. ') group.addoption('--cov-report', action=StoreReport, default={}, metavar='TYPE', type=validate_report, help='Type of report to generate: term, term-missing, ' diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 0d1b5a23..2647edfd 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -1999,6 +1999,33 @@ def test_double_cov2(testdir): assert result.ret == 0 +def test_cov_reset(testdir): + script = testdir.makepyfile(SCRIPT_SIMPLE) + result = testdir.runpytest('-v', + '--assert=plain', + '--cov=%s' % script.dirpath(), + '--cov-reset', + script) + + assert 'coverage: platform' not in result.stdout.str() + + +def test_cov_reset_then_set(testdir): + script = testdir.makepyfile(SCRIPT_SIMPLE) + result = testdir.runpytest('-v', + '--assert=plain', + '--cov=%s' % script.dirpath(), + '--cov-reset', + '--cov=%s' % script.dirpath(), + script) + + result.stdout.fnmatch_lines([ + '*- coverage: platform *, python * -*', + 'test_cov_reset_then_set* %s*' % SCRIPT_SIMPLE_RESULT, + '*1 passed*' + ]) + + @pytest.mark.skipif('sys.platform == "win32" and platform.python_implementation() == "PyPy"') def test_cov_and_no_cov(testdir): script = testdir.makepyfile(SCRIPT_SIMPLE)