Sub topic 6
Comparing floating-point numbers with pytest.approx
This chapter assumes you can write a basic pytest test with plain assert, and that you know Python stores floats in binary. It uses the total function from cart.py in earlier chapters.
Why exact comparison fails
Most decimal fractions have no exact binary representation. Arithmetic on them leaves tiny rounding errors, so a result that looks correct on paper can differ from the expected literal.
print(0.1 + 0.2 == 0.3)
print(0.1 + 0.2)The first line prints False. The second prints 0.30000000000000004. A test that asserts == 0.3 would fail even though the code is correct.
Using pytest.approx
pytest.approx(expected) creates an object that compares equal to any number close enough to expected. You put it on the right side of ==.
# test_cart.py
import pytest
from cart import total
def test_total_with_tax():
assert total([10, 20], tax_rate=0.1) == pytest.approx(33.0)Here total computes 30 * 1.1. The result is 33.00000000000001, which is not exactly 33.0. The approx comparison accepts it.
If the values differ by more than the tolerance, the failure report shows the expected value, the tolerance and the actual value. This makes the mismatch easy to read.
How the tolerance works
By default, approx accepts a difference up to the larger of two limits:
- A relative tolerance of
1e-6, which is one millionth of the expected value. - An absolute tolerance of
1e-12.
The absolute limit matters when the expected value is zero. A relative tolerance of zero would accept nothing, so the absolute limit gives it room.
You can override either limit:
import pytest
def test_custom_tolerances():
assert 100.4 == pytest.approx(100, rel=0.01)
assert 0.05 == pytest.approx(0, abs=0.1)The first assertion allows a 1% difference. The second allows a difference of up to 0.1.
Lists, tuples, and dictionaries
approx also works on sequences and mappings. Each element is compared with the same tolerance.
import pytest
def test_collections():
assert [0.1 + 0.2, 1.0] == pytest.approx([0.3, 1.0])
assert {"a": 0.1 + 0.2} == pytest.approx({"a": 0.3})The sequences must have the same length. Dictionaries must have the same keys.
Common errors
- Putting
approxon the left side works in most cases, but keep it on the right for consistent failure messages. - Using
approxfor integers or strings is unnecessary. Use plainassertfor those. - Using a large
relvalue hides real bugs. Choose the smallest tolerance that fits the problem. - Do not use
approxto compare money in production code. Usedecimal.Decimalthere, and keepapproxfor tests of inherently inexact math.
Exercise
A function returns 0.30000000000000004. You write assert result == pytest.approx(0.3) and it passes. Another function should return exactly 0, but returns 1e-9 because of rounding. Does assert result == pytest.approx(0) pass with the default tolerances, and how would you make it pass?
Show answer
It fails. With an expected value of 0, the relative limit is 0, so only the absolute limit of 1e-12 applies, and 1e-9 exceeds it. Pass an absolute tolerance, for example pytest.approx(0, abs=1e-6).