Skip to content

Enable coveragerc #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def pytest_addoption(parser):
group.addoption('--cov', action='append', default=[], metavar='path',
dest='cov_source',
help='measure coverage for filesystem path '
'(multi-allowed)')
'(multi-allowed, implies --cov-enabled)')
group.addoption('--cov-enabled', action='store_true', default=False,
dest='cov_enabled',
help='enable coverage measurements')
group.addoption('--cov-report', action='append', default=[],
metavar='type', dest='cov_report',
choices=['term', 'term-missing', 'annotate', 'html',
Expand All @@ -32,17 +35,25 @@ def pytest_addoption(parser):
'default: False')


def postprocess(options):
"""Avoid making a custom action by editing options after parsing."""
if options.cov_source:
options.cov_enabled = True


@pytest.mark.try_last
def pytest_load_initial_conftests(early_config, parser, args):
ns = parser.parse_known_args(args)
if ns.cov_source:
plugin = CovPlugin(ns, early_config.pluginmanager)
options = parser.parse_known_args(args)
postprocess(options)
if options.cov_enabled:
plugin = CovPlugin(options, early_config.pluginmanager)
early_config.pluginmanager.register(plugin, '_cov')


def pytest_configure(config):
"""Activate coverage plugin if appropriate."""
if config.getvalue('cov_source'):
postprocess(config.option)
if config.getvalue('cov_enabled'):
if not config.pluginmanager.hasplugin('_cov'):
plugin = CovPlugin(config.option, config.pluginmanager,
start=False)
Expand Down Expand Up @@ -91,7 +102,7 @@ class Config(object):
config = Config()

self.cov_controller = controller_cls(
self.options.cov_source,
self.options.cov_source or None,
self.options.cov_report or ['term'],
self.options.cov_config,
config,
Expand Down
48 changes: 47 additions & 1 deletion test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def test_foo():
assert False
'''

COVERAGERC_SOURCE = '''\
[run]
source = .
'''

SCRIPT_CHILD = '''
import sys

Expand Down Expand Up @@ -58,7 +63,7 @@ def test_foo(idx):
import coverage

def test_foo(cov):
assert isinstance(cov, coverage.control.coverage)
assert isinstance(cov, coverage.coverage)
'''

SCRIPT_FUNCARG_NOT_ACTIVE = '''
Expand Down Expand Up @@ -107,6 +112,47 @@ def test_central(testdir):
assert result.ret == 0


def test_central_nonspecific(testdir):
script = testdir.makepyfile(SCRIPT)

result = testdir.runpytest('-v',
'--cov-enabled',
'--cov-report=term-missing',
script)

result.stdout.fnmatch_lines([
'*- coverage: platform *, python * -*',
'test_central_nonspecific * %s *' % SCRIPT_RESULT,
'*10 passed*'
])

# multi-module coverage report
assert result.stdout.lines[-3].startswith('TOTAL ')

assert result.ret == 0


def test_central_coveragerc(testdir):
script = testdir.makepyfile(SCRIPT)
testdir.tmpdir.join('.coveragerc').write(COVERAGERC_SOURCE)

result = testdir.runpytest('-v',
'--cov-enabled',
'--cov-report=term-missing',
script)

result.stdout.fnmatch_lines([
'*- coverage: platform *, python * -*',
'test_central_coveragerc * %s *' % SCRIPT_RESULT,
'*10 passed*',
])

# single-module coverage report
assert result.stdout.lines[-3].startswith('test_central_coveragerc ')

assert result.ret == 0


def test_no_cov_on_fail(testdir):
script = testdir.makepyfile(SCRIPT_FAIL)

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ usedevelop = True
setenv =
PYTHONHASHSEED = random
deps =
#FIXME: delete next line after merging https://github.com/schlamar/cov-core/pull/5
git+git://github.com/bukzor/cov-core.git
pytest
pytest-xdist
virtualenv
Expand Down