Python unittest – assertNotEqual() function
Last Updated :
29 Aug, 2020
assertNotEqual() in Python is a unittest library function that is used in unit testing to check the inequality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are unequal assertNotEqual() will return true else return false.
Syntax:
assertNotEqual(firstValue, secondValue, message)
Parameters: assertNotEqual() accept three parameters which are listed below with explanation:
- firstValue: variable of any type which is used in the comparison by function
- secondValue: variable of any type which is used in the comparison by function
- message: a string sentence as a message which got displayed when the test case got failed.
Listed below are two different examples illustrating the positive and negative test case for given assert function:
Example 1: Negative Test case
Python3
import unittest
class TestStringMethods(unittest.TestCase):
def test_negative( self ):
firstValue = "geeks"
secondValue = "geeksg"
message = "First value and second value are not unequal !"
self .assertNotEqual(firstValue, secondValue, message)
if __name__ = = '__main__' :
unittest.main()
|
Output:
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)
Example 2: Positive Test case
Python3
import unittest
class TestStringMethods(unittest.TestCase):
def test_positive( self ):
firstValue = "geeks"
secondValue = "geeks"
message = "First value and second value are not unequal !"
self .assertNotEqual(firstValue, secondValue, message)
if __name__ = = '__main__' :
unittest.main()
|
Output:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Reference: https://docs.python.org/3/library/unittest.html
Share your thoughts in the comments
Please Login to comment...