From a02e1c83a273517dd82ef638cbb7717e5f52bbc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Fri, 24 Nov 2017 10:59:30 +0200 Subject: [PATCH] Add a no_cover marker/fixture. Close #78. --- src/pytest_cov/engine.py | 8 ++++++++ src/pytest_cov/plugin.py | 20 +++++++++++++++++++ tests/test_pytest_cov.py | 42 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/pytest_cov/engine.py b/src/pytest_cov/engine.py index 824c0618..8f22dcc8 100644 --- a/src/pytest_cov/engine.py +++ b/src/pytest_cov/engine.py @@ -31,6 +31,14 @@ def __init__(self, cov_source, cov_report, cov_config, cov_append, cov_branch, c self.failed_slaves = [] self.topdir = os.getcwd() + def pause(self): + self.cov.stop() + self.unset_env() + + def resume(self): + self.cov.start() + self.set_env() + def set_env(self): """Put info about coverage into the env so that subprocesses can activate coverage.""" if self.cov_source is None: diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index e03ec2a0..60f5e2fb 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -3,6 +3,8 @@ import pytest import argparse + +import sys from coverage.misc import CoverageException from . import embed @@ -283,6 +285,20 @@ def pytest_runtest_teardown(self, item): embed.cleanup(self.cov) self.cov = None + @compat.hookwrapper + def pytest_runtest_call(self, item): + if item.get_marker("no_cover") or "no_cover" in item.fixturenames: + self.cov_controller.pause() + yield + self.cov_controller.resume() + else: + yield + + +@pytest.fixture +def no_cover(): + pass + @pytest.fixture def cov(request): @@ -294,3 +310,7 @@ def cov(request): if plugin.cov_controller: return plugin.cov_controller.cov return None + + +def pytest_configure(config): + config.addinivalue_line("markers", "no_cover: disable coverage for this test.") diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index e4057ead..7f5cb627 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -229,7 +229,7 @@ def test_annotate_output_dir(testdir): assert result.ret == 0 -def test_html_output_dir(testdir,prop): +def test_html_output_dir(testdir, prop): script = testdir.makepyfile(SCRIPT) result = testdir.runpytest('-v', @@ -1015,6 +1015,46 @@ def test_cover_conftest_dist(testdir): result.stdout.fnmatch_lines([CONF_RESULT]) +def test_no_cover_marker(testdir): + testdir.makepyfile(mod=MODULE) + script = testdir.makepyfile(''' +import pytest +import mod +import subprocess +import sys + +@pytest.mark.no_cover +def test_basic(): + mod.func() + subprocess.check_call([sys.executable, '-c', 'from mod import func; func()']) +''') + result = testdir.runpytest('-v', '-ra', '--strict', + '--cov=%s' % script.dirpath(), + '--cov-report=term-missing', + script) + assert result.ret == 0 + result.stdout.fnmatch_lines(['mod* 2 * 1 * 50% * 2']) + + +def test_no_cover_fixture(testdir): + testdir.makepyfile(mod=MODULE) + script = testdir.makepyfile(''' +import mod +import subprocess +import sys + +def test_basic(no_cover): + mod.func() + subprocess.check_call([sys.executable, '-c', 'from mod import func; func()']) +''') + result = testdir.runpytest('-v', '-ra', '--strict', + '--cov=%s' % script.dirpath(), + '--cov-report=term-missing', + script) + assert result.ret == 0 + result.stdout.fnmatch_lines(['mod* 2 * 1 * 50% * 2']) + + COVERAGERC = ''' [report] # Regexes for lines to exclude from consideration