assertNotAlmostEqualPython中的()是unittest庫函數,用於單元測試中以檢查兩個給定值是否不是約。該函數將使用五個參數作為輸入,並根據斷言條件返回布爾值。
該函數通過計算差值,四舍五入到給定的小數位數(默認為7),然後與零進行比較,以檢查第一和第二個近似不相等。
如果提供了增量而不是位置,則第一和第二之間的差異必須小於或等於增量。
用法:
assertNotAlmostEqual(第一,第二,地點= 7,消息=無,增量=無)
參數:assertNotAlmostEqual()接受以下說明的三個參數:
- first:第一個輸入值(整數)
- second:第二個輸入值(整數)
- places:近似的小數位
- message:作為測試消息失敗時顯示的消息的字符串語句。
- delta:近似的增量值
下麵列出了兩個不同的示例,它們說明了給定assert函數的正麵和負麵測試案例:
例:
Python3
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# negative test function to test if values are almost not-equal with place
def test_negativeWithPlaces(self):
first = 4.4555
second = 4.4566
decimalPlace = 2
# error message in case if test case got failed
message = "first and second are almost equal."
# assert function() to check if values are almost not-equal
self.assertNotAlmostEqual(first, second, decimalPlace, message)
# positive test function to test if values are almost not-equal with place
def test_positiveWithPlaces(self):
first = 4.4555
second = 4.4566
decimalPlace = 3
# error message in case if test case got failed
message = "first and second are almost equal."
# assert function() to check if values are almost not-equal
self.assertNotAlmostEqual(first, second, decimalPlace, message)
# negative test function to test if values are almost not-equal with delta
def test_negativeWithDelta(self):
first = 4.4555
second = 4.4566
delta = 0.002
# error message in case if test case got failed
message = "first and second are almost equal."
# assert function() to check if values are almost equal
self.assertNotAlmostEqual(first, second, None, message, delta)
# positive test function to test if values are almost not-equal with delta
def test_positiveWithDelta(self):
first = 4.4555
second = 4.4566
delta = 0.0001
# error message in case if test case got failed
message = "first and second are almost equal."
# assert function() to check if values are almost not-equal
self.assertNotAlmostEqual(first, second, None, message, delta)
if __name__ == '__main__':
unittest.main()
輸出:
FF.. ====================================================================== FAIL:test_negativeWithDelta (__main__.TestStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/ebb259873a4edc722a83da1a694e0ce1.py", line 34, in test_negativeWithDelta self.assertNotAlmostEqual(first, second, None, message, delta) AssertionError:4.4555 == 4.4566 within 0.002 delta:first and second are almost equal. ====================================================================== FAIL:test_negativeWithPlaces (__main__.TestStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/ebb259873a4edc722a83da1a694e0ce1.py", line 14, in test_negativeWithPlaces self.assertNotAlmostEqual(first, second, decimalPlace, message) AssertionError:4.4555 == 4.4566 within 2 places:first and second are 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 assertAlmostEqual()用法及代碼示例
- Python unittest assertLessEqual()用法及代碼示例
- Python unittest assertGreaterEqual()用法及代碼示例
- Python unittest assertGreater()用法及代碼示例
- Python unittest assertTrue()用法及代碼示例
- Python unittest assertLess()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam.Pradhan大神的英文原創作品 Python unittest – assertNotAlmostEqual() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。