简体   繁体   中英

pytest-cov always show 0 coverage for Tavern test

I have a python3 application running with flask and I am using Tavern and pytest for test cases. The test cases are executed as expected and it shows results correctly. But I always get 0% code coverage.

This is the command line for running tests:

python3 -m pytest --cov=api --cov-report html:reports/Coverage --cov-report term --junitxml=reports/UnitTest.xm
l --html=reports/TestSummary.html --self-contained-html

All the source code are under api module. And the coverage results only shows 100% for the file __init__.py in each module. It doesn't show any coverage data for the real source code.

The dependencies versions for the tests in this project is:

flask==1.1.1
flask_testing==0.7.1
coverage>=4.0.3
nose>=1.3.7
pluggy>=0.3.1
py>=1.4.31
randomize>=0.13
pytest==4.6.4
pytest-cov>=2.6.0
tavern==0.26.4
pytest-html>=1.20.0
pytest-freezegun>=0.3.0
pytest-mock>=1.10.3

The content of .coveragerc in my project is:

[report]
exclude_lines =
    if __name__ == .__main__.:

By default (that is unless you're using an experimental plugin such as https://github.com/taverntesting/tavern-flask ), your application code is running in a different OS process to the process which your tests are running in.

Due to the way that the coverage identifies which lines have been executed, it can only test code in the same process as it is running in. You can read more about how it works here: https://coverage.readthedocs.io/en/v4.5.x/howitworks.html

To achieve what you're after - which I assume is to get the coverage of your web server code, run your flask server with coverage.py , eg coverage run --source=app flask run , then run your tests against the server as you are now (minus turning on coverage). The coverage output will then contain info about which lines have been executed when the tavern tests were running when you kill the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM