Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python unittest – assertIsNone() function

  • Last Updated : 29 Aug, 2020

assertIsNone() in Python is a unittest library function that is used in unit testing to check that input value is None or not. This function will take two parameters as input and return a boolean value depending upon assert condition. If input value is equal to None assertIsNone() will return true else return false.

Syntax: assertIsNone(testValue, message)

Parameters: assertIsNone() accept two parameters which are listed below with explanation:

  • testValue:  test variable as the input value to check equality with None
  • 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 TestMethods(unittest.TestCase):
    # test function 
    def test_negative(self):
        firstValue = "geeks"
        # error message in case if test case got failed
        message = "Test value is not none."
        # assertIsNone() to check that if input value is none
        self.assertIsNone(firstValue, message)
  
if __name__ == '__main__':
    unittest.main()

Output:

F
======================================================================
FAIL: test_negative (__main__.TestMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "p1.py", line 14, in test_negative
    self.assertIsNone(firstValue, message)
AssertionError: 'geeks' is not None : Test value is not none.

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

FAILED (failures=1)


Example 2: Positive Test case

Python3




# unit test case
import unittest
  
  
class TestMethods(unittest.TestCase):
    # test function 
    def test_positive(self):
        firstValue = None
        # error message in case if test case got failed
        message = "Test value is not none."
        # assertIsNone() to check that if input value is none
        self.assertIsNone(firstValue, message)
  
if __name__ == '__main__':
    unittest.main()

Output:

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

OK


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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!