test functions with unittest
I want to use the unittest library in the functions project. I also want to use object attributes to remove repetition of some values from the tests.
preview
I have these tests by the end of the chapter
1import src.functions
2import unittest
3
4
5def assert_is_none(something):
6 assert something is None
7
8
9class TestFunctions(unittest.TestCase):
10
11 first = 'first'
12 last = 'last'
13 a_tuple = (0, 1, 2, 'n')
14 a_list = [0, 1, 2, 'n']
15 a_set = {0, 1, 2, 'n'}
16 a_dictionary = {'key': 'value'}
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
22 def test_making_a_function_w_return(self):
23 assert_is_none(src.functions.w_return())
24 self.assertIs(
25 src.functions.w_return(), None
26 )
28 def test_making_a_function_w_return_none(self):
29 assert_is_none(src.functions.w_return_none())
30 self.assertIs(
31 src.functions.w_return_none(), None
32 )
33
34 def test_what_happens_after_functions_return(self):
35 assert_is_none(
36 src.functions.return_leaves_the_function()
37 )
38 self.assertIs(
39 src.functions.return_leaves_the_function(),
40 None
41 )
43 def test_constant_function(self):
44 self.assertEqual(
45 src.functions.constant(), 'the same thing'
46 )
47
48 def test_identity_function(self):
49 assert_is_none(src.functions.identity(None))
50 self.assertIs(
51 src.functions.identity(None), None
52 )
53
54 self.assertEqual(
55 src.functions.identity(object), object
56 )
58 def test_why_use_a_function(self):
59 def add_x(number):
60 return 3 + number
61
62 self.assertEqual(add_x(0), 3)
63 self.assertEqual(add_x(1), 4)
64 self.assertEqual(add_x(2), 5)
65 self.assertEqual(add_x(3), 6)
66 self.assertEqual(add_x(4), 7)
67 self.assertEqual(add_x(5), 8)
68 self.assertEqual(add_x(6), 9)
69 self.assertEqual(add_x(7), 10)
70 self.assertEqual(add_x(8), 11)
71 self.assertEqual(add_x(9), 12)
73 def test_positional_arguments(self):
74 positional_arguments = (
75 src.functions.positional_arguments
76 )
77
78 self.assertEqual(
79 positional_arguments(self.first, self.last),
80 (self.first, self.last)
81 )
82 self.assertEqual(
83 positional_arguments(self.last, self.first),
84 (self.last, self.first)
85 )
86
87 self.assertEqual(
88 positional_arguments(0, 1), (0, 1)
89 )
90
91 self.assertEqual(
92 positional_arguments(
93 self.a_tuple, self.a_list
94 ),
95 (self.a_tuple, self.a_list)
96 )
97
98 self.assertEqual(
99 src.functions.keyword_arguments(
100 self.a_set, self.a_dictionary
101 ),
102 (self.a_set, self.a_dictionary)
103 )
105 def test_keyword_arguments(self):
106 keyword_arguments = (
107 src.functions.keyword_arguments
108 )
109
110 self.assertEqual(
111 keyword_arguments(
112 first_input=self.first,
113 last_input=self.last,
114 ),
115 (self.first, self.last)
116 )
117 self.assertEqual(
118 keyword_arguments(
119 last_input=self.last,
120 first_input=self.first,
121 ),
122 (self.first, self.last)
123 )
124
125 self.assertEqual(
126 keyword_arguments(
127 last_input=0, first_input=1,
128 ),
129 (1, 0)
130 )
131
132 self.assertEqual(
133 keyword_arguments(
134 first_input=self.a_tuple,
135 last_input=self.a_list,
136 ),
137 (self.a_tuple, self.a_list)
138 )
139
140 self.assertEqual(
141 src.functions.positional_arguments(
142 last_input=self.a_dictionary,
143 first_input=self.a_set,
144 ),
145 (self.a_set, self.a_dictionary)
146 )
148 def test_args_and_kwargs(self):
149 self.assertEqual(
150 src.functions.args_and_kwargs(
151 self.first, last_input=self.last
152 ),
153 (self.first, self.last)
154 )
156 def test_optional_arguments(self):
157 optional_arguments = (
158 src.functions.optional_arguments
159 )
160
161 first_name, last_name = 'jane', 'doe'
162 self.assertEqual(
163 optional_arguments(
164 first_name,
165 ),
166 (first_name, last_name)
167 )
168
169 first_name, blow = 'joe', 'blow'
170 self.assertEqual(
171 optional_arguments(
172 first_name, blow
173 ),
174 (first_name, blow)
175 )
176
177 first_name = 'john'
178 self.assertEqual(
179 optional_arguments(
180 first_input=first_name,
181 ),
182 (first_name, last_name)
183 )
184
185 last_name = 'smith'
186 self.assertEqual(
187 optional_arguments(
188 last_input=last_name,
189 first_input=first_name,
190 ),
191 (first_name, last_name)
192 )
194 def test_unknown_number_of_arguments(self):
195 unknown_number_of_arguments = (
196 src.functions.unknown_number_of_arguments
197 )
198
199 a_tuple = (0, 1)
200 a_dictionary = {'a': 2, 'b': 3}
201 self.assertEqual(
202 unknown_number_of_arguments(
203 *a_tuple, **a_dictionary
204 ),
205 (a_tuple, a_dictionary)
206 )
207
208 a_dictionary = {'a': 2, 'b': 3, 'c': 4}
209 self.assertEqual(
210 unknown_number_of_arguments(
211 *a_tuple, **a_dictionary
212 ),
213 (a_tuple, a_dictionary)
214 )
215
216 a_tuple = (0, 1, 2)
217 a_dictionary = {'a': 3, 'b': 4, 'c': 5}
218 self.assertEqual(
219 unknown_number_of_arguments(
220 *a_tuple, **a_dictionary
221 ),
222 (a_tuple, a_dictionary)
223 )
224
225 a_tuple = (0, 1, 2, 'n')
226 self.assertEqual(
227 unknown_number_of_arguments(*a_tuple),
228 (a_tuple, {})
229 )
230
231 a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
232 self.assertEqual(
233 unknown_number_of_arguments(**a_dictionary),
234 ((), a_dictionary)
235 )
236
237 self.assertEqual(
238 unknown_number_of_arguments(), ((), {})
239 )
240
241
242# Exceptions seen
243# AssertionError
244# NameError
245# TypeError
246# SyntaxError
247# AttributeError
open the project
I open a terminal
I change directory to the project
the terminal shows I am in the
functionsfolder.../pumping_python/functionsI open
test_functions.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
the terminal shows
test_functions.py ............ [100%] =================== 12 passed in G.HIs ===================
add TestFunctions object
RED: make it fail
I add an object named
Functionstotest_functions.pythe test is still green.
I change the name of the object to
TestFunctionsthe terminal is my friend, and shows AttributeError
GREEN: make it pass
I add unittest.TestCase as the parent object of
TestFunctionsthe terminal is my friend, and shows NameError
- caption:
terminal
- NameError: name ‘unittest’ is not defined.
Did you forget to import ‘unittest’?
I add an import statement at the top of the file
1import src.functions 2import unittest 3 4 5def assert_equal(input_1, input_2):the terminal is my friend, and shows AssertionError
I change True to False in the assertion
the test passes.
REFACTOR: make it better
test_making_a_function_w_pass with unittest
RED: make it fail
I go back to the terminal where the tests are running
I remove
test_failureI move test_making_a_function_w_pass to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_making_a_function_w_pass() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_making_a_function_w_pass
13class TestFunctions(unittest.TestCase):
14
15 # def test_making_a_function_w_pass():
16 def test_making_a_function_w_pass(self):
17 assert_is_none(src.functions.w_pass())
the test is green again.
REFACTOR: make it better
I add a call to the assertIsNot method
the terminal is my friend, and shows AssertionError
I change assertIsNot to assertIs
the test passes.
I remove the commented lines from test_making_a_function_w_pass
test_making_a_function_w_return with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_making_a_function_w_return to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_making_a_function_w_return() takes 0 positional arguments but 1 was givenbecause …
GREEN: make it pass
I add self to the parentheses of test_making_a_function_w_return
17 self.assertIs(src.functions.w_pass(), None)
18
19 # def test_making_a_function_w_return():
20 def test_making_a_function_w_return(self):
21 assert_is_none(src.functions.w_return())
green again.
REFACTOR: make it better
I add a call to the assertIsNot method
the terminal is my friend, and shows AssertionError
I change assertIsNot to assertIs
the test passes.
I remove the commented lines from test_making_a_function_w_return
test_making_a_function_w_return_none with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_making_a_function_w_return_none to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_making_a_function_w_return_none() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_making_a_function_w_return_none
21 self.assertIs(
22 src.functions.w_return(), None
23 )
24
25 # def test_making_a_function_w_return_none():
26 def test_making_a_function_w_return_none(self):
27 assert_is_none(src.functions.w_return_none())
green.
REFACTOR: make it better
I add a call to the assertIsNot method
the terminal is my friend, and shows AssertionError
I change assertIsNot to assertIs
the test passes.
I remove the commented lines from test_making_a_function_w_return_none
test_what_happens_after_functions_return with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_what_happens_after_functions_return to make it a method of the TestFunctions class
GREEN: make it pass
I add self to the parentheses of test_what_happens_after_functions_return
27 self.assertIs(
28 src.functions.w_return_none(), None
29 )
30
31 # def test_what_happens_after_functions_return():
32 def test_what_happens_after_functions_return(self):
33 assert_is_none(
34 src.functions.return_leaves_the_function()
35 )
36
37
38def test_constant_function():
green again.
REFACTOR: make it better
I add a call to the assertIsNot method
the terminal is my friend, and shows AssertionError
I change assertIsNot to assertIs
the test passes.
I remove the commented lines from test_what_happens_after_functions_return
test_constant_function with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_constant_function to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_constant_function() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_constant_function
35 self.assertIs(
36 src.functions.return_leaves_the_function(),
37 None
38 )
39
40 # def test_constant_function():
41 def test_constant_function(self):
42 assert_equal(
43 src.functions.constant(), 'the same thing'
44 )
green again.
REFACTOR: make it better
I add a call to the assertNotEqual method
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual
the test passes.
I remove the commented lines and the call to my assert_equal function (since it is now a repetition) from test_constant_function
test_identity_function with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_identity_function to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_identity_function() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_identity_function
40 def test_constant_function(self):
41 self.assertEqual(
42 src.functions.constant(), 'the same thing'
43 )
44
45 # def test_identity_function():
46 def test_identity_function(self):
47 assert_is_none(src.functions.identity(None))
the test is green again.
REFACTOR: make it better
I add calls to the assertIsNot and assertNotEqual methods
the terminal is my friend, and shows AssertionError
I change assertIsNot to assertIs for the first assertion in test_identity_function
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual for the second assertion in test_identity_function
the test passes.
I remove the commented lines and the call to my assert_equal function from test_identity_function
test_why_use_a_function with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_why_use_a_function to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_why_use_a_function() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_why_use_a_function
51 self.assertEqual(
52 src.functions.identity(object), object
53 )
54
55 # def test_why_use_a_function():
56 def test_why_use_a_function(self):
57 def add_x(number):
58 return 3 + number
green again.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_why_use_a_function
the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_why_use_a_function
the test passes.
I remove the commented lines and the calls to my assert_equal function from test_why_use_a_function
test_positional_arguments with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_positional_arguments to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_positional_arguments() takes 0 positional arguments but 1 was givenbecause …
GREEN: make it pass
I add self to the parentheses of test_positional_arguments
68 self.assertEqual(add_x(9), 12)
69
70 # def test_positional_arguments():
71 def test_positional_arguments(self):
72 positional_arguments = (
73 src.functions.positional_arguments
74 )
green.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_positional_arguments
the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_positional_arguments
the test passes.
I remove the commented lines and the calls to my assert_equal function from test_positional_arguments
test_keyword_arguments with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_keyword_arguments to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_keyword_arguments() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_keyword_arguments
96 a_set = {0, 1, 2, 'n'}
97 a_dictionary = {'key': 'value'}
98 self.assertEqual(
99 src.functions.keyword_arguments(
100 a_set, a_dictionary
101 ),
102 (a_set, a_dictionary)
103 )
104
105 # def test_keyword_arguments():
106 def test_keyword_arguments(self):
107 keyword_arguments = (
108 src.functions.keyword_arguments
109 )
110 first, last = 'first', 'last'v
green again.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_keyword_arguments
the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_keyword_arguments
the test passes.
I remove the commented lines and the calls to my assert_equal function from test_keyword_arguments
test_args_and_kwargs with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_args_and_kwargs to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_args_and_kwargs() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_args_and_kwargs
141 a_set = {0, 1, 2, 'n'}
142 a_dictionary = {'key': 'value'}
143 self.assertEqual(
144 src.functions.positional_arguments(
145 last_input=a_dictionary,
146 first_input=a_set,
147 ),
148 (a_set, a_dictionary)
149 )
150
151 # def test_args_and_kwargs():
152 def test_args_and_kwargs(self):
153 first, last = 'first', 'last'
the test is green again.
REFACTOR: make it better
I add a call to the assertNotEqual method in test_args_and_kwargs
the terminal is my friend, and shows AssertionError
I change the call from assertNotEqual to assertEqual in test_args_and_kwargs
the test passes.
I add variables for the call to
src.functions.args_and_kwargsand my expectationI remove the commented lines and the call to my assert_equal function from test_args_and_kwargs
test_optional_arguments with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_optional_arguments to make it a method of the TestFunctions class
GREEN: make it pass
I add self to the parentheses of test_optional_arguments
154 self.assertEqual(
155 src.functions.args_and_kwargs(
156 first, last_input=last
157 ),
158 (first, last)
159 )
160
161 # def test_optional_arguments():
162 def test_optional_arguments(self):
163 optional_arguments = (
164 src.functions.optional_arguments
165 )
green again.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_optional_arguments
the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_optional_arguments
the test passes.
I remove the commented lines and the calls to my assert_equal function from test_optional_arguments
test_unknown_number_of_arguments with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_unknown_number_of_arguments to make it a method of the TestFunctions class
the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_unknown_number_of_arguments() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_unknown_number_of_arguments
190 last_name = 'smith'
191 self.assertEqual(
192 optional_arguments(
193 last_input=last_name,
194 first_input=first_name,
195 ),
196 (first_name, last_name)
197 )
198
199 # def test_unknown_number_of_arguments():
200 def test_unknown_number_of_arguments(self):
201 unknown_number_of_arguments = (
202 src.functions.unknown_number_of_arguments
203 )
green.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_unknown_number_of_arguments
the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_unknown_number_of_arguments
the test passes.
I remove the commented lines and the calls to my assert_equal function from test_unknown_number_of_arguments
I remove my assert_equal function since it is no longer used
1import src.functions 2import unittest 3 4 5def assert_is_none(something): 6 assert something is None 7 8 9class TestFunctions(unittest.TestCase):
extract first, last object attributes
I want to use object attributes to remove repetition from the tests.
I go back to the terminal where the tests are running
I add object attributes for
'first'and'last'I use the object attributes for
firstandlastin test_positional_argumentsthe test is still green.
I use the object attributes for
firstandlastin test_keyword_argumentsstill green.
I use the object attributes for
firstandlastin test_args_and_kwargsgreen.
I remove the commented lines from test_args_and_kwargs
extract a_tuple object attribute
I add an object attribute for
(0, 1, 2, 'n')I use the new object attribute for
a_tuplein test_positional_argumentsstill green.
I use the new object attribute for
a_tuplein test_keyword_argumentsthe test is still green.
extract a_list object attribute
I add an object attribute for
[0, 1, 2, 'n']I use the new object attribute for
a_listin test_positional_argumentsstill green.
I use the new object attribute for
a_listin test_keyword_argumentsgreen.
extract a_set object attribute
I add an object attribute for
{0, 1, 2, 'n'}I use the new object attribute to remove repetition of
{0, 1, 2, 'n'}from test_positional_argumentsstill green.
I use the new object attributes to remove repetition of
{0, 1, 2, 'n'}from test_keyword_argumentsstill green
extract a_dictionary object attribute
I add an object attribute for
a_dictionaryI use the new object attribute for
a_dictionaryin test_positional_argumentsthe test is still green.
I remove the commented lines from test_positional_arguments
I use the new object attributes for
a_dictionaryin test_keyword_argumentsthe test is still green.
I remove the commented lines from test_keyword_arguments
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
functionsthe terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I can use the unittest library to write tests with the methods of the unittest.TestCase class or I can write them with bare assert statements.
I can use object attributes for things that repeat so that methods of the same object can use them.
code from the chapter
what is next?
Would you like to test the person project with the unittest library?
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.