Test Overview
This test checks if the test coverage tool correctly measures the percentage of code lines executed during testing. It verifies that the coverage report matches the expected coverage value.
This test checks if the test coverage tool correctly measures the percentage of code lines executed during testing. It verifies that the coverage report matches the expected coverage value.
import unittest import coverage class Calculator: def add(self, a, b): return a + b def subtract(self, a, b): return a - b class TestCalculator(unittest.TestCase): def test_add(self): calc = Calculator() self.assertEqual(calc.add(2, 3), 5) if __name__ == '__main__': cov = coverage.Coverage() cov.start() unittest.main(exit=False) cov.stop() cov.save() analysis = cov.analysis('test_coverage_metrics.py') coverage_percent = analysis[4] print(f"Coverage: {coverage_percent:.2f}%") assert coverage_percent >= 50, "Coverage is less than 50%"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and coverage measurement begins | Coverage tool initialized, test runner ready | - | PASS |
| 2 | Test runner executes test_add method of TestCalculator | Calculator instance created, add method called with (2,3) | Check if add(2,3) returns 5 | PASS |
| 3 | Coverage measurement stops and data saved | Coverage data collected for executed lines | - | PASS |
| 4 | Coverage report generated and coverage percentage calculated | Coverage report shows lines executed vs total lines | Assert coverage_percent >= 50 | PASS |