Complete worked example
python
import pytest
def total(values):
return sum(values)
def test_decimal_total():
assert total([0.1, 0.2]) == pytest.approx(0.3, rel=1e-9, abs=1e-12)- Reasoning
- pytest.approx compares values using explicit relative and absolute tolerances. The complete example totals a small collection, then compares the result with an independently calculated decimal target. It keeps the tolerance tight enough to reject a meaningful added charge. When rel and abs are both supplied, meeting either tolerance can be sufficient. This is not a requirement to satisfy both simultaneously. The example explicitly supplies both values so readers can inspect the intended bound rather than assuming that library defaults match their domain.