Summary: in this tutorial, you’ll learn how to use the Python assertIsNone()
method to test if an expression is None
.
Introduction to the Python assertIsNone() method #
The
is a method of the assertIsNone()
TestCase
class of the unittest module. The
test if an expression is None:assertIsNone()
assertIsNone(expr, msg=None)
Code language: Python (python)
If the expr
is None
, the test passes. Otherwise, the test will fail.
The msg
is optional. It’ll be displayed in the test result if the test fails.
Python assertIsNone() method examples #
Let’s take some examples of using the assertIsNone()
method.
1) Using assertIsNone() with a success case #
The following example uses the assertIsNone()
to test if the message variable is None:
import unittest
class TestNone(unittest.TestCase):
def test_variable_none(self):
message = None
self.assertIsNone(message)
Code language: Python (python)
Run the test:
python -m unittest -v
Code language: Python (python)
Output:
test_variable_none (test_none.TestNone) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
Code language: plaintext (plaintext)
2) Using assertIsNone() with a failed case #
The following example uses the assertIsNone()
method to test if the message
variable is None:
import unittest
class TestNone(unittest.TestCase):
def test_variable_not_none(self):
message = 'Hello'
self.assertIsNone(message)
Code language: Python (python)
Run the test:
python -m unittest -v
Code language: Python (python)
Output:
test_variable_not_none (test_none.TestNone) ... FAIL
======================================================================
FAIL: test_variable_not_none (test_none.TestNone)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\python-unit-testing\test_none.py", line 7, in test_variable_not_none
self.assertIsNone(message)
AssertionError: 'Hello' is not None
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Code language: plaintext (plaintext)
Since the message is 'Hello'
, it is not None. Therefore, the test failed.
3) Using assertIsNone() with a failed case with a message #
The following example uses the assertIsNone()
to test if the message variable is None. Also, we show a message when the test fails:
import unittest
class TestNone(unittest.TestCase):
def test_variable_not_none(self):
message = 'Hello'
self.assertIsNone(
message,
f'The message is "{message}" so it is not None.'
)
Code language: Python (python)
Run the test:
python -m unittest -v
Code language: Python (python)
Output:
test_variable_not_none (test_none.TestNone) ... FAIL
======================================================================
FAIL: test_variable_not_none (test_none.TestNone)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\python-unit-testing\test_none.py", line 7, in test_variable_not_none
self.assertIsNone(
AssertionError: 'Hello' is not None : The message is "Hello" so it is not None.
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Code language: plaintext (plaintext)
Python assertIsNotNone() method #
The
is opposite of the assertIsNotNone()
assertIsNone()
method. The
method tests if a variable is not None.assertIsNotNone()
assertIsNotNone(expr, msg=None)
Code language: Python (python)
The test passes if the expr
is not None
or fails otherwise. For example:
import unittest
class TestNone(unittest.TestCase):
def test_variable_is_not_none(self):
message = 'Bye'
self.assertIsNotNone(message)
Code language: Python (python)
Run the test:
python -m unittest -v
Code language: Python (python)
Output:
test_variable_is_not_none (test_not_none.TestNone) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Code language: plaintext (plaintext)
Summary #
- Use the
assertIsNone()
method to test if a variable isNone
. - use the
assertIsNotNone()
method to test if a variable is notNone
.