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_pythonfolderI open
test_assertion_error.pyI use pytest-watcher to run the tests automatically
the 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
the terminal is my friend, and shows AssertionError
because None is None
I change assertIsNotNone to a call to the assertIsNone method
the test passes
I add assertIsNone for False
the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for True
the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for an integer (a whole number without decimals)
the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for a float (binary floating point decimal number)
the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for a string (anything in quotes)
the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
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)the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for a list (anything in square brackets
[ ])the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for a set (anything in curly braces
{ }, not key-value pairs)the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
I add assertIsNone for a dictionary (key-value pairs in curly braces ‘{ }’ separated by commas)
the terminal is my friend, and shows AssertionError
I change the call from assertIsNone to assertIsNotNone
the test passes because a dictionary is not None.
I remove the commented lines from test_assertion_error_w_none
I open a new terminal then change directories to
assertion_error
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_errorthe 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.