Python unittest – assertIsInstance() function

29 Aug, 2020
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
Python3
# test suiteimport unittest  # test class    class Myclass:    x = 5    class Myclass2:    x = 6    class TestClass(unittest.TestCase):    # test function to test whether obj is instance of class    def test_negative(self):        objectName = Myclass()        # error message in case if test case got failed        message = "given object is not instance of Myclass."        # assertIsInstance() to check if obj is instance of class        self.assertIsInstance(objectName, Myclass2, message)    if __name__ == '__main__':    unittest.main()
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
Python3
# test suiteimport unittest  # test classclass Myclass:    x = 5    class TestClass(unittest.TestCase):    # test function to test whether obj is instance of class    def test_positive(self):        objectName = Myclass()        # error message in case if test case got failed        message = "given object is not instance of Myclass."        # assertIsInstance() to check if obj is instance of class        self.assertIsInstance(objectName, Myclass, message)    if __name__ == '__main__':    unittest.main()
Output:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Reference: https://docs.python.org/3/library/unittest.html
Python unittest-library
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Python | os.path.join() method
Create a Pandas DataFrame from Lists