Test Overview
This test runs a Python pytest suite with coverage measurement in a Continuous Integration (CI) pipeline. It verifies that the test coverage meets the minimum required threshold.
This test runs a Python pytest suite with coverage measurement in a Continuous Integration (CI) pipeline. It verifies that the test coverage meets the minimum required threshold.
import pytest import subprocess def test_coverage_threshold(): # Run pytest with coverage and generate a coverage report result = subprocess.run([ 'pytest', '--cov=.', '--cov-report=term-missing', '--cov-fail-under=80', '-k', 'not test_coverage_threshold' ], capture_output=True, text=True) # Assert that pytest exited with code 0 (success) assert result.returncode == 0, f"Coverage threshold not met. Output:\n{result.stdout}\n{result.stderr}"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts: pytest command with coverage options is executed | CI environment runs pytest with coverage measuring all source files | - | PASS |
| 2 | pytest discovers and runs all test functions | Tests execute, coverage data is collected for each source line | - | PASS |
| 3 | pytest generates coverage report showing lines covered and missing | Coverage report output is available in terminal | - | PASS |
| 4 | pytest checks if coverage percentage meets or exceeds 80% | Coverage threshold check performed internally by pytest-cov plugin | Assert that pytest exit code is 0 indicating coverage threshold met | PASS |
| 5 | Test ends with success if coverage threshold is met | CI pipeline receives success status | - | PASS |