I am learning to use pytest
. It complains about the call difference(5.2, 5.6)
returning the value -0.39999999999999947
instead of 0.4
. Surely these two numbers are not the same, but is there a way for pytest
to tell that this error is OK?
Elaborate form of my question:
My root directory looks like this:
Learn-Python-Testing
|
|- math_functions
| |- __init__.py
|
|- tests.py
This is the implementation of my math function:
# file math_functions/__init__.py
import numpy as np
def difference(a: float, b: float) -> float:
return a-b
And here are four test cases, of which one will fail:
# file tests.py
import numpy as np
import pytest
import math_functions # the self-written module I want to test
@pytest.mark.parametrize("argument_1, argument_2, difference", [
(71, 15, 56),
(71, -15, 86),
(5.2, 5.6, -0.4),
(152, 156, -4),
])
def test_difference(argument_1, argument_2, difference):
assert math_functions.difference(argument_1, argument_2) == difference
When running
~/Learn-Python-Testing$ pytest tests.py
it prints the following:
=============================== test session starts ===============================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
Using --randomly-seed=3306462934
rootdir: /home/nerdontour/Learn-Python-Testing
plugins: randomly-3.11.0, anyio-3.3.2, dash-2.0.0
collected 4 items
tests.py ..F. [100%]
==================================== FAILURES =====================================
__________________________ test_difference[5.2-5.6--0.4] __________________________
argument_1 = 5.2, argument_2 = 5.6, difference = -0.4
@pytest.mark.parametrize("argument_1, argument_2, difference", [
(71, 15, 56),
(71, -15, 86),
(5.2, 5.6, -0.4),
(152, 156, -4),
])
def test_difference(argument_1, argument_2, difference):
> assert math_functions.difference(argument_1, argument_2) == difference
E assert -0.39999999999999947 == -0.4
E + where -0.39999999999999947 = <function difference at 0x7fa0e41f3a60>(5.2, 5.6)
E + where <function difference at 0x7fa0e41f3a60> = math_functions.difference
tests.py:25: AssertionError
============================= short test summary info =============================
FAILED tests.py::test_difference[5.2-5.6--0.4] - assert -0.39999999999999947 == ...
=========================== 1 failed, 3 passed in 0.13s ===========================
Now I am aware that indeed -0.39999999999999947 != -0.4
. However, you would certainly agree that I do not want my test system to hint me on such a small computation error (would you?). Is there a way to tell pytest
that any result within the range [-0.39998, -0.40002]
(say) is OK? Or is there a different best practice for this problem?