From 08f1101455ba293dda388fdb3b61e62fd95a827d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Tue, 29 Oct 2024 21:02:22 +0200 Subject: [PATCH] Add `--cov-precision` option. Close #655. --- src/pytest_cov/plugin.py | 9 ++++++++- tests/test_pytest_cov.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index b2b103f0..916efc8e 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -149,6 +149,12 @@ def pytest_addoption(parser): default=None, help='Enable branch coverage.', ) + group.addoption( + '--cov-precision', + type=int, + default=None, + help='Override the reporting precision.', + ) group.addoption( '--cov-context', action='store', @@ -257,7 +263,8 @@ class Config: cov_config = self.cov_controller.cov.config if self.options.cov_fail_under is None and hasattr(cov_config, 'fail_under'): self.options.cov_fail_under = cov_config.fail_under - self.options.cov_precision = getattr(cov_config, 'precision', 0) + if self.options.cov_precision is None: + self.options.cov_precision = getattr(cov_config, 'precision', 0) def _is_worker(self, session): return getattr(session.config, 'workerinput', None) is not None diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 72f7ab1a..8aa2a339 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -467,6 +467,15 @@ def test_cov_min_float_value_not_reached(testdir): result.stdout.fnmatch_lines(['FAIL Required test coverage of 88.89% not reached. Total coverage: 88.89%']) +def test_cov_min_float_value_not_reached_cli(testdir): + script = testdir.makepyfile(SCRIPT) + result = testdir.runpytest( + '-v', f'--cov={script.dirpath()}', '--cov-report=term-missing', '--cov-precision=3', '--cov-fail-under=88.89', script + ) + assert result.ret == 1 + result.stdout.fnmatch_lines(['FAIL Required test coverage of 88.89% not reached. Total coverage: 88.89%']) + + def test_cov_min_no_report(testdir): script = testdir.makepyfile(SCRIPT)