Python中的assertLessEqual()是單元測試庫函數,用於單元測試中以檢查第一個給定值是否小於或等於第二個值。此函數將使用三個參數作為輸入,並根據斷言條件返回布爾值。
此函數檢查第一個給定值是否小於或等於第二個值,如果是,則返回true,否則,如果第一個值不小於或等於第二個值,則返回false。
用法:assertLessEqual(first, second, message=None)
參數:assertLessEqual()接受以下三個參數的說明:
- first:第一個輸入值(整數)
- second:第二個輸入值(整數)
- message:作為測試消息失敗時顯示的消息的字符串語句。
下麵列出的示例說明給定的assert函數的正負測試用例:
範例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()
輸出:
F
======================================================================
FAIL:test_negativeForLessEqual (__main__.TestStringMethods)
———————————————————————-
Traceback (most recent call last):
File “p1.py”, line 13, in test_negativeForLessEqual
self.assertLessEqual(first, second, message)
AssertionError:6 not less than or equal to 5:first value is not less than or equal to second value.———————————————————————-
Ran 1 test in 0.000sFAILED (failures=1)
範例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()
輸出:
.
———————————————————————-
Ran 1 test in 0.000sOK
相關用法
- Python unittest assertNotIn()用法及代碼示例
- Python unittest assertNotEqual()用法及代碼示例
- Python unittest assertFalse()用法及代碼示例
- Python unittest assertIsNot()用法及代碼示例
- Python unittest assertIs()用法及代碼示例
- Python unittest assertIsNone()用法及代碼示例
- Python unittest assertIsNone()用法及代碼示例
- Python unittest assertIsNotNone()用法及代碼示例
- Python unittest assertEqual()用法及代碼示例
- Python unittest assertIn()用法及代碼示例
- Python unittest assertNotIsInstance()用法及代碼示例
- Python unittest assertIsInstance()用法及代碼示例
- Python unittest assertAlmostEqual()用法及代碼示例
- Python unittest assertNotAlmostEqual()用法及代碼示例
- Python unittest assertGreaterEqual()用法及代碼示例
- Python unittest assertGreater()用法及代碼示例
- Python unittest assertTrue()用法及代碼示例
- Python unittest assertLess()用法及代碼示例
- Python Wand function()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam.Pradhan大神的英文原創作品 Python – assertLessEqual() function in unittest。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。