AssertionError 3: use assertIsNotNone and assertIsNone
I used the assertIsNotNone and assertIsNone methods to test None. I want to use them to replace assertIs and assertIsNot in test_assertion_error_w_none.
preview
I have these tests by the end of the chapter
continue the project
Make sure you are in the
pumping_pythonfolder with pwd in the terminalpwdif the terminal does not show
.../pumping_pythonchange directory to the
pumping_pythonfolderOnce in
pumping_python, change directory to the projectcd assertion_errorthe terminal shows
.../pumping_python/assertion_errorI run the tests with pytest-watcher
uv run pytest-watcher . --nowthe terminal is my friend, and shows
rootdir: .../pumping_python/assertion_error configfile: pyproject.toml collected 6 items tests/test_assertion_error.py ...... [100%] ================== 6 passed in A.BCs ===================I hold ctrl (Windows) or option (MacOS) on the keyboard, then click on
tests/test_assertion_error.pywith the mouse to open it
use assertIsNotNone and assertIsNone
I add a call to assertIsNotNone in test_assertion_error_w_none
30 def test_assertion_error_w_none(self): 31 assert None is None 32 self.assertIs(None, None) 33 self.assertIsNotNone(None) 34 35 assert False is not None 36 self.assertIsNot(False, None)the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly Nonebecause None is None
I change assertIsNotNone to a call to the assertIsNone method
30 def test_assertion_error_w_none(self): 31 assert None is None 32 self.assertIs(None, None) 33 # self.assertIsNotNone(None) 34 self.assertIsNone(None) 35 36 assert False is not None 37 self.assertIsNot(False, None)the test passes
I add assertIsNone for False
36 assert False is not None 37 self.assertIsNot(False, None) 38 self.assertIsNone(False) 39 40 assert True is not None 41 self.assertIsNot(True, None)the terminal is my friend, and shows AssertionError
AssertionError: False is not NoneI change the call from assertIsNone to assertIsNotNone
36 assert False is not None 37 self.assertIsNot(False, None) 38 # self.assertIsNone(False) 39 self.assertIsNotNone(False) 40 41 assert True is not None 42 self.assertIsNot(True, None)I add assertIsNone for True
41 assert True is not None 42 self.assertIsNot(True, None) 43 self.assertIsNone(True) 44 45 assert self.an_integer is not None 46 self.assertIsNot(self.an_integer, None)the terminal is my friend, and shows AssertionError
AssertionError: True is not NoneI change the call from assertIsNone to assertIsNotNone
41 assert True is not None 42 self.assertIsNot(True, None) 43 # self.assertIsNone(True) 44 self.assertIsNotNone(True) 45 46 assert self.an_integer is not None 47 self.assertIsNot(self.an_integer, None)I add assertIsNone for an integer (a whole number without decimals)
46 assert self.an_integer is not None 47 self.assertIsNot(self.an_integer, None) 48 self.assertIsNone(self.an_integer) 49 50 assert self.a_float is not None 51 self.assertIsNot(self.a_float, None)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not NoneI change the call from assertIsNone to assertIsNotNone
46 assert self.an_integer is not None 47 self.assertIsNot(self.an_integer, None) 48 # self.assertIsNone(self.an_integer) 49 self.assertIsNotNone(self.an_integer) 50 51 assert self.a_float is not None 52 self.assertIsNot(self.a_float, None)the test passes because an integer (a whole number without decimals) is not None.
I add assertIsNone for a float (binary floating point decimal number)
51 assert self.a_float is not None 52 self.assertIsNot(self.a_float, None) 53 self.assertIsNone(self.a_float) 54 55 assert self.a_string is not None 56 self.assertIsNot(self.a_string, None)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not NoneI change the call from assertIsNone to assertIsNotNone
51 assert self.a_float is not None 52 self.assertIsNot(self.a_float, None) 53 # self.assertIsNone(self.a_float) 54 self.assertIsNotNone(self.a_float) 55 56 assert self.a_string is not None 57 self.assertIsNot(self.a_string, None)the test passes because a float (binary floating point decimal number) is not None.
I add assertIsNone for a string (anything in quotes)
56 assert self.a_string is not None 57 self.assertIsNot(self.a_string, None) 58 self.assertIsNone(self.a_string) 59 60 assert self.a_tuple is not None 61 self.assertIsNot(self.a_tuple, None)the terminal is my friend, and shows AssertionError
AssertionError: 'a string' is not NoneI change the call from assertIsNone to assertIsNotNone
56 assert self.a_string is not None 57 self.assertIsNot(self.a_string, None) 58 # self.assertIsNone(self.a_string) 59 self.assertIsNotNone(self.a_string) 60 61 assert self.a_tuple is not None 62 self.assertIsNot(self.a_tuple, None)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)61 assert self.a_tuple is not None 62 self.assertIsNot(self.a_tuple, None) 63 self.assertIsNone(self.a_tuple) 64 65 assert self.a_list is not None 66 self.assertIsNot(self.a_list, None)the terminal is my friend, and shows AssertionError
AssertionError: (1, 2, 3, 'n') is not NoneI change the call from assertIsNone to assertIsNotNone
61 assert self.a_tuple is not None 62 self.assertIsNot(self.a_tuple, None) 63 # self.assertIsNone(self.a_tuple) 64 self.assertIsNotNone(self.a_tuple) 65 66 assert self.a_list is not None 67 self.assertIsNot(self.a_list, None)the test passes because a tuple (anything in parentheses
( )separated by a comma) is not None.I add assertIsNone for a list (anything in square brackets ‘[ ]’)
66 assert self.a_list is not None 67 self.assertIsNot(self.a_list, None) 68 self.assertIsNone(self.a_list) 69 70 assert self.a_set is not None 71 self.assertIsNot(self.a_set, None)the terminal is my friend, and shows AssertionError
AssertionError: [1, 2, 3, 'n'] is not NoneI change the call from assertIsNone to assertIsNotNone
66 assert self.a_list is not None 67 self.assertIsNot(self.a_list, None) 68 # self.assertIsNone(self.a_list) 69 self.assertIsNotNone(self.a_list) 70 71 assert self.a_set is not None 72 self.assertIsNot(self.a_set, None)the test passes because a list (anything in square brackets ‘[ ]’) is not None.
I add assertIsNone for a set (anything in curly braces
{ }, not key-value pairs)71 assert self.a_set is not None 72 self.assertIsNot(self.a_set, None) 73 self.assertIsNone(self.a_set) 74 75 assert self.a_dictionary is not None 76 self.assertIsNot(self.a_dictionary, None)the terminal is my friend, and shows AssertionError
AssertionError: {1, 2, 3, 'n'} is not NoneI change the call from assertIsNone to assertIsNotNone
71 assert self.a_set is not None 72 self.assertIsNot(self.a_set, None) 73 # self.assertIsNone(self.a_set) 74 self.assertIsNotNone(self.a_set) 75 76 assert self.a_dictionary is not None 77 self.assertIsNot(self.a_dictionary, None)the test passes because a set (anything in curly braces
{ }, not key-value pairs) is not None.I add assertIsNone for a dictionary (key-value pairs in curly braces ‘{ }’ separated by a comma)
76 assert self.a_dictionary is not None 77 self.assertIsNot(self.a_dictionary, None) 78 self.assertisNone(self.a_dictionary) 79 80 def test_assertion_error_w_false(self):the terminal is my friend, and shows AssertionError
AssertionError: {1, 2, 3, 'n'} is not NoneI change the call from assertIsNone to assertIsNotNone
76 assert self.a_dictionary is not None 77 self.assertIsNot(self.a_dictionary, None) 78 # self.assertisNone(self.a_dictionary) 79 self.assertIsNotNone(self.a_dictionary) 80 81 def test_assertion_error_w_false(self):the test passes because a dictionary (key-value pairs in curly braces ‘{ }’ separated by a comma) is not None.
I remove the commented lines and assertions that use assertIsNot, assertIs, and basic assert statements from test_assertion_error_w_none
30 def test_assertion_error_w_none(self): 31 self.assertIsNone(None) 32 self.assertIsNot(False, None) 33 self.assertIsNotNone(False) 34 self.assertIsNotNone(True) 35 self.assertIsNotNone(self.an_integer) 36 self.assertIsNotNone(self.a_float) 37 self.assertIsNotNone(self.a_string) 38 self.assertIsNotNone(self.a_tuple) 39 self.assertIsNotNone(self.a_list) 40 self.assertIsNotNone(self.a_set) 41 self.assertIsNotNone(self.a_dictionary) 42 43 def test_assertion_error_w_false(self):I add a git commit message in the other terminal
git commit -am \ 'use assertIsNotNone and assertIsNone'the terminal shows a summary of the changes then goes back to the command line.
I can use assertIsNotNone and assertIsNone instead of 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 assertIsNotNone and assertIsNone methods to remove repetition of None from assertions that test if something is None or not - assertIs(x, None) and assertIsNot(x, None).
code from the chapter
what is next?
I know
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.