pytest: assert almost equal

后端 未结 7 1519
时光说笑
时光说笑 2020-12-24 03:55

How to do assert almost equal with py.test for floats without resorting to something like:

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


        
7条回答
  •  被撕碎了的回忆
    2020-12-24 04:48

    I'd use nose.tools. It plays well with py.test runner and have other equally useful asserts - assert_dict_equal(), assert_list_equal(), etc.

    from nose.tools import assert_almost_equals
    assert_almost_equals(x, y, places=7) #default is 7 
    

提交回复
热议问题