Closed
Description
Summary
When running with --cov-context=test
tests marked with @pytest.mark.no_cover()
fail with coverage.exceptions.CoverageException: Cannot switch context, coverage is not started
.
Expected vs actual result
Reproducer
uv run --with=pytest-cov==6.1.0 pytest --cov=test_no_cover --cov-context=test ./test_no_cover.py
import pytest
def test_with_coverage():
do_print("Hi!")
@pytest.mark.no_cover()
def test_without_coverage():
do_print("Hello!")
def do_print(message):
print(message)
Full output:
========================================================================= test session starts ==========================================================================
platform linux -- Python 3.13.2, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/wsl
plugins: cov-6.1.0
collected 2 items
test_no_cover.py .F [100%]
=============================================================================== FAILURES ===============================================================================
________________________________________________________________________ test_without_coverage _________________________________________________________________________
self = <pytest_cov.plugin.TestContextPlugin object at 0x7f13be219a90>, item = <Function test_without_coverage>
def pytest_runtest_call(self, item):
> self.switch_context(item, 'run')
.cache/uv/archive-v0/cNa-as4OHY7hEnWef4tpk/lib/python3.13/site-packages/pytest_cov/plugin.py:421:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.cache/uv/archive-v0/cNa-as4OHY7hEnWef4tpk/lib/python3.13/site-packages/pytest_cov/plugin.py:425: in switch_context
self.cov.switch_context(context)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <coverage.control.Coverage object at 0x7f13be2e57f0>, new_context = 'test_no_cover.py::test_without_coverage|run'
def switch_context(self, new_context: str) -> None:
"""Switch to a new dynamic context.
`new_context` is a string to use as the :ref:`dynamic context
<dynamic_contexts>` label for collected data. If a :ref:`static
context <static_contexts>` is in use, the static and dynamic context
labels will be joined together with a pipe character.
Coverage collection must be started already.
.. versionadded:: 5.0
"""
if not self._started: # pragma: part started
> raise CoverageException("Cannot switch context, coverage is not started")
E coverage.exceptions.CoverageException: Cannot switch context, coverage is not started
.cache/uv/archive-v0/cNa-as4OHY7hEnWef4tpk/lib/python3.13/site-packages/coverage/control.py:755: CoverageException
============================================================================ tests coverage ============================================================================
___________________________________________________________ coverage: platform linux, python 3.13.2-final-0 ____________________________________________________________
Name Stmts Miss Cover
--------------------------------------
test_no_cover.py 8 1 88%
--------------------------------------
TOTAL 8 1 88%
======================================================================= short test summary info ========================================================================
FAILED test_no_cover.py::test_without_coverage - coverage.exceptions.CoverageException: Cannot switch context, coverage is not started
===================================================================== 1 failed, 1 passed in 0.13s ======================================================================
Versions
Python 3.13.2
Package Version
---------- -------
coverage 7.8.0
iniconfig 2.1.0
packaging 24.2
pip 24.3.1
pluggy 1.5.0
pytest 8.3.5
pytest-cov 6.1.0