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


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


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

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

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

用法: assertAlmostEqual(first, second, places=7, message=None, delta=None)

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



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

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

例:

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




相关用法


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