当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python unittest assertNotAlmostEqual()用法及代码示例


assertNotAlmostEqualPython中的()是unittest库函数,用于单元测试中以检查两个给定值是否不是。该函数将使用五个参数作为输入,并根据断言条件返回布尔值。

该函数通过计算差值,四舍五入到给定的小数位数(默认为7),然后与零进行比较,以检查第一和第二个近似不相等。

如果提供了增量而不是位置,则第一和第二之间的差异必须小于或等于增量。

用法:

assertNotAlmostEqual(第一,第二,地点= 7,消息=无,增量=无)



参数assertNotAlmostEqual()接受以下说明的三个参数:

  • first:第一个输入值(整数)
  • second:第二个输入值(整数)
  • places:近似的小数位
  • message:作为测试消息失败时显示的消息的字符串语句。
  • delta:近似的增量值

下面列出了两个不同的示例,它们说明了给定assert函数的正面和负面测试案例:

例:

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




相关用法


注:本文由纯净天空筛选整理自Shivam.Pradhan大神的英文原创作品 Python unittest – assertNotAlmostEqual() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。