1. Home
  2. Blog
  3. Test Automation

Test Automation

Pytest approx: Comparing Floats With rel, abs and Collections

Pytest approx compares floating-point results within a tolerance, so 0.1 + 0.2 equals 0.3 in your tests the way it does in maths. We tested its default tolerances right at their boundaries, rel and abs, lists and dicts, NaN and infinity, NumPy arrays and the near-zero trap, and compared it with math.isclose and numpy.allclose, which disagree on the same numbers.

9 min read
Pytest approx: comparing floats with rel, abs and collections

Key takeaways

  • Pytest approx passes when values are within a relative tolerance of 1e-6 or an absolute tolerance of 1e-12, whichever is larger.
  • 1,000,000.9 matched 1,000,000 but 1,000,001.1 did not, exactly at the one-in-a-million boundary.
  • Near zero, only the tiny absolute tolerance applies: 1e-11 did not match 0 until we passed abs=1e-9.
  • numpy.allclose called 1e-11 and 0 equal, while approx and math.isclose did not.

Floating-point numbers are stored in binary, so most decimal fractions are approximations: 0.1 + 0.2 is 0.30000000000000004. Any test that checks a calculated float with == is fragile, failing on tiny rounding differences between platforms, library versions or the order of operations. Pytest approx solves this by comparing within a tolerance, with readable syntax and clear failure messages.

All tests ran with pytest 9.2.0 on Python 3.13.6; the NumPy checks used NumPy 2.5.5. Every assertion below passed unless marked as a deliberate failure.

Why == fails for floats

def test_plain_equality_fails():
    assert 0.1 + 0.2 != 0.3      # passes: they really are different

Neither 0.1 nor 0.2 has an exact binary representation, and the tiny errors add up. Comparing with == asks whether the bits are identical, which is almost never what a test means. A test of a tax calculation, a statistics function or a physics formula should ask whether the answer is close enough.

Basic use

import pytest

def test_approx_default():
    assert 0.1 + 0.2 == pytest.approx(0.3)

Wrap the expected value in pytest.approx and compare with == (or !=). It reads like the maths and works inside any assertion. Integers work too: 3 == pytest.approx(3.0000001) passed.

Default tolerances, tested at the boundary

def test_rel_default_boundary():
    assert 1_000_000 + 0.9 == pytest.approx(1_000_000)    # within 1e-6 relative
    assert 1_000_000 + 1.1 != pytest.approx(1_000_000)    # just outside

By default, approx uses a relative tolerance of 1e-6 (one part in a million) of the expected value. For 1,000,000, the allowed difference is 1.0, so 0.9 away passed and 1.1 away failed. Relative tolerance scales with the size of the number, which is usually what you want: a millimetre matters for a bolt, not for the distance to the Moon.

The near-zero trap

def test_near_zero():
    assert 1e-13 == pytest.approx(0)
    assert 1e-11 != pytest.approx(0)
    assert 1e-11 == pytest.approx(0, abs=1e-9)

A relative tolerance of anything times zero is zero, so for an expected value of 0 only the default absolute tolerance of 1e-12 applies. 1e-13 passed, but 1e-11, which is tiny by any practical measure, did not. Results that should be zero often come out as 1e-10 or 1e-16 after floating-point arithmetic, such as math.sin(math.pi) or a balance of debits and credits. When the expected value is or might be zero, pass an explicit abs that matches your domain.

Setting rel and abs

def test_rel_abs():
    assert 101 == pytest.approx(100, rel=0.02)     # within 2 percent
    assert 103 != pytest.approx(100, rel=0.02)
    assert 100.4 == pytest.approx(100, abs=0.5)    # within 0.5 either way

rel is a fraction of the expected value and abs is a fixed amount. The combinations behave differently, which we confirmed: passing only abs switches the relative tolerance off completely (1,000,000.9 failed against approx(1_000_000, abs=0.5) even though it is within one part in a million), passing only rel keeps the default absolute tolerance of 1e-12, and passing both lets a value through when it is within either tolerance. Negative or NaN tolerances raise ValueError, according to pytest’s own test suite. Choose rel for quantities that vary in size (prices, distances, durations) and abs for values with a known resolution (sensor readings to 0.1 degree, probabilities near zero).

Lists, tuples and dicts

def test_collections():
    assert [0.1 + 0.2, 0.2 + 0.4] == pytest.approx([0.3, 0.6])
    assert {"a": 0.1 + 0.2, "b": 1.0} == pytest.approx({"a": 0.3, "b": 1.0})
    assert (1/3, 2/3) == pytest.approx((0.3333333, 0.6666667))

approx compares sequences element by element and dictionaries key by key, applying the same tolerance to every number. Lengths and keys must match exactly. This keeps tests of functions that return several floats, like statistics summaries or coordinates, to a single readable assertion. Combine it with parametrized fixtures from our pytest fixtures guide to check many cases at once.

NaN and infinity

def test_nan():
    assert math.nan != pytest.approx(math.nan)
    assert math.nan == pytest.approx(math.nan, nan_ok=True)

def test_inf():
    assert math.inf == pytest.approx(math.inf)

NaN (“not a number”) is never equal to anything, including itself, and approx follows that rule unless you pass nan_ok=True, useful when a function is supposed to return NaN for missing data. Infinity equals only infinity of the same sign; no tolerance makes a finite number match it.

NumPy arrays

a = np.array([0.1, 0.2]) * 3        # [0.30000000000000004, 0.6000000000000001]
a == pytest.approx([0.3, 0.6])      # True
np.allclose(a, [0.3, 0.6])          # True

approx understands NumPy arrays and compares them element-wise, so a single assert covers a whole array. In our pytest venv NumPy was not installed and that test was skipped with pytest.importorskip("numpy"), a handy way to make optional-dependency tests safe; we ran the NumPy checks with a separate interpreter that had NumPy 2.5.5.

pytest.approx vs math.isclose vs numpy.allclose

math.isclose(1e-11, 0)                  # False  (rel 1e-9, abs 0)
1e-11 == pytest.approx(0)               # False  (rel 1e-6, abs 1e-12)
np.allclose([1e-11], [0])               # True   (rtol 1e-5, atol 1e-8)
Function Default relative Default absolute
pytest.approx 1e-6 1e-12
math.isclose 1e-9 0
numpy.allclose / isclose 1e-5 1e-8

The three standard tools gave different answers for the same pair of numbers because their defaults differ by orders of magnitude. math.isclose has no absolute tolerance at all by default, so nothing is close to zero; NumPy is the most forgiving. When a team mixes them, the same comparison can pass in one test and fail in another. Pick explicit tolerances when it matters, rather than relying on any default.

Reading failure messages

assert [1.0, 2.0, 3.0] == pytest.approx([1.0, 2.1, 3.0])      # deliberate failure

E   comparison failed. Mismatched elements: 1 / 3:
E   Max absolute difference: 0.10000000000000009
E   Max relative difference: 0.050000000000000044
E   Index | Obtained | Expected
E   1     | 2.0      | 2.1 ± 2.1e-06

On failure, pytest shows how many elements mismatched, the largest absolute and relative differences and a table of the failing positions with the allowed tolerance (± 2.1e-06, which is 1e-6 of 2.1). That makes it easy to see whether a test is slightly too strict or the code is genuinely wrong.

What approx cannot do

def test_wrong_side():
    with pytest.raises(TypeError):
        assert pytest.approx(1) > 0.5

approx supports only equality and inequality. Ordering comparisons such as > raise TypeError, which we caught with pytest.raises; for “at least” checks, compare the raw numbers with a margin yourself. approx also does not handle strings, and nested structures beyond simple sequences and mappings may not compare as you expect.

Money is not a float problem

def test_money_wrong():
    assert 0.1 * 3 == pytest.approx(0.3)                 # passes, but hides the real issue
    from decimal import Decimal
    assert Decimal("0.1") * 3 == Decimal("0.3")          # exact

If a test needs approx to check a currency total, the code under test probably should not use floats for money. Decimal or integer cents give exact results and exact assertions. Rounding rules for stored amounts are covered in our SQL ROUND guide.

Choosing tolerances

  • Start from the domain: how precise is the input data and how precise does the answer need to be?
  • Use abs near zero and rel for values that scale.
  • Do not loosen tolerances to silence a failing test without understanding why it failed.
  • Keep tolerances in one place, for example a constant or fixture, so they are consistent across a suite.
  • Generate inputs with property-based testing to find the cases where your tolerance is too tight, before they become flaky tests on another platform.

Final thoughts

Pytest approx replaces fragile float equality with tolerant, readable assertions. Remember its defaults (1e-6 relative, 1e-12 absolute), set an explicit abs whenever zero is a possible answer, use it directly on lists, dicts and NumPy arrays, and do not assume other tools share its defaults: in our test, three standard functions gave two different answers for the same numbers. For money, fix the data type rather than the assertion.

Frequently asked questions

What is the default tolerance of pytest approx?

A relative tolerance of one millionth of the expected value, or an absolute tolerance of 1e-12, whichever is larger. So for large numbers the relative part dominates, and near zero only the absolute part applies.

How do I compare a float to zero in pytest?

Pass an explicit absolute tolerance, for example pytest.approx(0, abs=1e-9). The default absolute tolerance is only 1e-12, so tiny rounding errors near zero otherwise fail.

Can pytest approx compare lists and dictionaries?

Yes. It compares sequences element by element and dictionaries key by key, applying the same tolerance to each number. Lengths and keys must match exactly.

What is the difference between rel and abs in pytest approx?

rel is a fraction of the expected value, so the allowed error grows with the number. abs is a fixed amount. Passing only abs turns relative tolerance off, while passing both lets a value pass if it is within either one.

Why does pytest approx fail with NaN?

NaN is never equal to anything, including itself. Pass nan_ok=True to pytest.approx if your test expects NaN as a valid result.

Keep learning

Work with Stack Quora

Need help with testing, WordPress or SEO?

We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

Random Forest in Python: Bagging, Feature Sampling and OOB From Scratch

Random forest in Python from scratch: bootstrap sampling, feature sampling, number of trees, out-of-bag evaluation and impurity vs permutation importance.

Read article →
Software Development

MIME Types Explained: Content-Type, nosniff and a Common Types List

MIME types explained and tested in a browser: Content-Type, nosniff, scripts and stylesheets with wrong types, a common MIME types…

Read article →
DevOps

Git Clean Explained: Remove Untracked Files Without Regret

Git clean explained with dry runs: -n, -f, -d, -x, -X, exclusions, paths, nested repositories and interactive mode, plus a…

Read article →
Test Automation

Pytest approx: Comparing Floats With rel, abs and Collections

Pytest approx explained: default tolerances, the near-zero trap, rel and abs, lists and dicts, NaN, NumPy, and how it differs…

Read article →
Software Testing

Accessibility Testing: WCAG Checklist, Automated Tools and Manual Checks

Accessibility testing explained: WCAG basics, an axe-core scan of a broken and a fixed page, what automation missed, contrast checks…

Read article →
Databases & SQL

SQL UNPIVOT: Turn Columns Into Rows, 4 Ways Tested

SQL UNPIVOT explained: turn columns into rows with UNION ALL, CROSS JOIN with CASE, JSON functions or native UNPIVOT, with…

Read article →
Software Development

Intl.NumberFormat in JavaScript: Currency, Compact, Percent and Units

Intl.NumberFormat in JavaScript: locales, currency, compact notation, percent, units, rounding, formatToParts, the non-breaking space trap and formatter reuse.

Read article →
Software Development

Python Abstract Class: ABC, abstractmethod and When to Use Them

Python abstract class explained: ABC and abstractmethod, abstract properties, shared logic with super, register, collections.abc and ABC vs Protocol, tested.

Read article →
Software Development

Python Subprocess Explained: run, Capture Output, Timeouts and Safety

Python subprocess explained with tested examples: run, capture_output, check, timeouts, input, environment, shell injection, encoding on Windows and Popen streaming.

Read article →
Databases & SQL

SQL EXPLAIN Plan: How to Read Query Plans and Fix Slow Queries

SQL EXPLAIN plan explained: read scans, index searches, covering indexes and temporary sorts, with before and after timings for five…

Read article →
Artificial Intelligence

Decision Tree in Python: Gini, Entropy and Overfitting From Scratch

Decision tree in Python from scratch: Gini impurity, entropy, information gain, best splits, readable rules, and how depth and leaf…

Read article →
Software Development

URL Query Parameters: Encoding, URLSearchParams and Common Traps

URL query parameters explained: reading and building them with URLSearchParams and urllib, percent encoding, plus signs, repeated keys and nine…

Read article →
DevOps

Git Blame Explained: Find Who Changed a Line, and Why

Git blame explained with a real repository: ignore whitespace, line ranges and functions, ignore-revs files for formatting commits, moved code…

Read article →
Test Automation

Pytest Benchmark: Measure Speed and Catch Performance Regressions

Pytest benchmark tutorial: the benchmark fixture, reading results, parametrized and pedantic benchmarks, saving baselines and failing the build on regressions.

Read article →
Software Testing

Defect Density: Formula, Worked Examples and How to Use It

Defect density explained: the formula, a five-module worked example, per KLOC vs per function point, severity weighting, defect removal efficiency…

Read article →
Databases & SQL

Case Insensitive Search in SQL: LOWER, LIKE, COLLATE and Indexes

Case insensitive search in SQL: LOWER, LIKE, COLLATE NOCASE and expression indexes compared with query plans, plus accented letters and…

Read article →
Databases & SQL

ON DELETE CASCADE in SQL: CASCADE vs SET NULL vs RESTRICT

ON DELETE CASCADE in SQL tested: CASCADE vs SET NULL vs RESTRICT, multi-level cascades, the SQLite foreign key pragma, row…

Read article →
Software Development

JavaScript BigInt Explained: Big Numbers, Division and JSON

JavaScript BigInt explained: the 2^53 limit, creating and converting BigInt, division, mixing types, JSON stringify and parse without losing digits,…

Read article →
Artificial Intelligence

Logistic Regression in Python: Sigmoid, Log Loss and Gradient Descent From Scratch

Logistic regression in Python from scratch: sigmoid, log loss, gradient descent, odds ratios, thresholds, sigmoid overflow and separable data, tested…

Read article →
DevOps

Git Worktree Explained: Work on Two Branches at Once

Git worktree explained: add, list, remove, prune, lock and detached worktrees, a hotfix workflow, and the errors you will hit,…

Read article →
Test Automation

Pytest ini Explained: addopts, markers, testpaths and Precedence

Pytest ini explained: testpaths, addopts, strict markers, xfail_strict, filterwarnings, minversion, config file precedence and command-line overrides, tested.

Read article →
Software Testing

Test Estimation Techniques: WBS, Three Point PERT and Monte Carlo

Test estimation techniques worked through: WBS, three point PERT, total uncertainty, a Monte Carlo check, percentage rules and Wideband Delphi.

Read article →
Databases & SQL

Gaps and Islands in SQL: Find Streaks and Missing Ranges

Gaps and islands in SQL: find login streaks and missing ranges with ROW_NUMBER, DENSE_RANK, LAG and LEAD, plus the duplicate…

Read article →
Databases & SQL

INSERT INTO SELECT in SQL: Copy Rows Between Tables Safely

INSERT INTO SELECT in SQL: copy rows between tables with chosen columns, joins and summaries, avoid duplicates on reruns, upsert,…

Read article →
Artificial Intelligence

Linear Regression in Python: Least Squares From Scratch, With R Squared

Linear regression in Python from scratch: closed form, NumPy least squares and gradient descent, R squared, RMSE, outliers and curved…

Read article →
DevOps

Git Remote Explained: Add, Set-URL, Rename, Remove and Push URLs

Git remote explained: add, set-url, separate push URLs, rename, remove and prune, plus how to avoid leaking tokens in remote…

Read article →
Test Automation

Pytest Mock: mocker.patch, spy, side_effect and autospec Explained

Pytest mock explained: mocker.patch, return_value, side_effect, spy, autospec and PropertyMock, with tested examples for APIs, clocks and retries.

Read article →
Software Testing

System Testing Explained: Types, Process and a Real End-to-End Example

System testing explained: types, entry and exit criteria, the process, and a real end-to-end test suite run against a small…

Read article →
Artificial Intelligence

Naive Bayes Classifier Explained: Build a Spam Filter in Python

Naive Bayes classifier explained by building a spam filter in Python: Bayes rule, the naive assumption, Laplace smoothing, log probabilities…

Read article →
DevOps

Git Branch Explained: Create, Switch, Rename, Delete and Recover

Git branch explained with tested commands: list, create, switch, rename, delete, merged checks, recovering deleted branches, remote tracking and naming.

Read article →
Test Automation

Pytest raises: Testing Exceptions, match and excinfo

Pytest raises explained with tested examples: match regexes, excinfo, custom and chained exceptions, parametrized error cases, pytest.warns and common mistakes.

Read article →
Software Testing

Non Functional Testing: Types, Metrics and a Real Load Test

Non functional testing explained: 10 key types, measurable requirements, latency percentiles and what a small real load test revealed about…

Read article →
Artificial Intelligence

K Means Clustering Explained: How It Works, With Python Code

K means clustering explained: the algorithm, a from-scratch NumPy version, choosing k with the elbow and silhouette methods, k-means++, scaling…

Read article →
DevOps

Git Log Explained: 15 Useful Commands and Pretty Formats

Git log explained with 15 tested commands: oneline, graph, filters by author, date, message and code, file history, stats, pretty…

Read article →
Test Automation

Pytest conftest.py Explained: Shared Fixtures, Hooks and Overrides

Pytest conftest.py explained with a tested suite: shared fixtures, scope and teardown, folder overrides, autouse, custom options, markers and useful…

Read article →
Software Testing

Usability Testing: Methods, Metrics, SUS Score and Checklist

Usability testing explained: methods, planning, writing tasks, running sessions, metrics, SUS scoring, how many users you need and a practical…

Read article →