Add pytest and coverage configuration

Add `pytest-cov` which automatically measures coverage while tests are
executing and update tox and ci build configuration accordingly.

Add configuration for coverage:
  1. Enable analysing logical branches
  2. Save coverage details as HTML for local development use
  3. Configure the HTML output to include coverage context: on the
     right-hand side of each covered code line there is an expandable
     section which lists every test that ran that line.
This commit is contained in:
Šarūnas Nejus 2024-05-04 01:18:42 +01:00
parent 34a59f98b3
commit 8b4983fe7c
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
5 changed files with 46 additions and 22 deletions

View file

@ -1,10 +0,0 @@
[report]
omit =
*/pyshared/*
*/python?.?/*
*/test/*
exclude_lines =
assert False
raise NotImplementedError
if __name__ == .__main__.:
def __repr__

View file

@ -49,10 +49,11 @@ jobs:
run: |
tox -e py-test
- name: Test latest Python version with tox and get coverage
if: matrix.python-version == '3.x'
- name: Upload code coverage
if: matrix.python-version == '3.7' && matrix.platform == 'ubuntu-latest'
run: |
tox -vv -e py-cov
pip install codecov || true
codecov || true
- name: Test latest Python version with tox and mypy
if: matrix.python-version == '3.x'
@ -63,12 +64,6 @@ jobs:
run: |
tox -vv -e py-mypy
- name: Upload code coverage
if: matrix.python-version == '3.x'
run: |
pip install codecov || true
codecov || true
test-docs:
runs-on: ubuntu-latest

View file

@ -1,3 +1,43 @@
[tool:pytest]
# do not litter the working directory
cache_dir = /tmp/pytest_cache
# slightly more verbose output
console_output_style = count
addopts =
# show all skipped/failed/xfailed tests in the summary except passed
-ra
--strict-config
# record coverage in these two packages
--cov=beets
--cov=beetsplug
# save xml for coverage upload
--cov-report=xml:.reports/coverage.xml
# save html files for local dev use
--cov-report=html:.reports/html
# record coverage across logical branches
--cov-branch
# show which tests cover specific lines in the code (available in HTML)
--cov-context=test
[coverage:run]
data_file = .reports/coverage/data
branch = true
relative_files = true
[coverage:report]
precision = 2
skip_empty = true
show_missing = true
exclude_lines =
pragma: no cover
if TYPE_CHECKING
if typing.TYPE_CHECKING
raise AssertionError
raise NotImplementedError
[coverage:html]
show_contexts = true
[flake8]
min-version = 3.6
accept-encodings = utf-8

View file

@ -103,11 +103,11 @@ setup(
extras_require={
"test": [
"beautifulsoup4",
"coverage",
"flask",
"mock",
"pylast",
"pytest",
"pytest-cov",
"python-mpd2",
"python3-discogs-client>=2.3.15",
"py7zr",

View file

@ -25,8 +25,7 @@ deps =
mypy: {[_mypy]deps}
passenv = INTEGRATION_TEST
commands =
test: python -bb -m pytest -rs {posargs}
cov: coverage run -m pytest -rs {posargs}
test: python -m pytest {posargs}
lint: python -m flake8 {posargs} {[_lint]files}
mypy: mypy -p beets -p beetsplug
mypy: mypy test