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 |
pytest.approx in tests?pytest.approxpytest.approx allows tests to pass if numbers are close within a tolerance, avoiding false failures.result is approximately equal to 3.14 using pytest.approx?def test_value():
value = 0.1 + 0.2
assert value == pytest.approx(0.3)def test_approx():
result = 1.0001
assert result == pytest.approx(1.0, rel=0.00001)pytest.approx?Pass the test and this pattern moves unknown