Python – 单元测试中的 assertLess()函数
Python中的 assertLess() 是一个 unittest 库函数,用于单元测试以检查第一个给定值是否小于第二个值。该函数将采用三个参数作为输入,并根据断言条件返回一个布尔值。
该函数检查第一个给定的值是否小于第二个值,如果是,则返回 true,如果第一个值不小于第二个值,则返回 false。
Syntax: assertLess(first, second, message=None)
Parameters: assertLess() accept three parameters which are listed below with explanation:
- first: first input value (integer)
- second: second input value (integer)
- message: a string sentence as a message which got displayed when the test case got failed.
下面列出了一个示例,说明了给定断言函数的正面和负面测试用例:
示例 1 :如果第一个值不小于第二个值。
Python3
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# negative test function to test if
# values1 is less than value2
def test_negativeForLess(self):
first = 6
second = 5
# error message in case if test case got failed
message = "first value is not less that second value."
# assert function() to check if values1 is
# less than value2
self.assertLess(first, second, message)
# main for function call
if __name__ == '__main__':
unittest.main()
Python
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# positive test function to test if
# values are almost equal with place
def test_positiveForLess(self):
first = 2
second = 3
# error message in case if test case got failed
message = "first value is not less that second value."
# assert function() to check if values1 is
# less than value2
self.assertLess(first, second, message)
# main for function call
if __name__ == '__main__':
unittest.main()
输出:
F
======================================================================
FAIL: test_negativeForLess (__main__.TestStringMethods)
———————————————————————-
Traceback (most recent call last):
File “p1.py”, line 13, in test_negativeForLess
self.assertLess(first, second, message)
AssertionError: 5 not less than 5 : first value is not less that second value.
———————————————————————-
Ran 1 test in 0.000s
FAILED (failures=1)
示例 2:如果第一个给定值小于第二个值
Python
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# positive test function to test if
# values are almost equal with place
def test_positiveForLess(self):
first = 2
second = 3
# error message in case if test case got failed
message = "first value is not less that second value."
# assert function() to check if values1 is
# less than value2
self.assertLess(first, second, message)
# main for function call
if __name__ == '__main__':
unittest.main()
输出:
.
———————————————————————-
Ran 1 test in 0.000s
OK