Test Overview
This test checks if a floating-point calculation result is approximately equal to an expected value using pytest.approx. It verifies that small differences due to floating-point precision do not cause the test to fail.
This test checks if a floating-point calculation result is approximately equal to an expected value using pytest.approx. It verifies that small differences due to floating-point precision do not cause the test to fail.
import pytest def test_approximate_comparison(): result = 0.1 + 0.2 expected = 0.3 assert result == pytest.approx(expected, rel=1e-9)
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | - | PASS |
| 2 | Calls test_approximate_comparison function | Function execution begins | - | PASS |
| 3 | Calculates result = 0.1 + 0.2 | result variable holds floating-point sum 0.30000000000000004 | - | PASS |
| 4 | Compares result to expected using pytest.approx with relative tolerance 1e-9 | Comparison allows small floating-point difference | Assert result == pytest.approx(expected, rel=1e-9) | PASS |
| 5 | Test completes successfully | Test runner reports test passed | - | PASS |