test AssertionError with None
I want to use the assertIsNotNone and assertIsNone methods to test None in test_assertion_error_w_none.
preview
test_assertion_error_w_none looks like this by the end of the chapter
54 def test_assertion_error_w_none(self):
55 assert None is None
56 self.assertIs(None, None)
57 self.assertIsNone(None)
58
59 assert_is_not_none(False)
60 self.assertIsNot(False, None)
61 self.assertIsNotNone(False)
62
63 assert_is_not_none(True)
64 self.assertIsNot(True, None)
65 self.assertIsNotNone(True)
66
67 assert_is_not_none(self.an_integer)
68 self.assertIsNot(self.an_integer, None)
69 self.assertIsNotNone(self.an_integer)
70
71 assert_is_not_none(self.a_float)
72 self.assertIsNot(self.a_float, None)
73 self.assertIsNotNone(self.a_float)
74
75 assert_is_not_none(self.a_string)
76 self.assertIsNot(self.a_string, None)
77 self.assertIsNotNone(self.a_string)
78
79 assert_is_not_none(self.a_tuple)
80 self.assertIsNot(self.a_tuple, None)
81 self.assertIsNotNone(self.a_tuple)
82
83 assert_is_not_none(self.a_list)
84 self.assertIsNot(self.a_list, None)
85 self.assertIsNotNone(self.a_list)
86
87 assert_is_not_none(self.a_set)
88 self.assertIsNot(self.a_set, None)
89 self.assertIsNotNone(self.a_set)
90
91 assert_is_not_none(self.a_dictionary)
92 self.assertIsNot(self.a_dictionary, None)
93 self.assertIsNotNone(self.a_dictionary)
open the project
I open a terminal
I change directory to the assertion_error folder in the
pumping_pythonfoldercd assertion_errorI open
test_assertion_error.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows
tests/test_assertion_error.py ....... [100%] ================== 7 passed in D.EFs ===================
use assertIsNotNone and assertIsNone
I add a call to assertIsNotNone in test_assertion_error_w_none
54 def test_assertion_error_w_none(self): 55 assert None is None 56 self.assertIs(None, None) 57 self.assertIsNotNone(None) 58 59 assert_is_not_none(False)the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly Nonebecause None is None
I change assertIsNotNone to a call to the assertIsNone method
54 def test_assertion_error_w_none(self): 55 assert None is None 56 self.assertIs(None, None) 57 # self.assertIsNotNone(None) 58 self.assertIsNone(None) 59 60 assert_is_not_none(False)the test passes
I add assertIsNone for False
60 assert_is_not_none(False) 61 self.assertIsNot(False, None) 62 self.assertIsNone(False) 63 64 assert_is_not_none(True)the terminal is my friend, and shows AssertionError
AssertionError: False is not NoneI change the call from assertIsNone to assertIsNotNone
60 assert_is_not_none(False) 61 self.assertIsNot(False, None) 62 # self.assertIsNone(False) 63 self.assertIsNotNone(False) 64 65 assert_is_not_none(True)I add assertIsNone for True
65 assert_is_not_none(True) 66 self.assertIsNot(True, None) 67 self.assertIsNone(True) 68 69 assert_is_not_none(self.an_integer)the terminal is my friend, and shows AssertionError
AssertionError: True is not NoneI change the call from assertIsNone to assertIsNotNone
65 assert_is_not_none(True) 66 self.assertIsNot(True, None) 67 # self.assertIsNone(True) 68 self.assertIsNotNone(True) 69 70 assert_is_not_none(self.an_integer)I add assertIsNone for an integer (a whole number without decimals)
70 assert_is_not_none(self.an_integer) 71 self.assertIsNot(self.an_integer, None) 72 self.assertIsNone(self.an_integer) 73 74 assert_is_not_none(self.a_float)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not NoneI change the call from assertIsNone to assertIsNotNone
70 assert_is_not_none(self.an_integer) 71 self.assertIsNot(self.an_integer, None) 72 # self.assertIsNone(self.an_integer) 73 self.assertIsNotNone(self.an_integer) 74 75 assert_is_not_none(self.a_float)I add assertIsNone for a float (binary floating point decimal number)
75 assert_is_not_none(self.a_float) 76 self.assertIsNot(self.a_float, None) 77 self.assertIsNone(self.a_float) 78 79 assert_is_not_none(self.a_string)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not NoneI change the call from assertIsNone to assertIsNotNone
75 assert_is_not_none(self.a_float) 76 self.assertIsNot(self.a_float, None) 77 # self.assertIsNone(self.a_float) 78 self.assertIsNotNone(self.a_float) 79 80 assert_is_not_none(self.a_string)I add assertIsNone for a string (anything in quotes)
80 assert_is_not_none(self.a_string) 81 self.assertIsNot(self.a_string, None) 82 self.assertIsNone(self.a_string) 83 84 assert_is_not_none(self.a_tuple)the terminal is my friend, and shows AssertionError
AssertionError: '' is not NoneI change the call from assertIsNone to assertIsNotNone
80 assert_is_not_none(self.a_string) 81 self.assertIsNot(self.a_string, None) 82 # self.assertIsNone(self.a_string) 83 self.assertIsNotNone(self.a_string) 84 85 assert_is_not_none(self.a_tuple)the test passes because a string (anything in quotes) is not None.
I add assertIsNone for a tuple (anything in parentheses
( )separated by a comma)85 assert_is_not_none(self.a_tuple) 86 self.assertIsNot(self.a_tuple, None) 87 self.assertIsNone(self.a_tuple) 88 89 assert_is_not_none(self.a_list)the terminal is my friend, and shows AssertionError
AssertionError: () is not NoneI change the call from assertIsNone to assertIsNotNone
85 assert_is_not_none(self.a_tuple) 86 self.assertIsNot(self.a_tuple, None) 87 # self.assertIsNone(self.a_tuple) 88 self.assertIsNotNone(self.a_tuple) 89 90 assert_is_not_none(self.a_list)I add assertIsNone for a list (anything in square brackets
[ ])90 assert_is_not_none(self.a_list) 91 self.assertIsNot(self.a_list, None) 92 self.assertIsNone(self.a_list) 93 94 assert_is_not_none(self.a_set)the terminal is my friend, and shows AssertionError
AssertionError: [] is not NoneI change the call from assertIsNone to assertIsNotNone
90 assert_is_not_none(self.a_list) 91 self.assertIsNot(self.a_list, None) 92 # self.assertIsNone(self.a_list) 93 self.assertIsNotNone(self.a_list) 94 95 assert_is_not_none(self.a_set)I add assertIsNone for a set (anything in curly braces
{ }, not key-value pairs)95 assert_is_not_none(self.a_set) 96 self.assertIsNot(self.a_set, None) 97 self.assertIsNone(self.a_set) 98 99 assert_is_not_none(self.a_dictionary)the terminal is my friend, and shows AssertionError
AssertionError: set() is not NoneI change the call from assertIsNone to assertIsNotNone
95 assert_is_not_none(self.a_set) 96 self.assertIsNot(self.a_set, None) 97 # self.assertIsNone(self.a_set) 98 self.assertIsNotNone(self.a_set) 99 100 assert_is_not_none(self.a_dictionary)I add assertIsNone for a dictionary (key-value pairs in curly braces ‘{ }’ separated by commas)
100 assert_is_not_none(self.a_dictionary) 101 self.assertIsNot(self.a_dictionary, None) 102 self.assertIsNone(self.a_dictionary) 103 104 def test_assertion_error_w_false(self):the terminal is my friend, and shows AssertionError
AssertionError: {} is not NoneI change the call from assertIsNone to assertIsNotNone
100 assert_is_not_none(self.a_dictionary) 101 self.assertIsNot(self.a_dictionary, None) 102 # self.assertIsNone(self.a_dictionary) 103 self.assertIsNotNone(self.a_dictionary) 104 105 def test_assertion_error_w_false(self):the test passes because a dictionary is not None.
I remove the commented lines from test_assertion_error_w_none
54 def test_assertion_error_w_none(self): 55 assert None is None 56 self.assertIs(None, None) 57 self.assertIsNone(None) 58 59 assert_is_not_none(False) 60 self.assertIsNot(False, None) 61 self.assertIsNotNone(False) 62 63 assert_is_not_none(True) 64 self.assertIsNot(True, None) 65 self.assertIsNotNone(True)67 assert_is_not_none(self.an_integer) 68 self.assertIsNot(self.an_integer, None) 69 self.assertIsNotNone(self.an_integer) 70 71 assert_is_not_none(self.a_float) 72 self.assertIsNot(self.a_float, None) 73 self.assertIsNotNone(self.a_float) 74 75 assert_is_not_none(self.a_string) 76 self.assertIsNot(self.a_string, None) 77 self.assertIsNotNone(self.a_string)79 assert_is_not_none(self.a_tuple) 80 self.assertIsNot(self.a_tuple, None) 81 self.assertIsNotNone(self.a_tuple) 82 83 assert_is_not_none(self.a_list) 84 self.assertIsNot(self.a_list, None) 85 self.assertIsNotNone(self.a_list) 86 87 assert_is_not_none(self.a_set) 88 self.assertIsNot(self.a_set, None) 89 self.assertIsNotNone(self.a_set) 90 91 assert_is_not_none(self.a_dictionary) 92 self.assertIsNot(self.a_dictionary, None) 93 self.assertIsNotNone(self.a_dictionary) 94 95 def test_assertion_error_w_false(self):I open a new terminal then change directories to
assertion_errorcd assertion_errorI add a git commit message in the new terminal
git commit -am \ 'use assertIsNotNone and assertIsNone'
I can use assertIsNotNone and assertIsNone for assertIsNot(x, None) and assertIs(x, None).
close the project
I close
test_assertion_error.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
assertion_errorcd ..the terminal is my friend, and shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I can use assertIsNone methods and assertIsNotNone for assertions that test if something is None or not - assertIs(x, None) and assertIsNot(x, None).
code from the chapter
what is next?
Would you like to use the assertIsNotNone and assertIsNone methods with the functions project?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.