Skip to content
Merged
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
16 changes: 10 additions & 6 deletions src/pytest_cov/engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Coverage controllers for use by pytest-cov and nose-cov."""

import argparse
import contextlib
import copy
import functools
Expand All @@ -11,6 +12,7 @@
import warnings
from io import StringIO
from pathlib import Path
from typing import Union

import coverage
from coverage.data import CoverageData
Expand Down Expand Up @@ -67,13 +69,14 @@ def _data_suffix(name):
class CovController:
"""Base class for different plugin implementations."""

def __init__(self, cov_source, cov_report, cov_config, cov_append, cov_branch, config=None, nodeid=None):
def __init__(self, options: argparse.Namespace, config: Union[None, object], nodeid: Union[None, str]):
"""Get some common config used by multiple derived classes."""
self.cov_source = cov_source
self.cov_report = cov_report
self.cov_config = cov_config
self.cov_append = cov_append
self.cov_branch = cov_branch
self.cov_source = options.cov_source
self.cov_report = options.cov_report
self.cov_config = options.cov_config
self.cov_append = options.cov_append
self.cov_branch = options.cov_branch
self.cov_precision = options.cov_precision
self.config = config
self.nodeid = nodeid

Expand Down Expand Up @@ -199,6 +202,7 @@ def summary(self, stream):
'show_missing': ('term-missing' in self.cov_report) or None,
'ignore_errors': True,
'file': stream,
'precision': self.cov_precision,
}
skip_covered = isinstance(self.cov_report, dict) and 'skip-covered' in self.cov_report.values()
options.update({'skip_covered': skip_covered or None})
Expand Down
18 changes: 7 additions & 11 deletions src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING

import coverage
import pytest
Expand All @@ -17,6 +18,9 @@
from . import compat
from . import embed

if TYPE_CHECKING:
from .engine import CovController


def validate_report(arg):
file_choices = ['annotate', 'html', 'xml', 'json', 'lcov']
Expand Down Expand Up @@ -198,7 +202,7 @@ class CovPlugin:
distributed worker.
"""

def __init__(self, options, pluginmanager, start=True, no_cov_should_warn=False):
def __init__(self, options: argparse.Namespace, pluginmanager, start=True, no_cov_should_warn=False):
"""Creates a coverage pytest plugin.

We read the rc file that coverage uses to get the data file
Expand Down Expand Up @@ -240,23 +244,15 @@ def __init__(self, options, pluginmanager, start=True, no_cov_should_warn=False)

# worker is started in pytest hook

def start(self, controller_cls, config=None, nodeid=None):
def start(self, controller_cls: type['CovController'], config=None, nodeid=None):
if config is None:
# fake config option for engine
class Config:
option = self.options

config = Config()

self.cov_controller = controller_cls(
self.options.cov_source,
self.options.cov_report,
self.options.cov_config,
self.options.cov_append,
self.options.cov_branch,
config,
nodeid,
)
self.cov_controller = controller_cls(self.options, config, nodeid)
self.cov_controller.start()
self._started = True
self._start_path = Path.cwd()
Expand Down
33 changes: 33 additions & 0 deletions tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,39 @@ def test_cov_min_float_value_not_reached_cli(testdir):
result.stdout.fnmatch_lines(['FAIL Required test coverage of 88.89% not reached. Total coverage: 88.89%'])


def test_cov_precision(testdir):
script = testdir.makepyfile(SCRIPT)
result = testdir.runpytest('-v', f'--cov={script.dirpath()}', '--cov-report=term-missing', '--cov-precision=6', script)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
'Name Stmts Miss Cover Missing',
'----------------------------------------------------------',
'test_cov_precision.py 9 1 88.888889% 11',
'----------------------------------------------------------',
'TOTAL 9 1 88.888889%',
]
)


def test_cov_precision_from_config(testdir):
script = testdir.makepyfile(SCRIPT)
testdir.tmpdir.join('pyproject.toml').write("""
[tool.coverage.report]
precision = 6""")
result = testdir.runpytest('-v', f'--cov={script.dirpath()}', '--cov-report=term-missing', script)
assert result.ret == 0
result.stdout.fnmatch_lines(
[
'Name Stmts Miss Cover Missing',
'----------------------------------------------------------------------',
'test_cov_precision_from_config.py 9 1 88.888889% 11',
'----------------------------------------------------------------------',
'TOTAL 9 1 88.888889%',
]
)


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

Expand Down