0
0
PyTesttesting~10 mins

pytest-cov for coverage - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs a simple function and uses pytest-cov to check how much of the code is covered by the test. It verifies that the coverage report shows 100% coverage for the tested function.

Test Code - pytest
PyTest
import pytest

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

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

# To run coverage: pytest --cov=./ --cov-report=term-missing
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts by running pytest with coverage plugin enabledTerminal ready to run pytest command-PASS
2pytest discovers test_add function in test filepytest test collection complete-PASS
3pytest runs test_add which calls add(2, 3)Function add executes and returns 5assert add(2, 3) == 5PASS
4pytest-cov collects coverage data during test runCoverage data recorded for add function-PASS
5pytest finishes and prints coverage report showing 100% coverage for add functionTerminal displays coverage summary with no missing linesCoverage report shows 100% coveragePASS
Failure Scenario
Failing Condition: Test fails if add function returns wrong result or coverage is incomplete
Execution Trace Quiz - 3 Questions
Test your understanding
What does pytest-cov do during the test run?
AIt records which lines of code were executed
BIt runs tests faster
CIt changes the test results
DIt skips tests without coverage
Key Result
Using pytest-cov helps you see exactly which parts of your code are tested, so you can improve your tests and avoid missing important cases.