Skip to content

Commit a02e1c8

Browse files
committed
Add a no_cover marker/fixture. Close #78.
1 parent 509f77f commit a02e1c8

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/pytest_cov/engine.py

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ def __init__(self, cov_source, cov_report, cov_config, cov_append, cov_branch, c
3131
self.failed_slaves = []
3232
self.topdir = os.getcwd()
3333

34+
def pause(self):
35+
self.cov.stop()
36+
self.unset_env()
37+
38+
def resume(self):
39+
self.cov.start()
40+
self.set_env()
41+
3442
def set_env(self):
3543
"""Put info about coverage into the env so that subprocesses can activate coverage."""
3644
if self.cov_source is None:

src/pytest_cov/plugin.py

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55
import argparse
6+
7+
import sys
68
from coverage.misc import CoverageException
79

810
from . import embed
@@ -283,6 +285,20 @@ def pytest_runtest_teardown(self, item):
283285
embed.cleanup(self.cov)
284286
self.cov = None
285287

288+
@compat.hookwrapper
289+
def pytest_runtest_call(self, item):
290+
if item.get_marker("no_cover") or "no_cover" in item.fixturenames:
291+
self.cov_controller.pause()
292+
yield
293+
self.cov_controller.resume()
294+
else:
295+
yield
296+
297+
298+
@pytest.fixture
299+
def no_cover():
300+
pass
301+
286302

287303
@pytest.fixture
288304
def cov(request):
@@ -294,3 +310,7 @@ def cov(request):
294310
if plugin.cov_controller:
295311
return plugin.cov_controller.cov
296312
return None
313+
314+
315+
def pytest_configure(config):
316+
config.addinivalue_line("markers", "no_cover: disable coverage for this test.")

tests/test_pytest_cov.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def test_annotate_output_dir(testdir):
229229
assert result.ret == 0
230230

231231

232-
def test_html_output_dir(testdir,prop):
232+
def test_html_output_dir(testdir, prop):
233233
script = testdir.makepyfile(SCRIPT)
234234

235235
result = testdir.runpytest('-v',
@@ -1015,6 +1015,46 @@ def test_cover_conftest_dist(testdir):
10151015
result.stdout.fnmatch_lines([CONF_RESULT])
10161016

10171017

1018+
def test_no_cover_marker(testdir):
1019+
testdir.makepyfile(mod=MODULE)
1020+
script = testdir.makepyfile('''
1021+
import pytest
1022+
import mod
1023+
import subprocess
1024+
import sys
1025+
1026+
@pytest.mark.no_cover
1027+
def test_basic():
1028+
mod.func()
1029+
subprocess.check_call([sys.executable, '-c', 'from mod import func; func()'])
1030+
''')
1031+
result = testdir.runpytest('-v', '-ra', '--strict',
1032+
'--cov=%s' % script.dirpath(),
1033+
'--cov-report=term-missing',
1034+
script)
1035+
assert result.ret == 0
1036+
result.stdout.fnmatch_lines(['mod* 2 * 1 * 50% * 2'])
1037+
1038+
1039+
def test_no_cover_fixture(testdir):
1040+
testdir.makepyfile(mod=MODULE)
1041+
script = testdir.makepyfile('''
1042+
import mod
1043+
import subprocess
1044+
import sys
1045+
1046+
def test_basic(no_cover):
1047+
mod.func()
1048+
subprocess.check_call([sys.executable, '-c', 'from mod import func; func()'])
1049+
''')
1050+
result = testdir.runpytest('-v', '-ra', '--strict',
1051+
'--cov=%s' % script.dirpath(),
1052+
'--cov-report=term-missing',
1053+
script)
1054+
assert result.ret == 0
1055+
result.stdout.fnmatch_lines(['mod* 2 * 1 * 50% * 2'])
1056+
1057+
10181058
COVERAGERC = '''
10191059
[report]
10201060
# Regexes for lines to exclude from consideration

0 commit comments

Comments
 (0)