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
相關用法
- 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 assertNotAlmostEqual()用法及代碼示例
- Python unittest assertLessEqual()用法及代碼示例
- Python unittest assertGreaterEqual()用法及代碼示例
- Python unittest assertGreater()用法及代碼示例
- Python unittest assertTrue()用法及代碼示例
- Python unittest assertLess()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam.Pradhan大神的英文原創作品 Python unittest – assertAlmostEqual() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。