0
0
PyTesttesting~5 mins

Why coverage measures test completeness in PyTest

Choose your learning style9 modes available
Introduction

Coverage shows how much of your code is tested. It helps find parts not checked by tests.

You want to know if your tests check all important parts of your program.
You need to improve your tests to catch more bugs.
You want to avoid missing errors in code that is never tested.
You want to show your team or boss how complete your testing is.
You want to focus testing on parts of code that are rarely used.
Syntax
PyTest
pytest --cov=your_package tests/

This command runs tests and measures coverage for 'your_package'.

Replace 'your_package' with the folder or module you want to check.

Examples
Runs tests in 'tests/' and measures coverage for 'myapp' folder.
PyTest
pytest --cov=myapp tests/
Runs a single test file and measures coverage for 'module_name'.
PyTest
pytest --cov=module_name test_module.py
Shows coverage report with lines not covered in the terminal.
PyTest
pytest --cov=src --cov-report=term-missing tests/
Sample Program

This simple code has two functions: add and subtract. The test only checks add.

Running coverage will show subtract is not tested.

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
OutputSuccess
Important Notes

Coverage does not guarantee your tests find all bugs, only that code runs during tests.

High coverage is good, but tests must also check correct behavior.

Use coverage reports to improve tests by adding missing cases.

Summary

Coverage measures how much code your tests run.

It helps find untested parts of your program.

Use coverage to improve test completeness and quality.