Python中的assertNotEqual()是單元測試庫函數,用於單元測試中以檢查兩個值的不相等性。此函數將使用三個參數作為輸入,並根據斷言條件返回布爾值。如果兩個輸入值都不相等,則assertNotEqual()將返回true,否則返回false。
用法:
assertNotEqual(firstValue, secondValue, message)
參數:assertNotEqual()接受以下說明的三個參數:
- firstValue:按函數比較中使用的任何類型的變量
- secondValue:按函數比較中使用的任何類型的變量
- message:測試用例失敗時顯示的字符串句子作為消息。
下麵列出了兩個不同的示例,它們說明了給定assert函數的正麵和負麵測試案例:
示例1:否定測試用例
Python3
# unit test case
import unittest
class TestStringMethods(unittest.TestCase):
# test function to test equality of two value
def test_negative(self):
firstValue = "geeks"
secondValue = "geeksg"
# error message in case if test case got failed
message = "First value and second value are not unequal !"
# assertNotEqual() to check unequality of first & second value
self.assertNotEqual(firstValue, secondValue, message)
if __name__ == '__main__':
unittest.main()
輸出:
F ====================================================================== FAIL:test_negative (__main__.TestStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File "p1.py", line 12, in test_negative self.assertNotEqual(firstValue, secondValue, message) AssertionError:'geeks' == 'geeks':First value and second value are not unequal! ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1)
示例2:正測試用例
Python3
# unit test case
import unittest
class TestStringMethods(unittest.TestCase):
# test function to test equality of two value
def test_positive(self):
firstValue = "geeks"
secondValue = "geeks"
# error message in case if test case got failed
message = "First value and second value are not unequal !"
# assertNotEqual() to check equality of first & second value
self.assertNotEqual(firstValue, secondValue, message)
if __name__ == '__main__':
unittest.main()
輸出:
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
參考:https://docs.python.org/3/library/unittest.html
相關用法
- Python unittest assertNotIn()用法及代碼示例
- 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 assertLessEqual()用法及代碼示例
- Python unittest assertGreaterEqual()用法及代碼示例
- Python unittest assertGreater()用法及代碼示例
- Python unittest assertTrue()用法及代碼示例
- Python unittest assertLess()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam.Pradhan大神的英文原創作品 Python unittest – assertNotEqual() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。