pytest-dev / pytest-cov

Coverage plugin for pytest.
MIT License
1.76k stars 212 forks source link

Line 0 reported as "missing" in coverage report #585

Open psjamesh opened 1 year ago

psjamesh commented 1 year ago

When running pytest with the --cov option, the report contains "missing" entries for a few files with line number 0 reported as the culprit. Not all files exhibit this issue and there is no obvious correlation between the files that do.

We are running tests using tox. See various configuration items below which might help with debugging the issue.


A file which is reported as having a "missing" entry on line 0.

from knox.views import LoginView as KnoxLoginView
from rest_framework.authentication import BasicAuthentication

class LoginView(KnoxLoginView):
    authentication_classes = [BasicAuthentication]

Extract from the coverage report (XML) for this file:

......
        <class name="views.py" filename="src/apps/api_auth/views.py" complexity="0" line-rate="0.8" branch-rate="0">
                <methods/>
                <lines>
                        <line number="0" hits="0"/>
                        <line number="1" hits="1"/>
                        <line number="2" hits="1"/>
                        <line number="5" hits="1"/>
                        <line number="6" hits="1"/>
                </lines>
        </class>
......

Contents of tox.ini:

[tox]
env_list =
    flake8
    black
    py311
requires =
    tox>4
    virtualenv>20.2

[gh-actions]
python =
    3.11: flake8,black,py311

[testenv:flake8]
description = install flake8 in a virtual environment and invoke it from the root of the project
deps = flake8
commands = flake8 .

[testenv:black]
description = install black in a virtual environment and invoke it from the root of the project
deps = black
commands = black --diff --check .

[testenv]
description = install pytest in a virtual environment and invoke it from the root of the project, outputting junit and coverage reports to a reports folder.
deps =
    -r requirements-test.txt
commands = pytest --junitxml=reports/unit-test-{envname}.xml --cov=. --cov-report=xml:reports/pytest-cov-{envname}.xml --cov-fail-under=95 -vvv {posargs}

Contents of .coveragerc:

[run]
omit =
    tests/**/*
    **/migrations/*
    **/.tox/**/*
    **/venv/**/*
    **/.venv/**/*

source = src/

Contents of pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE = deploydocus_project.settings
python_files = tests.py test_*.py *_tests.py
pythonpath = src
testpaths = tests
addopts = -p no:warnings --strict-markers --no-migrations --reuse-db --import-mode importlib

Contents of requirements-test.txt:

uWSGI==2.0.21
dependency-injector==4.41.0
Django==4.1.7
django-allow-cidr==0.6.0
django-environ==0.9.0
django-health-check==3.17.0
django-prometheus==2.2.0
djangorestframework==3.14.0
django-rest-knox==4.2.0
prometheus-client==0.16.0
psycopg2-binary==2.9.5
whitenoise==6.3.0

pytest
pytest-cov==4.0.0
pytest-django
parameterized
assertpy
mutatest
flatten-dict
abompard commented 1 year ago

Probably related to https://github.com/nedbat/coveragepy/issues/1419