Pytest approximately equal scalars and arrays

less than 1 minute read

Pytest cleanly handles so many continuous integration issues that it is not worth fooling around with obsolete nose and legacy unittest modules. One of those many areas is in the constant need to compare equality of floating point numbers. In general computer representations of floating point numbers have finite precision, and so in general the fundamental arithmetic assumptions learned in elementary school about real numbers are broken, including associativity. A practical solution to this problem is to compare numbers (scalars or arrays) to within an absolute and relative tolerance. Widely known functions exist to compare “equality” of floating point numbers in Python and Fortran among other numerical languages.

pytest.approx provides a syntactically clean approach that may be more intuitive and readable for CI applications. It works with scalars and arrays of all sorts including the ubiquitous numpy.ndarray.

Example

This example shows how to replace numpy.testing.assert_allclose() with pytest.approx():

import numpy as np
from pytest import approx


def test_mynums():
    x = np.array([2.00000000001, 1.99999999999])
    
    assert x == approx(2.)

Whereas with Numpy, the last line would have been np.testing.assert_allclose(x, 2.). I find the Pytest syntax and appearance to be more readable.

Leave a Comment