Open In App

Python unittest - assertIsInstance() function

Last Updated : 15 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

assertIsInstance() in Python is a unittest library function that is used in unit testing to check whether an object is an instance of a given class or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. If the object is an instance ofthe given class it will return true else it returns false.

Syntax: assertIsInstance(object, className, message)

Parameters: assertIsInstance() accept three parameters which are listed below with explanation:

  • object:  Object which is checked as an instance of the given class
  • className: Class name to be compared for object instance
  • 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

Output:

F
======================================================================
FAIL: test_negative (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/a2e9f97d79f7d8c1fbd00c4df704d402.py", line 22, in test_negative
    self.assertIsInstance(objectName, Myclass2, message)
AssertionError: <__main__.Myclass object at 0x7f1e9bead0b8> is not an instance of <class '__main__.Myclass2'> : given object is not instance of Myclass.

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

FAILED (failures=1)

Example 2: Positive Test case

Output:

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

OK

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


Next Article
Practice Tags :

Similar Reads