pythonpytestcoverage.pypytest-cov

CoverageWarning: Unrecognized option '[tool.coverage.run] exclude_also=' in config file pyproject.toml


When I run pytest:

uv run pytest . -k "test_my_project" --cov=development/my_project

I receive the following report, with 1 warning:

========================= test session starts ====================================
platform win32 -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
Using --random-order-bucket=global
Using --random-order-seed=449769

rootdir: D:\dev-python
configfile: pyproject.toml
plugins: cov-6.2.1, find-dependencies-0.6.0, mock-3.14.1, random-order-1.2.0,
syrupy-4.9.1
collected 4 items / 2 skipped / 2 selected          

development\my_project\test_my_project.py ..                              [100%]

========================= warnings summary =======================================
.venv\Lib\site-packages\coverage\config.py:340
  D:\dev-python\.venv\Lib\site-packages\coverage\config.py:340: CoverageWarning: 
  Unrecognized option '[tool.coverage.run] exclude_also=' 
  in config file pyproject.toml

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================= tests coverage ==========================================
__________________ coverage: platform win32, python 3.13.5-final-0 _______________

Name    Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------
TOTAL      92      0      8      0   100%

2 files skipped due to complete coverage.
========================= 2 passed, 2 skipped, 1 warning in 5.36s ================

Here is an excerpt from my pyproject.toml:

[tool.coverage.run]
branch = true
disable_warnings = ["module-not-imported"]
omit = [
    "*/__main__.py",
    "*/setup.py",
]
exclude_also = [
    "\\# pragma: no cover\b",
    "^\\s*raise AssertionError\b",
    "^\\s*raise NotImplementedError\b",
    "^\\s*return NotImplemented\b",
    "^\\s*raise$",
    "^\\s*if (False|TYPE_CHECKING):",
    "if __name__ == '__main__':$",
]

[tool.coverage.report]
show_missing = true
skip_covered = true
sort="Cover"

From this question, I understand that exclude_also was introduced in version 7.2.0 of Coverage.py and that pytest-cov is just a wrapper around it.

When I run uv pip list, I get the following output:

Package                  Version
------------------------ ---------------
...
coverage                 7.10.3
...
pytest                   8.4.1
pytest-cov               6.2.1
pytest-find-dependencies 0.6.0
pytest-mock              3.14.1
pytest-random-order      1.2.0
...

Since my setup seems up-to-date, why am I receiving a warning anyway?


Solution

  • Coverage is warning because exclude_also is a report option, but you put it under [tool.coverage.run]. Move it to [tool.coverage.report] and the warning goes away:

    [tool.coverage.run]
    branch = true
    disable_warnings = ["module-not-imported"]
    omit = ["*/__main__.py","*/setup.py"]
    
    [tool.coverage.report]
    show_missing = true
    skip_covered = true
    sort = "Cover"
    exclude_also = [
      '\# pragma: no cover\b',
      '^\s*raise AssertionError\b',
      '^\s*raise NotImplementedError\b',
      '^\s*return NotImplemented\b',
      '^\s*raise$',
      '^\s*if (False|TYPE_CHECKING):',
      "if __name__ == '__main__':$",
    ]
    

    make sure pytest is running the same coverage you think it is: python -c "import coverage; print(coverage.__version__, coverage.__file__)"