test functions with assertIsNotNone and assertIsNone
I want to use the assertIsNotNone and assertIsNone methods for the assertions that check if the result of a function call is None.
preview
I have these tests by the end of the chapter
1import src.functions
2import unittest
3
4
5class TestFunctions(unittest.TestCase):
6
7 first = 'first'
8 last = 'last'
9 a_tuple = (0, 1, 2, 'n')
10 a_list = [0, 1, 2, 'n']
11 a_set = {0, 1, 2, 'n'}
12 a_dictionary = {'key': 'value'}
13
14 def test_making_a_function_w_pass(self):
15 self.assertIsNone(src.functions.w_pass())
16
17 def test_making_a_function_w_return(self):
18 self.assertIsNone(src.functions.w_return())
19
20 def test_making_a_function_w_return_none(self):
21 self.assertIsNone(
22 src.functions.w_return_none()
23 )
24
25 def test_what_happens_after_functions_return(self):
26 self.assertIsNone(
27 src.functions.return_leaves_the_function()
28 )
29
30 def test_constant_function(self):
31 result = src.functions.constant()
32 expectation = 'the same thing'
33
34 assert result == expectation
35 self.assertEqual(result, expectation)
36
37 def test_identity_function(self):
38 self.assertIsNone(
39 src.functions.identity(None)
40 )
41
42 result = src.functions.identity(object)
43
44 assert result == object
45 self.assertEqual(result, object)
46
47 def test_why_use_a_function(self):
48 def add_x(number):
49 return 3 + number
50
51 result = add_x(0)
52 expectation = 3
53 assert result == expectation
54 self.assertEqual(result, expectation)
55
56 result = add_x(1)
57 expectation = 4
58 assert result == expectation
59 self.assertEqual(result, expectation)
60
61 result = add_x(2)
62 expectation = 5
63 assert result == expectation
64 self.assertEqual(result, expectation)
65
66 result = add_x(3)
67 expectation = 6
68 assert result == expectation
69 self.assertEqual(result, expectation)
70
71 result = add_x(4)
72 expectation = 7
73 assert result == expectation
74 self.assertEqual(result, expectation)
75
76 result = add_x(5)
77 expectation = 8
78 assert result == expectation
79 self.assertEqual(result, expectation)
80
81 result = add_x(6)
82 expectation = 9
83 assert result == expectation
84 self.assertEqual(result, expectation)
85
86 result = add_x(7)
87 expectation = 10
88 assert result == expectation
89 self.assertEqual(result, expectation)
90
91 result = add_x(8)
92 expectation = 11
93 assert result == expectation
94 self.assertEqual(result, expectation)
95
96 result = add_x(9)
97 expectation = 12
98 assert result == expectation
99 self.assertEqual(result, expectation)
100
101 def test_positional_arguments(self):
102 positional_arguments = (
103 src.functions.positional_arguments
104 )
105
106 reality = positional_arguments(
107 self.first, self.last
108 )
109 my_expectation = (self.first, self.last)
110 assert reality == my_expectation
111 self.assertEqual(reality, my_expectation)
112
113 reality = positional_arguments(
114 self.last, self.first
115 )
116 my_expectation = (self.last, self.first)
117 assert reality == my_expectation
118 self.assertEqual(reality, my_expectation)
119
120 reality = positional_arguments(0, 1)
121 my_expectation = (0, 1)
122 assert reality == my_expectation
123 self.assertEqual(reality, my_expectation)
124
125 reality = positional_arguments(
126 self.a_tuple, self.a_list
127 )
128
129 my_expectation = (self.a_tuple, self.a_list)
130 assert reality == my_expectation
131 self.assertEqual(reality, my_expectation)
132
133 keyword_arguments = (
134 src.functions.keyword_arguments
135 )
136
137 reality = keyword_arguments(
138 self.a_set, self.a_dictionary,
139 )
140 my_expectation = (
141 self.a_set, self.a_dictionary
142 )
143 assert reality == my_expectation
144 self.assertEqual(reality, my_expectation)
145
146 def test_keyword_arguments(self):
147 keyword_arguments = (
148 src.functions.keyword_arguments
149 )
150
151 reality = keyword_arguments(
152 first_input=self.first,
153 last_input=self.last,
154 )
155 my_expectation = (self.first, self.last)
156 assert reality == my_expectation
157 self.assertEqual(reality, my_expectation)
158
159 reality = keyword_arguments(
160 last_input=self.last,
161 first_input=self.first,
162 )
163 my_expectation = (self.first, self.last)
164 assert reality == my_expectation
165 self.assertEqual(reality, my_expectation)
166
167 reality = keyword_arguments(
168 last_input=0, first_input=1,
169 )
170 my_expectation = (1, 0)
171 assert reality == my_expectation
172 self.assertEqual(reality, my_expectation)
173
174 reality = keyword_arguments(
175 first_input=self.a_tuple,
176 last_input=self.a_list,
177 )
178 my_expectation = (self.a_tuple, self.a_list)
179 assert reality == my_expectation
180 self.assertEqual(reality, my_expectation)
181
182 positional_arguments = (
183 src.functions.positional_arguments
184 )
185
186 reality = positional_arguments(
187 last_input=self.a_dictionary,
188 first_input=self.a_set,
189 )
190 my_expectation = (
191 self.a_set, self.a_dictionary
192 )
193 assert reality == my_expectation
194 self.assertEqual(reality, my_expectation)
195
196 def test_args_and_kwargs(self):
197 reality = src.functions.args_and_kwargs(
198 self.first, last_input=self.last,
199 )
200 my_expectation = (self.first, self.last)
201 assert reality == my_expectation
202 self.assertEqual(reality, my_expectation)
203
204 def test_optional_arguments(self):
205 optional_arguments = (
206 src.functions.optional_arguments
207 )
208
209 first_name, last_name = 'jane', 'doe'
210
211 reality = optional_arguments(first_name)
212 my_expectation = (first_name, last_name)
213 assert reality == my_expectation
214 self.assertEqual(reality, my_expectation)
215
216 first_name, blow = 'joe', 'blow'
217
218 reality = optional_arguments(
219 first_name, blow
220 )
221 my_expectation = (first_name, blow)
222 assert reality == my_expectation
223 self.assertEqual(reality, my_expectation)
224
225 first_name = 'john'
226
227 reality = optional_arguments(
228 first_input=first_name
229 )
230 my_expectation = (first_name, last_name)
231 assert reality == my_expectation
232 self.assertEqual(reality, my_expectation)
233
234 last_name = 'smith'
235
236 reality = optional_arguments(
237 last_input=last_name,
238 first_input=first_name,
239 )
240 my_expectation = (first_name, last_name)
241 assert reality == my_expectation
242 self.assertEqual(reality, my_expectation)
243
244 def test_unknown_number_of_arguments(self):
245 unknown_number_of_arguments = (
246 src.functions.unknown_number_of_arguments
247 )
248
249 a_tuple = (0, 1)
250 a_dictionary = {'a': 2, 'b': 3}
251
252 reality = unknown_number_of_arguments(
253 *a_tuple, **a_dictionary
254 )
255 my_expectation = (a_tuple, a_dictionary)
256 assert reality == my_expectation
257 self.assertEqual(reality, my_expectation)
258
259 a_tuple = (0, 1)
260 a_dictionary = {'a': 2, 'b': 3, 'c': 4}
261
262 reality = unknown_number_of_arguments(
263 *a_tuple, **a_dictionary
264 )
265 my_expectation = (a_tuple, a_dictionary)
266 assert reality == my_expectation
267 self.assertEqual(reality, my_expectation)
268
269 a_tuple = (0, 1, 2)
270 a_dictionary = {'a': 3, 'b': 4, 'c': 5}
271
272 reality = unknown_number_of_arguments(
273 *a_tuple, **a_dictionary
274 )
275 my_expectation = (a_tuple, a_dictionary)
276 assert reality == my_expectation
277 self.assertEqual(reality, my_expectation)
278
279 a_tuple = (0, 1, 2, 'n')
280
281 reality = unknown_number_of_arguments(*a_tuple)
282 my_expectation = (a_tuple, {})
283 assert reality == my_expectation
284 self.assertEqual(reality, my_expectation)
285
286 a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
287
288 reality = unknown_number_of_arguments(
289 **a_dictionary
290 )
291 my_expectation = ((), a_dictionary)
292 assert reality == my_expectation
293 self.assertEqual(reality, my_expectation)
294
295 reality = unknown_number_of_arguments()
296 my_expectation = ((), {})
297 assert reality == my_expectation
298 self.assertEqual(reality, my_expectation)
299
300
301# Exceptions seen
302# AssertionError
303# NameError
304# TypeError
305# SyntaxError
306# ModuleNotFoundError
307# AttributeError
open the project
I open a terminal
I change directory to the project
cd functionsthe terminal shows I am in the
functionsfolder.../pumping_python/functionsI open
test_functions.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
test_functions.py ............ [100%] =================== 12 passed in J.KLs ===================
test w_pass with assertIsNone
RED: make it fail
I add a call to assertIsNotNone in test_making_a_function_w_pass
5class TestFunctions(unittest.TestCase):
6
7 first = 'first'
8 last = 'last'
9 a_tuple = (0, 1, 2, 'n')
10 a_list = [0, 1, 2, 'n']
11 a_set = {0, 1, 2, 'n'}
12 a_dictionary = {'key': 'value'}
14 def test_making_a_function_w_pass(self):
15 result = src.functions.w_pass()
16
17 assert result is None
18 self.assertIs(result, None)
19 self.assertIsNotNone(result)
20
21 def test_making_a_function_w_return(self):
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
GREEN: make it pass
I change assertIsNotNone to assertIsNone
14 def test_making_a_function_w_pass(self):
15 result = src.functions.w_pass()
16
17 assert result is None
18 self.assertIs(result, None)
19 # self.assertIsNotNone(result)
20 self.assertIsNone(result)
21
22 def test_making_a_function_w_return(self):
the test passes.
REFACTOR: make it better
I remove the commented line,
assert result is Noneandself.assertIs(result, None)14 def test_making_a_function_w_pass(self): 15 result = src.functions.w_pass() 16 self.assertIsNone(result) 17 18 def test_making_a_function_w_return(self):I no longer need the
resultvariable since it is only used once. I callsrc.functions.w_passdirectly14 def test_making_a_function_w_pass(self): 15 # result = src.functions.w_pass() 16 # self.assertIsNone(result) 17 self.assertIsNone(src.functions.w_pass()) 18 19 def test_making_a_function_w_return(self):the test is still green.
I remove the commented lines
14 def test_making_a_function_w_pass(self): 15 self.assertIsNone(src.functions.w_pass()) 16 17 def test_making_a_function_w_return(self):
test w_return with assertIsNone
RED: make it fail
I add a call to assertIsNotNone in test_making_a_function_w_return
17 def test_making_a_function_w_return(self):
18 result = src.functions.w_return()
19
20 assert result is None
21 self.assertIs(result, None)
22 self.assertIsNotNone(result)
23
24 def test_making_a_function_w_return_none(self):
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
GREEN: make it pass
I change assertIsNotNone to assertIsNone
17 def test_making_a_function_w_return(self):
18 result = src.functions.w_return()
19
20 assert result is None
21 self.assertIs(result, None)
22 # self.assertIsNotNone(result)
23 self.assertIsNone(result)
24
25 def test_making_a_function_w_return_none(self):
the test passes.
REFACTOR: make it better
I remove the commented line,
assert result is Noneandself.assertIs(result, None)17 def test_making_a_function_w_return(self): 18 result = src.functions.w_return() 19 self.assertIsNone(result) 20 21 def test_making_a_function_w_return_none(self):I call
src.functions.w_returndirectly17 def test_making_a_function_w_return(self): 18 # result = src.functions.w_return() 19 # self.assertIsNone(result) 20 self.assertIsNone(src.functions.w_return()) 21 22 def test_making_a_function_w_return_none(self):the test is still green.
I remove the commented lines
17 def test_making_a_function_w_return(self): 18 self.assertIsNone(src.functions.w_return()) 19 20 def test_making_a_function_w_return_none(self):
test w_return_none with assertIsNone
RED: make it fail
I add a call to assertIsNotNone in test_making_a_function_w_return_none
20 def test_making_a_function_w_return_none(self):
21 result = src.functions.w_return_none()
22
23 assert result is None
24 self.assertIs(result, None)
25 self.assertIsNotNone(result)
26
27 def test_what_happens_after_functions_return(self):
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
GREEN: make it pass
I change assertIsNotNone to assertIsNone
20 def test_making_a_function_w_return_none(self):
21 result = src.functions.w_return_none()
22
23 assert result is None
24 self.assertIs(result, None)
25 # self.assertIsNotNone(result)
26 self.assertIsNone(result)
27
28 def test_what_happens_after_functions_return(self):
the test passes.
REFACTOR: make it better
I remove the commented line,
assert result is Noneandself.assertIs(result, None)20 def test_making_a_function_w_return_none(self): 21 result = src.functions.w_return_none() 22 self.assertIsNone(result) 23 24 def test_what_happens_after_functions_return(self):I no longer need the
resultvariable since it is only used once20 def test_making_a_function_w_return_none(self): 21 # result = src.functions.w_return_none() 22 # self.assertIsNone(result) 23 self.assertIsNone( 24 src.functions.w_return_none() 25 ) 26 27 def test_what_happens_after_functions_return(self):the test is still green.
I remove the commented lines
20 def test_making_a_function_w_return_none(self): 21 self.assertIsNone( 22 src.functions.w_return_none() 23 ) 24 25 def test_what_happens_after_functions_return(self):
test return_leaves_the_function with assertIsNone
RED: make it fail
I add a call to assertIsNotNone in test_what_happens_after_functions_return
25 def test_what_happens_after_functions_return(self):
26 result = src.functions.return_leaves_the_function()
27
28 assert result is None
29 self.assertIs(result, None)
30 self.assertIsNotNone(result)
31
32 def test_constant_function(self):
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
GREEN: make it pass
I change assertIsNotNone to assertIsNone
25 def test_what_happens_after_functions_return(self):
26 result = src.functions.return_leaves_the_function()
27
28 assert result is None
29 self.assertIs(result, None)
30 # self.assertIsNotNone(result)
31 self.assertIsNone(result)
32
33 def test_constant_function(self):
the test passes.
REFACTOR: make it better
I remove the commented line,
assert result is Noneandself.assertIs(result, None)25 def test_what_happens_after_functions_return(self): 26 result = src.functions.return_leaves_the_function() 27 self.assertIsNone(result) 28 29 def test_constant_function(self):I no longer need the
resultvariable since it is only used once. I callsrc.functions.return_leaves_the_functiondirectly25 def test_what_happens_after_functions_return(self): 26 # result = src.functions.return_leaves_the_function() 27 # self.assertIsNone(result) 28 self.assertIsNone( 29 src.functions.return_leaves_the_function() 30 ) 31 32 def test_constant_function(self):the test is still green.
I remove the commented lines
25 def test_what_happens_after_functions_return(self): 26 self.assertIsNone( 27 src.functions.return_leaves_the_function() 28 ) 29 30 def test_constant_function(self):
test identity with assertIsNone
RED: make it fail
I add a call to assertIsNotNone in test_identity_function
37 def test_identity_function(self):
38 result = src.functions.identity(None)
39
40 assert result == None
41 self.assertEqual(result, None)
42 self.assertIsNotNone(result)
43
44 result = src.functions.identity(object)
45
46 assert result == object
47 self.assertEqual(result, object)
48
49 def test_why_use_a_function(self):
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
GREEN: make it pass
I change assertIsNotNone to assertIsNone
37 def test_identity_function(self):
38 result = src.functions.identity(None)
39
40 assert result == None
41 self.assertEqual(result, None)
42 # self.assertIsNotNone(result)
43 self.assertIsNone(result)
44
45 result = src.functions.identity(object)
46
47 assert result == object
48 self.assertEqual(result, object)
49
50 def test_why_use_a_function(self):
the test passes.
REFACTOR: make it better
I remove the commented line,
assert result == Noneandself.assertEqual(result, None)37 def test_identity_function(self): 38 result = src.functions.return_leaves_the_function() 39 self.assertIsNone(result) 40 41 def test_constant_function(self):I no longer need the
resultvariable since it is only used once forsrc.functions.identity(None). I call the function directly37 def test_identity_function(self): 38 # result = src.functions.identity(None) 39 # self.assertIsNone(result) 40 self.assertIsNone( 41 src.functions.identity(None) 42 ) 43 44 result = src.functions.identity(object) 45 46 assert result == object 47 self.assertEqual(result, object) 48 49 def test_why_use_a_function(self):the test is still green.
I remove the commented lines
37 def test_identity_function(self): 38 self.assertIsNone( 39 src.functions.identity(None) 40 ) 41 42 result = src.functions.identity(object) 43 44 assert result == object 45 self.assertEqual(result, object) 46 47 def test_why_use_a_function(self):I open a new terminal then change directories to
functionscd functionsI add a git commit message in the new terminal
git commit -am 'use assertIsNone'
close the project
I close
test_functions.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
functionscd ..the terminal 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?
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.