📜  Python – 单元测试中的 assertLessEqual()函数

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

Python – 单元测试中的 assertLessEqual()函数

Python中的assertLessEqual()是一个unittest库函数,用于单元测试以检查第一个给定值是否小于或等于第二个值。该函数将采用三个参数作为输入,并根据断言条件返回一个布尔值。

此函数检查第一个给定值是否小于或等于第二个值,如果是,则返回 true,否则如果第一个值不小于或等于第二个值,则返回 false。

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

示例 1:

Python3
# test suite
import unittest
  
class TestStringMethods(unittest.TestCase):
    
    # negative test function to test if values1 is less or equal than value2
    def test_negativeForLessEqual(self):
        first = 6
        second = 5
          
        # error message in case if test case got failed
        message = "first value is not less than or equal to second value."
          
        # assert function() to check if values1 is less or equal than value2
        self.assertLessEqual(first, second, message)
  
if __name__ == '__main__':
    unittest.main()


Python
# test suite
import unittest
  
class TestStringMethods(unittest.TestCase):
    
    # positive test function to test if value1 is less or equal than value2
    def test_positiveForLessEqual(self):
        first = 4
        second = 4
          
        # error message in case if  test case got failed
        message = "first value is not less than or equal to second value."
          
        # assert function() to check if values1 is less or equal than value2
        self.assertLessEqual(first, second, message)
  
  
if __name__ == '__main__':
    unittest.main()


输出:

示例 2:

Python

# test suite
import unittest
  
class TestStringMethods(unittest.TestCase):
    
    # positive test function to test if value1 is less or equal than value2
    def test_positiveForLessEqual(self):
        first = 4
        second = 4
          
        # error message in case if  test case got failed
        message = "first value is not less than or equal to second value."
          
        # assert function() to check if values1 is less or equal than value2
        self.assertLessEqual(first, second, message)
  
  
if __name__ == '__main__':
    unittest.main()

输出: