Python unittest – assertNotIn() function
assertNotIn() in Python is a unittest library function that is used in unit testing to check whether a string is not contained in other. This function will take three string parameters as input and return a boolean value depending upon the assert condition. If the key is not contained in container string it will return true else it returns false.
Syntax: assertNotIn(key, container, message)
Parameters: assertNotIn() accept three parameters which are listed below with explanation:
- key: a string whose presence is checked in the given container
- container: a string in which key string is searched
- 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 ):
key = "geeks"
container = "geeksforgeeks"
message = "key is present in container."
self .assertNotIn(key, container, message)
if __name__ = = '__main__' :
unittest.main()
|
Output:
F
======================================================================
FAIL: test_negative (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/e99e85838dde0c8ef29ab17fef478979.py", line 12, in test_negative
self.assertNotIn(key, container, message)
AssertionError: 'geeks' unexpectedly found in 'geeksforgeeks' : key is present in container.
----------------------------------------------------------------------
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 ):
key = "gfgs"
container = "geeksforgeeks"
message = "key is present in container."
self .assertNotIn(key, container, message)
if __name__ = = '__main__' :
unittest.main()
|
Output:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Reference: https://docs.python.org/3/library/unittest.html
Last Updated :
29 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...