Pytest and rounding floats in namedtuples

Viewed 480

If two named tuples are equal, they can be asserted in pytest like this:

assert tuple_under_test == expected_tuple

I want to do the same for named tuples containing floats. The catch is that these float values need to be rounded for proper comparison. Is there something similar to pytest.approx for named tuples?

assert tuple_under_test == compare_approx(Tuple(
    FloatValue=2.2
    FloatValue=2.3
), rel=0.1)
1 Answers

Just figured out that it is possible to use pytest.approx for named tuples as well. So this just works as exptected:

assert tuple_under_test == pytest.approx(Tuple(
    FloatValue=2.2
    FloatValue=2.3
), rel=0.1)

Great ;-)

Related