Open In App

Python unittest – assertIs() function

Improve
Improve
Like Article
Like
Save
Share
Report

assertIs() in Python is a unittest library function that is used in unit testing to test whether first and second input value evaluates to the same object or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input evaluates to the same object then assertIs() will return true else return false.

Syntax: assertIs(firstValue, secondValue, message)

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

  • firstValue  variable of any type which is used in the comparison by function
  • secondValue: variable of any type which is used in the comparison by function
  • 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




# unit test case
import unittest
  
class DummyClass:
  x = 5
  
class TestMethods(unittest.TestCase):
    # test function to test object equality of two value
    def test_negative(self):
        firstValue = DummyClass()
        secondValue = DummyClass()
        # error message in case if test case got failed
        message = "First value & second value are not evaluated to same object !"
        # assertIs() to check that if first & second evaluated to same object
        self.assertIs(firstValue, secondValue, message)
  
if __name__ == '__main__':
    unittest.main()


Output:

F
======================================================================
FAIL: test_negative (__main__.TestMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "p1.py", line 15, in test_negative
    self.assertIs(firstValue, secondValue, message)
AssertionError: <__main__.DummyClass object at 0x7f1d20251b70> is 
not <__main__.DummyClass object at 0x7f1d20251ba8> :
 First value and second value are not evaluated to same object!

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

FAILED (failures=1)


Example 2: Positive Test case

Python3




# unit test case
import unittest
  
class DummyClass:
  x = 5
  
class TestMethods(unittest.TestCase):
    # test function to test object equality of two value
    def test_positive(self):
        firstValue = DummyClass()
        secondValue = firstValue
        # error message in case if test case got failed
        message = "First value and second value are not evaluated to same object !"
        # assertIs() to check that if first & second evaluated to same object
        self.assertIs(firstValue, secondValue, message)
  
if __name__ == '__main__':
    unittest.main()


Output:

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

OK


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



Last Updated : 29 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads