Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get which statements are missed in python test coverage

I am new to python, I have written test cases for my class , I am using python -m pytest --cov=azuread_api to get code coverage.

I am getting coverage on the console as enter image description here

How do I get which lines are missed by test for e.g in aadadapter.py file

Thanks,

like image 311
Planet-Zoom Avatar asked Feb 16 '18 10:02

Planet-Zoom


People also ask

What is Pytest coverage report?

pytest --cov-report= --cov=myproj tests/ This mode can be especially useful on continuous integration servers, where a coverage file is needed for subsequent processing, but no local report needs to be viewed. For example, tests run on GitHub Actions could produce a . coverage file for use with Coveralls.

What is statement coverage Python?

Introduction. The coverage.py Python module provides statement coverage for Python. It accumulates coverage data over many runs; generates coverage reports; and annotates Python source showing which statements have been covered.

What is line or statement coverage in Python?

It is formally called line or statement coverage. This one is used by default in the most complete python code coverage lib – coverage.py. Assuming we have code in func.py and tests in test_func.py files, we can see coverage.py (+pytest-cov plugin) reports 100% code coverage:

How to test coverage in Python 1?

A Quick Intro to Test Coverage in Python 1 Installation 2 Using Coverage. The idea is to use it along with your test runner. It is fairly simple through command line. We can go over some examples. 3 Quick Guide: Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts of…

How to get pytest coverage report on command line?

If we run pytest as in the posts before, it reports that our test passes. To get a coverage report on the command line, we need to run pytest with the --cov option:

Why does pytest fail under 100 tests?

When it’s not met (code coverage less than expected) we fail the build, e.g. pytest –cov=src/ –cov-fail-under=100 tests/. In this example, the command will fail if our coverage is lower than 100%.


2 Answers

If you check the documentation for reporting in pytest-cov, you can see how to manipulate the report and generate extra versions.

For example, adding the option --cov-report term-missing you'll get the missing lines printed in the terminal.

A more user friendly option, would be to generate an html report by usign the --cov-report html option. Then you can navigate to the generated folder (htmlcov by default) and open the index.html with your browser and navigate your source code where the missing lines are highlighted.

like image 178
Ignacio Vergara Kausel Avatar answered Oct 01 '22 02:10

Ignacio Vergara Kausel


In addition to the answer from Ignacio, one can also set show_missing = true in .coveragerc, as pytest-cov reads that config file as well.

like image 20
pepoluan Avatar answered Oct 01 '22 01:10

pepoluan