pytest: ほぼ等しいとアサートする 質問する

pytest: ほぼ等しいとアサートする 質問する

次のようなものに頼らずに float をassert almost equal処理する方法:pytest

assert x - 0.00001 <= y <= x + 0.00001

より具体的には、float のペアをアンパックせずにすばやく比較するための優れたソリューションを知っておくと便利です。

assert (1.32, 2.4) == i_return_tuple_of_two_floats()

ベストアンサー1

この質問はpytestについて具体的に尋ねていることに気づきました。pytest 3.0にはapprox()関数(まあ、本当にクラスです) それはこの目的に非常に役立ちます。

import pytest

assert 2.2 == pytest.approx(2.3)
# fails, default is ± 2.3e-06
assert 2.2 == pytest.approx(2.3, 0.1)
# passes

# also works the other way, in case you were worried:
assert pytest.approx(2.3, 0.1) == 2.2
# passes

おすすめ記事