Thursday, October 2, 2025
HomeLanguagesPython unittest – assertIn() function

Python unittest – assertIn() function

assertIn() in Python is a unittest library function that is used in unit testing to check whether a string is contained in other 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:

  • 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




# 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 = "neveropen"
        # 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()


Output:

F
======================================================================
FAIL: test_negative (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/e920f2cd9195a3fd72bd531f7f101754.py", line 12, in test_negative
    self.assertIn(key, container, message)
AssertionError: 'gfg' not found in 'neveropen' : key is not in container.

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Example 2: Positive Test case

Python3




# test suite
import unittest
  
  
class TestStringMethods(unittest.TestCase):
    # test function to test whether key is present in container
    def test_positive(self):
        key = "Lazyroar"
        container = "neveropen"
        # 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()


Output:

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Reference: https://docs.python.org/3/library/unittest.html

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7079 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS