📜  Python unittest – assertNotAlmostEqual()函数

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

Python unittest – assertNotAlmostEqual()函数

Python中的assertNotAlmostEqual () 是一个 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 not-equal with place
    def test_negativeWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimalPlace = 2
        # error message in case if test case got failed
        message = "first and second are almost equal."
        # assert function() to check if values are almost not-equal
        self.assertNotAlmostEqual(first, second, decimalPlace, message)
  
    # positive test function to test if values are almost not-equal with place
    def test_positiveWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimalPlace = 3
        # error message in case if test case got failed
        message = "first and second are almost equal."
        # assert function() to check if values are almost not-equal
        self.assertNotAlmostEqual(first, second, decimalPlace, message)
  
    # negative test function to test if values are almost not-equal with delta
    def test_negativeWithDelta(self):
        first = 4.4555
        second = 4.4566
        delta = 0.002
        # error message in case if test case got failed
        message = "first and second are almost equal."
        # assert function() to check if values are almost equal
        self.assertNotAlmostEqual(first, second, None, message, delta)
  
    # positive test function to test if values are almost not-equal with delta
    def test_positiveWithDelta(self):
        first = 4.4555
        second = 4.4566
        delta = 0.0001
        # error message in case if test case got failed
        message = "first and second are almost equal."
        # assert function() to check if values are almost not-equal
        self.assertNotAlmostEqual(first, second, None, message, delta)
  
  
if __name__ == '__main__':
    unittest.main()


输出:

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

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

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

FAILED (failures=2)

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