Skip to content

Commit

Permalink
Add a no_cover marker/fixture. Close #78.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Nov 24, 2017
1 parent 509f77f commit a02e1c8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/pytest_cov/engine.py
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions src/pytest_cov/plugin.py
Expand Up @@ -3,6 +3,8 @@

import pytest
import argparse

import sys
from coverage.misc import CoverageException

from . import embed
Expand Down Expand Up @@ -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):
Expand All @@ -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.")
42 changes: 41 additions & 1 deletion tests/test_pytest_cov.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a02e1c8

Please sign in to comment.