📜  Python unittest – assertAlmostEqual()函数

📅  最后修改于: 2022-05-13 01:55:50.125000             🧑  作者: Mango

Python unittest – assertAlmostEqual()函数

Python中的assertAlmostEqual () 是一个 unittest 库函数,用于在单元测试中检查两个给定值是否几乎相等。该函数将五个参数作为输入,并根据断言条件返回一个布尔值。

此函数通过计算差异、四舍五入到给定的小数位数(默认为 7)并与零比较来检查firstsecond是否近似相等。

如果提供delta而不是places ,则firstsecond之间的差必须小于或等于delta

下面列出了两个不同的示例,说明了给定断言函数的正负测试用例:

例子 :

Python3
# test suite
import unittest
  
  
class TestStringMethods(unittest.TestCase):
    # negative test function to test if values are almost equal with place
    def test_negativeWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimalPlace = 3
        # error message in case if test case got failed
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal
        self.assertAlmostEqual(first, second, decimalPlace, message)
  
    # positive test function to test if values are almost equal with place
    def test_positiveWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimalPlace = 2
        # error message in case if test case got failed
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal
        self.assertAlmostEqual(first, second, decimalPlace, message)
  
    # negative test function to test if values are almost equal with delta
    def test_negativeWithDelta(self):
        first = 4.4555
        second = 4.4566
        delta = 0.0001
        # error message in case if test case got failed
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal
        self.assertAlmostEqual(first, second, None, message, delta)
  
    # positive test function to test if values are almost equal with delta
    def test_positiveWithDelta(self):
        first = 4.4555
        second = 4.4566
        delta = 0.002
        # error message in case if test case got failed
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal
        self.assertAlmostEqual(first, second, None, message, delta)
  
  
if __name__ == '__main__':
    unittest.main()


输出:

FF..
======================================================================
FAIL: test_negativeWithDelta (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/2c36bbd2c940a6c7b81a901ebfdd2ad8.py", line 34, in test_negativeWithDelta
    self.assertAlmostEqual(first, second, None, message, delta)
AssertionError: 4.4555 != 4.4566 within 0.0001 delta : first and second are not almost equal.

======================================================================
FAIL: test_negativeWithPlaces (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/2c36bbd2c940a6c7b81a901ebfdd2ad8.py", line 14, in test_negativeWithPlaces
    self.assertAlmostEqual(first, second, decimalPlace, message)
AssertionError: 4.4555 != 4.4566 within 3 places : first and second are not almost equal.

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=2)

参考:https://docs。 Python.org/3/library/unittest.html