assertNotIsInstance() in Python is a unittest library function that is used in unit testing to check whether an object is not 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 not an instance of the given class it will return true else it returns false.
Syntax: assertIsInstance(object, className, message)
Parameters: assertNotIsInstance() 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 suite import unittest # test class class Myclass: x = 5 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 instance of Myclass." # assertIsInstance() to check if obj is instance of class self .assertNotIsInstance(objectName, Myclass, message) if __name__ = = '__main__' : unittest.main() |
Output:
F ====================================================================== FAIL: test_negative (__main__.TestClass) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/73feafa72d632b19f11ac8251bb291d7.py", line 17, in test_negative self.assertNotIsInstance(objectName, Myclass, message) AssertionError: <__main__.Myclass object at 0x7fab0d3affd0> is an instance of <class '__main__.Myclass'> : given object is instance of Myclass. ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
Example 2: Positive Test case
Python3
# test suite import unittest # test class class Myclass: x = 5 class Myclass2: x = 5 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 instance of Myclass." # assert function() to check if obj is instance of class self .assertNotIsInstance(objectName, Myclass2, message) if __name__ = = '__main__' : unittest.main() |
Output:
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
Reference: https://docs.python.org/3/library/unittest.html
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.