diff --git a/pytest_cov.py b/pytest_cov.py index 476c784a..b4c504f1 100644 --- a/pytest_cov.py +++ b/pytest_cov.py @@ -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', @@ -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) @@ -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, diff --git a/test_pytest_cov.py b/test_pytest_cov.py index a1e61edd..177601c6 100644 --- a/test_pytest_cov.py +++ b/test_pytest_cov.py @@ -24,6 +24,11 @@ def test_foo(): assert False ''' +COVERAGERC_SOURCE = '''\ +[run] +source = . +''' + SCRIPT_CHILD = ''' import sys @@ -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 = ''' @@ -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) diff --git a/tox.ini b/tox.ini index 6a2af645..fe0acfa6 100644 --- a/tox.ini +++ b/tox.ini @@ -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