nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
2.99k stars 429 forks source link

Coverage reporting missing lines even though such lines are executed #1078

Closed qlands closed 2 years ago

qlands commented 3 years ago

I am testing an application and try to determine why Coverage reports missing a line that is executed. I insolated a test for this report.

In a file called assistants.py I have the following code:

def get_project_from_assistant(request, user, requested_project, assistant):
    # Get all the assistants the user has with that name across projects
    num_assistants = (
        request.dbsession.query(Userproject, Collaborator)
        .filter(Userproject.project_id == Collaborator.project_id)
        .filter(Userproject.user_id == user)
        .filter(Userproject.access_type == 1)
        .filter(Collaborator.coll_id == assistant)
        .count()
    )
    print("******************************777")
    print(__file__)
    print("******************************777")
    if num_assistants > 0:
        if num_assistants == 1:   # Line 302
            print("******************************2222")
            print("I am in line 304")
            print("******************************222")
            ....

I run the test with "pytest -s --cov=formshare" and my .coveragerc file is as follows:

# .coveragerc to control coverage.py
[run]
branch = True
source = formshare

[paths]
source =
    formshare/
    */site-packages/

[report]
show_missing = True
exclude_lines =
    pragma: no cover
    except

The output shows that the function get_project_from_assistant is executed many times because there are multiple calls across the source code but eventually line 304 onwards get executed:

******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************777
/home/cquiros/data/projects2017/personal/software/FormShare/formshare/processes/db/assistant.py
******************************777
******************************2222
I am in line 304
******************************222

However, this is the coverage information just for that file (I removed the rest of the lines for clarity):

Name                                                    Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------------------------------------------------------
formshare/processes/db/assistant.py                       152    101     60      3    26%   40-91, 95-136, 140-161, 175-181, 185-190, 212->213, 213, 247-254, 269-277, 302->326, 316->320, 320-326, 333-401, 405-416

It says that it misses from line 302->326 but is definitely passing by 304 because of the print()

Might this problem be related to passing many times through the same file?

Using: pytest version 5.4.2 coverage version 5.1

AndrewHoos commented 3 years ago

302->326 means that the coverage is missing the branch where you jump from 302 to 326 (e.g. you don't take the if block).

302-326 (note no >) would mean those lines are not covered.