I have a problem when using coverage.py. I am testing a FastAPI application and cannot get to 100% test coverage due to that. I also cannot share the code, because it is a business logic and therefore I am not allowed. However, I'll try to give the best possible example. The code that I wrote has to test an async endpoint that gets data from Redis and then saves it to both MongoDB and MariaDB (Don't ask why). Here is an example, that comes close to the logic written
@app.post('save/{data_uuid}')
async def save_to_database(
data_uuid: str
db: AsyncSession = Depends(get_db)
mongo_db: Client = Depends(get_mongo_conn)
redis: Redis = Depends(get_redis)
) -> SuccessMessage:
redis_model = redis.get_data_from_redis(data_uuid)
parser = Parser(db) # This is the last line covered by the tests, according to coverage.py
await save_model_to_mariadb(redis_model, db) # NOT RAN (according to Coverage.py)
await save_model_to_mongo(redis_model, parser, mongo_db) # NOT RAN (according to Coverage.py)
return {"message": "All well"} # This code is recognized as ran again
This code (of course, greatly simplified and not with all fn params) is tested using httpx.AsyncTestClient and the test just connects to the databases, mocks them both and mocks the redis method get_data to return some sample JSON string. Everything else is as it is and Coverage.py does not recognize the code as ran, only the part between the two comments is not "hit"
I tried to use a sync client, also tried to put some print statements between the places where the code is not "ran" and the print statements show output, also one of my tests checks whether there is a document saved in the Mongo database with the same uuid and there always is meaning that the code is actually ran. The problem is probably on me, but I gave up on guessing why, so I ask here for help.
I should also mention that all the tests pass successfully.