20C23027 Prac7
20C23027 Prac7
assertIn() in Python is a unittest library function that is used in unit testing to check whether a
string is contained or not. This function will take three string parameters as input and return a
Boolean value depending upon the assert condition. If the key is contained in container string
it will return true else it returns false.
Syntax: assertIn(key, container, message)
Parameters: assertIn() accept three parameters which are listed below with explanation:
Python code :
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# test function to test whether key is present in container
def test_negative(self):
key = "gfg"
container = "geeksforgeeks"
# error message in case if test case got failed
message = "key is not in container."
# assertIn() to check if key is in container
self.assertIn(key, container, message)
if __name__ == '__main__':
unittest.main()
# test suite
import unittest
class TestStringMethods(unittest.TestCase):
# test function to test whether key is present in container
def test_positive(self):
key = "geeks"
container = "geeksforgeeks"
# error message in case if test case got failed
message = "key is not in container."
# assertIn() to check if key is in container
self.assertIn(key, container, message)
if __name__ == '__main__':
unittest.main()