test functions with None
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())
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(src.functions.w_return_none())
22
23 def test_what_happens_after_functions_return(self):
24 self.assertIsNone(
25 src.functions.return_leaves_the_function()
26 )
28 def test_constant_function(self):
29 self.assertEqual(
30 src.functions.constant(), 'the same thing'
31 )
32
33 def test_identity_function(self):
34 self.assertIsNone(src.functions.identity(None))
35 self.assertEqual(
36 src.functions.identity(object), object
37 )
38
39 def test_why_use_a_function(self):
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.pyfrom thetestsfolderI 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
18 def test_making_a_function_w_pass(self):
19 assert_is_none(src.functions.w_pass())
20 self.assertIs(src.functions.w_pass(), None)
21 self.assertIsNotNone(src.functions.w_pass())
22
23 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
18 def test_making_a_function_w_pass(self):
19 assert_is_none(src.functions.w_pass())
20 self.assertIs(src.functions.w_pass(), None)
21 # self.assertIsNotNone(src.functions.w_pass())
22 self.assertIsNone(src.functions.w_pass())
23
24 def test_making_a_function_w_return(self):
the test passes.
REFACTOR: make it better
I remove the commented line, the call to my assert_is_none function and self.assertIs(src.functions.w_pass(), None) from test_making_a_function_w_pass since they are repetitions
18 def test_making_a_function_w_pass(self):
19 self.assertIsNone(src.functions.w_pass())
20
21 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
21 def test_making_a_function_w_return(self):
22 assert_is_none(src.functions.w_return())
23 self.assertIs(
24 src.functions.w_return(), None
25 )
26 self.assertIsNotNone(src.functions.w_return())
27
28 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
21 def test_making_a_function_w_return(self):
22 assert_is_none(src.functions.w_return())
23 self.assertIs(
24 src.functions.w_return(), None
25 )
26 # self.assertIsNotNone(src.functions.w_return())
27 self.assertIsNone(src.functions.w_return())
28
29 def test_making_a_function_w_return_none(self):
the test passes.
REFACTOR: make it better
I remove the commented line, the call to my assert_is_none function and self.assertIs(src.functions.w_return(), None) from test_making_a_function_w_return
21 def test_making_a_function_w_return(self):
22 self.assertIsNone(src.functions.w_return())
23
24 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
24 def test_making_a_function_w_return_none(self):
25 assert_is_none(src.functions.w_return_none())
26 self.assertIs(
27 src.functions.w_return_none(), None
28 )
29 self.assertIsNotNone(src.functions.w_return_none())
30
31 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
24 def test_making_a_function_w_return_none(self):
25 assert_is_none(src.functions.w_return_none())
26 self.assertIs(
27 src.functions.w_return_none(), None
28 )
29 # self.assertIsNotNone(src.functions.w_return_none())
30 self.assertIsNone(src.functions.w_return_none())
31
32 def test_what_happens_after_functions_return(self):
the test passes.
REFACTOR: make it better
I remove the commented line, the call to my assert_is_none function and self.assertIs(src.functions.w_return_none(), None) from test_making_a_function_w_return_none
24 def test_making_a_function_w_return_none(self):
25 self.assertIsNone(src.functions.w_return_none())
26
27 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
27 def test_what_happens_after_functions_return(self):
28 assert_is_none(
29 src.functions.return_leaves_the_function()
30 )
31 self.assertIs(
32 src.functions.return_leaves_the_function(),
33 None
34 )
35 self.assertIsNotNone(
36 src.functions.return_leaves_the_function()
37 )
38
39 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
27 def test_what_happens_after_functions_return(self):
28 assert_is_none(
29 src.functions.return_leaves_the_function()
30 )
31 self.assertIs(
32 src.functions.return_leaves_the_function(),
33 None
34 )
35 # self.assertIsNotNone(
36 self.assertIsNone(
37 src.functions.return_leaves_the_function()
38 )
39
40 def test_constant_function(self):
the test passes.
REFACTOR: make it better
I remove the commented line, the call to my assert_is_none function and self.assertIs(src.functions.return_leaves_the_function(), None) from test_what_happens_after_functions_return
27 def test_what_happens_after_functions_return(self):
28 self.assertIsNone(
29 src.functions.return_leaves_the_function()
30 )
31
32 def test_constant_function(self):
test identity with assertIsNone
RED: make it fail
I add a call to assertIsNotNone in test_identity_function
32 def test_constant_function(self):
33 self.assertEqual(
34 src.functions.constant(), 'the same thing'
35 )
36
37 def test_identity_function(self):
38 assert_is_none(src.functions.identity(None))
39 self.assertIs(
40 src.functions.identity(None), None
41 )
42 self.assertIsNotNone(src.functions.identity(None))
43 self.assertEqual(
44 src.functions.identity(object), object
45 )
46
47 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 assert_is_none(src.functions.identity(None))
39 self.assertIs(
40 src.functions.identity(None), None
41 )
42 # self.assertIsNotNone(src.functions.identity(None))
43 self.assertIsNone(src.functions.identity(None))
44 self.assertEqual(
45 src.functions.identity(object), object
46 )
the test passes.
REFACTOR: make it better
I remove the commented line, the call to my assert_is_none function and
self.assertIs(src.functions.identity(None), None)from test_identity_function37 def test_identity_function(self): 38 self.assertIsNone(src.functions.identity(None)) 39 self.assertEqual( 40 src.functions.identity(object), object 41 ) 42 43 def test_why_use_a_function(self):I remove my assert_is_none function since it is no longer used
1import src.functions 2import unittest 3 4 5class TestFunctions(unittest.TestCase):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 and assertIsNotNone methods 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.