Why not getting 100% coverage on FastAPI with pytest-cov using starlettes TestClient
Asked Answered
G

0

6

I simply do not understand the output from pytest-cov

enter image description here

My test module is the following

from starlette.testclient import TestClient


def test_ping(test_app: TestClient):
    response = test_app.get("/ping")
    assert response.status_code == 200
    assert response.json() == {"environment": "dev", "ping": "pong!", "testing": True}

I.e. I do not understand the 1 partial and the 1 missing. The full CLI report:

❯ docker-compose exec web coverage report -m                          
Name                     Stmts   Miss Branch BrPart  Cover   Missing
--------------------------------------------------------------------
app/__init__.py              0      0      0      0   100%
app/api/__init__.py          0      0      0      0   100%
app/api/crud.py             14      0      2      0   100%
app/api/ping.py              7      1      2      1    78%   267, 10->9
app/api/summaries.py        20      0      8      5    82%   12->11, 12->19, 19->18, 19->24, 24->23
app/config.py               12      0      2      1    93%   16->15
app/db_init.py               8      1      0      0    88%   268
app/main.py                 19      2      4      3    78%   270-274, 25->24, 25->31, 31->30
app/models/__init__.py       0      0      0      0   100%
app/models/pydantic.py       5      0      0      0   100%
app/models/tortoise.py       9      1      0      0    89%   11
db_generate_schema.py       13     13      2      0     0%   1-274
--------------------------------------------------------------------
TOTAL                      107     18     20     10    76%

I'd like to understand the missing 267, 10->9 first of all - I do not even have more than 15 lines in that file. That would probably explain the remaining issues as well.

Grannie answered 8/6, 2020 at 15:29 Comment(3)
I guess partial coverage is because you never test it with a non-default settings value.Pettway
the settings you mean? You are not actually supposed to change that in FastAPI framework actually - guess that's something to notify FastAPI about. But the missing is real strange, isn't?Grannie
I am also seeing incorrect missing lines when testing a fastAPI application. In my case the lines are real lines of code but my tests are executing them. They are all in routers and are all branches that raise exceptions (e.g. to raise 404 when a record is not found). I have no idea how to improve the accuracy of the coverage report.Frau

© 2022 - 2024 — McMap. All rights reserved.