0
0
PyTesttesting~5 mins

Coverage report formats (terminal, HTML, XML) in PyTest

Choose your learning style9 modes available
Introduction

Coverage reports show which parts of your code were tested. They help you find untested code so you can improve your tests.

You want to see test coverage results quickly in the terminal after running tests.
You need a detailed, easy-to-read report to share with your team using HTML format.
You want to integrate coverage results with other tools that read XML reports.
You want to check which lines of code are not covered by tests to improve quality.
Syntax
PyTest
pytest --cov=your_package --cov-report=term
pytest --cov=your_package --cov-report=html
pytest --cov=your_package --cov-report=xml

term shows coverage summary in the terminal.

html creates a detailed report you open in a browser.

xml creates an XML report for integration with other tools.

Examples
Run tests and show coverage summary in the terminal.
PyTest
pytest --cov=myapp --cov-report=term
Run tests and generate an HTML report in the htmlcov folder.
PyTest
pytest --cov=myapp --cov-report=html
Run tests and create an XML report named coverage.xml.
PyTest
pytest --cov=myapp --cov-report=xml
Sample Program

This simple test checks the add function. Running pytest with coverage and term report shows coverage summary in the terminal.

PyTest
# test_sample.py

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5

# Run coverage with terminal report:
# pytest --cov=. --cov-report=term
OutputSuccess
Important Notes

You can combine multiple reports by repeating --cov-report, e.g., --cov-report=term --cov-report=html.

HTML reports are saved in the htmlcov folder by default.

XML reports are useful for CI tools like Jenkins or SonarQube.

Summary

Coverage reports help find untested code.

Terminal reports show quick summaries.

HTML and XML reports provide detailed and shareable formats.