0
0
PyTesttesting~5 mins

Coverage in CI pipelines in PyTest

Choose your learning style9 modes available
Introduction

Coverage in CI pipelines helps check how much of your code is tested automatically. It shows if important parts are missing tests.

When you want to make sure new code changes have enough tests before merging.
When you want to catch untested code early during development.
When you want to keep track of test quality over time.
When you want to prevent bugs by ensuring critical code is covered by tests.
Syntax
PyTest
pytest --cov=your_package tests/

This command runs tests and measures coverage for the specified package.

Use --cov-report=term to see coverage summary in the terminal.

Examples
Runs tests in the tests/ folder and measures coverage for the myapp package.
PyTest
pytest --cov=myapp tests/
Shows coverage and lists lines not covered by tests in the terminal.
PyTest
pytest --cov=myapp --cov-report=term-missing tests/
Generates an XML coverage report for CI tools to read.
PyTest
pytest --cov=myapp --cov-report=xml tests/
Sample Program

This simple test checks only the add function. The subtract function is not tested, so coverage will show it as missing.

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

def subtract(a, b):
    return a - b


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

# Note: No test for subtract function

# Run with:
# pytest --cov=.
OutputSuccess
Important Notes

Always generate coverage reports in formats your CI tool supports, like XML or HTML.

Coverage shows what code runs during tests, but not if tests are correct.

High coverage is good, but focus also on meaningful tests.

Summary

Coverage in CI helps track how much code is tested automatically.

Use pytest --cov commands to measure and report coverage.

Integrate coverage reports in CI pipelines to catch missing tests early.