Meaning
Compares floating-point values using a relative tolerance to avoid false failures from tiny rounding errors. Addresses the pain point of exact equality checks failing on floats due to representation limits. Triggered when testing numerical computations or scientific code.
Primary Function
Floating-point comparison
Communicative Purpose
Prevents false test failures due to minor floating-point rounding differences.
Pattern
assert pytest.approx(computed, rel=tolerance) == expected
Core Structure
assert pytest.approx(..., rel=...) == ...
Função primária
Floating-point comparison
Propósito comunicativo
Prevents false test failures due to minor floating-point rounding differences.
Situações de gatilho
Numerical computing: testing outputs of floating-point math functions; Data science: validating model predictions with small precision differences; Physics simulation: asserting computed trajectories match expected values within tolerance
Contextos
pytest, scientific computing, data science, numerical algorithms
Padrão
assert pytest.approx(computed, rel=tolerance) == expected
Estrutura central
assert pytest.approx(..., rel=...) == ...
Slots de substituição
computed: float or sequence of floats, tolerance: float for relative tolerance, expected: float or sequence of floats
Colocados típicos
- pytest.raises
- pytest.fixture
- math.isclose
- numpy.testing.assert_allclose
Substituições comuns
- math.isclose(a
- b
- rel_tol=1e-7): standard library alternative
- does not support nested structures
- numpy.testing.assert_allclose: supports arrays but requires numpy dependency
Erros comuns
Using abs= instead of rel= when relative tolerance is needed: abs= uses absolute tolerance which doesn't scale with magnitude; Forgetting to import pytest: results in NameError; Comparing large nested structures without approx: exact equality fails on deep floating point mismatches
Similar / contraste
math.isclose: built-in standard library function for scalar float comparison; unittest.assertAlmostEqual: xUnit-style assertion for approximate equality
Interferências
Coming from unittest: using assertAlmostEqual instead of pytest.approx — pytest.approx integrates with pytest's assertion rewriting and supports nested data structures.
Família do chunk
- pytest.approx
- math.isclose
- numpy.testing.assert_allclose
- unittest.assertAlmostEqual
Nuance
Do not use for exact integer comparisons where rounding is not a concern. rel= tolerance scales with the magnitude of the expected value, so very small values might need an absolute tolerance (abs=) as well. pytest.approx works with dictionaries and lists of floats, not just scalars.
Efeito pragmático
Ensures numerical tests are robust against platform-specific floating-point representation differences while still catching significant regressions.
Dica de memória
Like a measuring tape that stretches slightly: pytest.approx lets your expected value have a little wiggle room.
Nota
pytest.approx can also be used with absolute tolerance via abs=, or both abs= and rel= together.
Upgrade path
numpy.testing.assert_allclose for array comparisons with detailed error messages
Log in to save chunks.