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
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 G.HIs ===================
add TestFunctions class
RED: make it fail
I add an object named
Functionstotest_functions.py8def assert_is_none(something): 9 assert something is None 10 11 12class Functions(object): 13 14 def test_failure(self): 15 self.assertEqual(True, False) 16 17 18def test_making_a_function_w_pass(): 19 assert_is_none(src.functions.w_pass())the test is still green.
I change the name of the object to
TestFunctions8def assert_is_none(something): 9 assert something is None 10 11 12# class Functions(object): 13class TestFunctions(object): 14 15 def test_failure(self):the terminal is my friend, and shows AttributeError
AttributeError: 'TestFunctions' object has no attribute 'assertEqual'
GREEN: make it pass
I add unittest.TestCase as the parent object of
TestFunctions12# class Functions(object): 13# class TestFunctions(object): 14class TestFunctions(unittest.TestCase): 15 16 def test_failure(self):the terminal is my friend, and shows NameError
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
AssertionError: True != FalseI change True to False in the assertion
13# class Functions(object): 14# class TestFunctions(object): 15class TestFunctions(unittest.TestCase): 16 17 def test_failure(self): 18 # self.assertEqual(True, False) 19 self.assertEqual(False, False) 20 21 22def test_making_a_function_w_pass():the test passes.
REFACTOR: make it better
I remove the commented lines
9def assert_is_none(something): 10 assert something is None 11 12 13class TestFunctions(unittest.TestCase): 14 15 def test_failure(self): 16 self.assertEqual(False, False) 17 18 19def test_making_a_function_w_pass():I open a new terminal then make sure I am in the
functionsfoldercd functionsI add a git commit message in the new terminal
git commit -am \ 'add TestFunctions class'
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
13class TestFunctions(unittest.TestCase): 14 15 def test_making_a_function_w_pass(): 16 assert_is_none(src.functions.w_pass()) 17 18 19def test_making_a_function_w_return():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 class (
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
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()) 18 self.assertIsNot( 19 src.functions.w_pass(), None 20 ) 21 22 23def test_making_a_function_w_return():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: NoneI change assertIsNot to assertIs
7 # def test_making_a_function_w_pass(): 8 def test_making_a_function_w_pass(self): 9 assert_is_none(src.functions.w_pass()) 10 # self.assertIsNot( 11 self.assertIs( 12 src.functions.w_pass(), None 13 ) 14 15 16def test_making_a_function_w_return():the test passes.
I remove the commented lines from test_making_a_function_w_pass
13class TestFunctions(unittest.TestCase): 14 15 def test_making_a_function_w_pass(self): 16 assert_is_none(src.functions.w_pass()) 17 self.assertIs(src.functions.w_pass(), None) 18 19 20def test_making_a_function_w_return():I add a git commit message in the other terminal
git commit -am \ 'move test_making_a_function_w_pass to TestFunctions'
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
17 self.assertIs(src.functions.w_pass(), None) 18 19 def test_making_a_function_w_return(): 20 assert_is_none(src.functions.w_return()) 21 22 23def test_making_a_function_w_return_none():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
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()) 22 self.assertIsNot( 23 src.functions.w_return(), None 24 ) 25 26 27def test_making_a_function_w_return_none():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: NoneI change assertIsNot to assertIs
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()) 22 # self.assertIsNot( 23 self.assertIs( 24 src.functions.w_return(), None 25 )the test passes.
I remove the commented lines from test_making_a_function_w_return
17 self.assertIs(src.functions.w_pass(), None) 18 19 def test_making_a_function_w_return(self): 20 assert_is_none(src.functions.w_return()) 21 self.assertIs( 22 src.functions.w_return(), None 23 ) 24 25 26def test_making_a_function_w_return_none():I add a git commit message in the other terminal
git commit -am \ 'move test_making_a_function_w_return to TestFunctions'
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
21 self.assertIs( 22 src.functions.w_return(), None 23 ) 24 25 def test_making_a_function_w_return_none(): 26 assert_is_none(src.functions.w_return_none()) 27 28 29def test_what_happens_after_functions_return():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 class (
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
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()) 28 self.assertIsNot( 29 src.functions.w_return_none(), None 30 ) 31 32 33def test_what_happens_after_functions_return():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: NoneI change assertIsNot to assertIs
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()) 28 # self.assertIsNot( 29 self.assertIs( 30 src.functions.w_return_none(), None 31 ) 32 33 34def test_what_happens_after_functions_return():the test passes.
I remove the commented lines from 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(self): 26 assert_is_none(src.functions.w_return_none()) 27 self.assertIs( 28 src.functions.w_return_none(), None 29 ) 30 31 32def test_what_happens_after_functions_return():I add a git commit message in the other terminal
git commit -am \ 'move test_making_a_function_w_return_none to TestFunctions'
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
27 self.assertIs( 28 src.functions.w_return_none(), None 29 ) 30 31 def test_what_happens_after_functions_return(): 32 assert_is_none( 33 src.functions.return_leaves_the_function() 34 ) 35 36 37def test_constant_function():the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_what_happens_after_functions_return() takes 0 positional arguments but 1 was given
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
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 self.assertIsNot( 37 src.functions.return_leaves_the_function(), 38 None 39 ) 40 41 42def test_constant_function():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: NoneI change assertIsNot to assertIs
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 # self.assertIsNot( 37 self.assertIs( 38 src.functions.return_leaves_the_function(), 39 None 40 )the test passes.
I remove the commented lines from 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(self): 32 assert_is_none( 33 src.functions.return_leaves_the_function() 34 ) 35 self.assertIs( 36 src.functions.return_leaves_the_function(), 37 None 38 ) 39 40 41def test_constant_function():I add a git commit message in the other terminal
git commit -am \ 'move test_what_happens_after_functions_return to TestFunctions'
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
35 self.assertIs( 36 src.functions.return_leaves_the_function(), 37 None 38 ) 39 40 def test_constant_function(): 41 assert_equal( 42 src.functions.constant(), 'the same thing' 43 ) 44 45 46def test_identity_function():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
40 # def test_constant_function(): 41 def test_constant_function(self): 42 assert_equal( 43 src.functions.constant(), 'the same thing' 44 ) 45 self.assertNotEqual( 46 src.functions.constant(), 'the same thing' 47 ) 48 49 50def test_identity_function():the terminal is my friend, and shows AssertionError
AssertionError: 'the same thing' == 'the same thing'I change assertNotEqual to assertEqual
40 # def test_constant_function(): 41 def test_constant_function(self): 42 assert_equal( 43 src.functions.constant(), 'the same thing' 44 ) 45 # self.assertNotEqual( 46 self.assertEqual( 47 src.functions.constant(), 'the same thing' 48 )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
35 self.assertIs( 36 src.functions.return_leaves_the_function(), 37 None 38 ) 39 40 def test_constant_function(self): 41 self.assertEqual( 42 src.functions.constant(), 'the same thing' 43 ) 44 45 46def test_identity_function():I add a git commit message in the other terminal
git commit -am \ 'move test_constant_function to TestFunctions'
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
40 def test_constant_function(self): 41 self.assertEqual( 42 src.functions.constant(), 'the same thing' 43 ) 44 45 def test_identity_function(): 46 assert_is_none(src.functions.identity(None)) 47 assert_equal( 48 src.functions.identity(object), object 49 ) 50 51 52def test_why_use_a_function():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 class (
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
45 # def test_identity_function(): 46 def test_identity_function(self): 47 assert_is_none(src.functions.identity(None)) 48 self.assertIsNot( 49 src.functions.identity(None), None 50 ) 51 52 assert_equal( 53 src.functions.identity(object), object 54 ) 55 self.assertNotEqual( 56 src.functions.identity(object), object 57 ) 58 59 60def test_why_use_a_function():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: NoneI change assertIsNot to assertIs for the first assertion in test_identity_function
45 # def test_identity_function(): 46 def test_identity_function(self): 47 assert_is_none(src.functions.identity(None)) 48 # self.assertIsNot( 49 self.assertIs( 50 src.functions.identity(None), None 51 ) 52 53 assert_equal( 54 src.functions.identity(object), object 55 )the terminal is my friend, and shows AssertionError
AssertionError: <class 'object'> == <class 'object'>I change assertNotEqual to assertEqual for the second assertion in test_identity_function
53 assert_equal( 54 src.functions.identity(object), object 55 ) 56 # self.assertNotEqual( 57 self.assertEqual( 58 src.functions.identity(object), object 59 ) 60 61 62def test_why_use_a_function():the test passes.
I remove the commented lines and the call to my assert_equal function from 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(self): 46 assert_is_none(src.functions.identity(None)) 47 self.assertIs( 48 src.functions.identity(None), None 49 ) 50 51 self.assertEqual( 52 src.functions.identity(object), object 53 ) 54 55 56def test_why_use_a_function():I add a git commit message in the other terminal
git commit -am \ 'move test_identity_function to TestFunctions'
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
51 self.assertEqual( 52 src.functions.identity(object), object 53 ) 54 55 def test_why_use_a_function(): 56 def add_x(number): 57 return 3 + number 58 59 assert_equal(add_x(0), 3) 60 assert_equal(add_x(1), 4) 61 assert_equal(add_x(2), 5) 62 assert_equal(add_x(3), 6) 63 assert_equal(add_x(4), 7) 64 assert_equal(add_x(5), 8) 65 assert_equal(add_x(6), 9) 66 assert_equal(add_x(7), 10) 67 assert_equal(add_x(8), 11) 68 assert_equal(add_x(9), 12) 69 70 71def test_positional_arguments():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 class (
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
55 # def test_why_use_a_function(): 56 def test_why_use_a_function(self): 57 def add_x(number): 58 return 3 + number 59 60 assert_equal(add_x(0), 3) 61 self.assertNotEqual(add_x(0), 3) 62 assert_equal(add_x(1), 4) 63 self.assertNotEqual(add_x(1), 4)58 assert_equal(add_x(2), 5) 59 self.assertNotEqual(add_x(2), 5) 60 assert_equal(add_x(3), 6) 61 self.assertNotEqual(add_x(3), 6) 62 assert_equal(add_x(4), 7) 63 self.assertNotEqual(add_x(4), 7) 64 assert_equal(add_x(5), 8) 65 self.assertNotEqual(add_x(5), 8)66 assert_equal(add_x(6), 9) 67 self.assertNotEqual(add_x(6), 9) 68 assert_equal(add_x(7), 10) 69 self.assertNotEqual(add_x(7), 10) 70 assert_equal(add_x(8), 11) 71 self.assertNotEqual(add_x(8), 11) 72 assert_equal(add_x(9), 12) 73 self.assertNotEqual(add_x(9), 12) 74 75 76def test_positional_arguments():the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_why_use_a_function
60 assert_equal(add_x(0), 3) 61 # self.assertNotEqual(add_x(0), 3) 62 self.assertEqual(add_x(0), 3) 63 assert_equal(add_x(1), 4) 64 # self.assertNotEqual(add_x(1), 4) 65 self.assertEqual(add_x(1), 4)66 assert_equal(add_x(2), 5) 67 # self.assertNotEqual(add_x(2), 5) 68 self.assertEqual(add_x(2), 5) 69 assert_equal(add_x(3), 6) 70 # self.assertNotEqual(add_x(3), 6) 71 self.assertEqual(add_x(3), 6)72 assert_equal(add_x(4), 7) 73 # self.assertNotEqual(add_x(4), 7) 74 self.assertEqual(add_x(4), 7) 75 assert_equal(add_x(5), 8) 76 # self.assertNotEqual(add_x(5), 8) 77 self.assertEqual(add_x(5), 8)78 assert_equal(add_x(6), 9) 79 # self.assertNotEqual(add_x(6), 9) 80 self.assertEqual(add_x(6), 9) 81 assert_equal(add_x(7), 10) 82 # self.assertNotEqual(add_x(7), 10) 83 self.assertEqual(add_x(7), 10)84 assert_equal(add_x(8), 11) 85 # self.assertNotEqual(add_x(8), 11) 86 self.assertEqual(add_x(8), 11) 87 assert_equal(add_x(9), 12) 88 # self.assertNotEqual(add_x(9), 12) 89 self.assertEqual(add_x(9), 12) 90 91 92def test_positional_arguments():the test passes.
I remove the commented lines and the calls to my assert_equal function from test_why_use_a_function
51 self.assertEqual( 52 src.functions.identity(object), object 53 ) 54 55 def test_why_use_a_function(self): 56 def add_x(number): 57 return 3 + number 58 59 self.assertEqual(add_x(0), 3) 60 self.assertEqual(add_x(1), 4) 61 self.assertEqual(add_x(2), 5) 62 self.assertEqual(add_x(3), 6) 63 self.assertEqual(add_x(4), 7) 64 self.assertEqual(add_x(5), 8) 65 self.assertEqual(add_x(6), 9) 66 self.assertEqual(add_x(7), 10) 67 self.assertEqual(add_x(8), 11) 68 self.assertEqual(add_x(9), 12) 69 70 71def test_positional_arguments():I add a git commit message in the other terminal
git commit -am \ 'move test_why_use_a_function to TestFunctions'
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
68 self.assertEqual(add_x(9), 12) 69 70 def test_positional_arguments(): 71 positional_arguments = ( 72 src.functions.positional_arguments 73 ) 74 first, last = 'first', 'last'76 assert_equal( 77 positional_arguments(first, last), 78 (first, last) 79 ) 80 assert_equal( 81 positional_arguments(last, first), 82 (last, first) 83 ) 84 85 assert_equal( 86 positional_arguments(0, 1), (0, 1) 87 )89 a_tuple = (0, 1, 2, 'n') 90 a_list = [0, 1, 2, 'n'] 91 assert_equal( 92 positional_arguments(a_tuple, a_list), 93 (a_tuple, a_list) 94 ) 95 96 a_set = {0, 1, 2, 'n'} 97 a_dictionary = {'key': 'value'} 98 assert_equal( 99 src.functions.keyword_arguments( 100 a_set, a_dictionary, 101 ), 102 (a_set, a_dictionary) 103 ) 104 105 106def test_keyword_arguments():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
77 assert_equal( 78 positional_arguments(first, last), 79 (first, last) 80 ) 81 self.assertNotEqual( 82 positional_arguments(first, last), 83 (first, last) 84 ) 85 assert_equal( 86 positional_arguments(last, first), 87 (last, first) 88 ) 89 self.assertNotEqual( 90 positional_arguments(last, first), 91 (last, first) 92 )94 assert_equal( 95 positional_arguments(0, 1), (0, 1) 96 ) 97 self.assertNotEqual( 98 positional_arguments(0, 1), (0, 1) 99 )101 a_tuple = (0, 1, 2, 'n') 102 a_list = [0, 1, 2, 'n'] 103 assert_equal( 104 positional_arguments(a_tuple, a_list), 105 (a_tuple, a_list) 106 ) 107 self.assertNotEqual( 108 positional_arguments(a_tuple, a_list), 109 (a_tuple, a_list) 110 )112 a_set = {0, 1, 2, 'n'} 113 a_dictionary = {'key': 'value'} 114 assert_equal( 115 src.functions.keyword_arguments( 116 a_set, a_dictionary, 117 ), 118 (a_set, a_dictionary) 119 ) 120 self.assertNotEqual( 121 src.functions.keyword_arguments( 122 a_set, a_dictionary 123 ), 124 (a_set, a_dictionary) 125 ) 126 127 128def test_keyword_arguments():the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_positional_arguments
77 assert_equal( 78 positional_arguments(first, last), 79 (first, last) 80 ) 81 # self.assertNotEqual( 82 self.assertEqual( 83 positional_arguments(first, last), 84 (first, last) 85 ) 86 assert_equal( 87 positional_arguments(last, first), 88 (last, first) 89 ) 90 # self.assertNotEqual( 91 self.assertEqual( 92 positional_arguments(last, first), 93 (last, first) 94 )96 assert_equal( 97 positional_arguments(0, 1), (0, 1) 98 ) 99 # self.assertNotEqual( 100 self.assertEqual( 101 positional_arguments(0, 1), (0, 1) 102 )104 a_tuple = (0, 1, 2, 'n') 105 a_list = [0, 1, 2, 'n'] 106 assert_equal( 107 positional_arguments(a_tuple, a_list), 108 (a_tuple, a_list) 109 ) 110 # self.assertNotEqual( 111 self.assertEqual( 112 positional_arguments(a_tuple, a_list), 113 (a_tuple, a_list) 114 )116 a_set = {0, 1, 2, 'n'} 117 a_dictionary = {'key': 'value'} 118 assert_equal( 119 src.functions.keyword_arguments( 120 a_set, a_dictionary, 121 ), 122 (a_set, a_dictionary) 123 ) 124 # self.assertNotEqual( 125 self.assertEqual( 126 src.functions.keyword_arguments( 127 a_set, a_dictionary 128 ), 129 (a_set, a_dictionary) 130 ) 131 132 133def test_keyword_arguments():the test passes.
I remove the commented lines and the calls to my assert_equal function from test_positional_arguments
68 self.assertEqual(add_x(9), 12) 69 70 def test_positional_arguments(self): 71 positional_arguments = ( 72 src.functions.positional_arguments 73 ) 74 first, last = 'first', 'last'76 self.assertEqual( 77 positional_arguments(first, last), 78 (first, last) 79 ) 80 self.assertEqual( 81 positional_arguments(last, first), 82 (last, first) 83 )85 self.assertEqual( 86 positional_arguments(0, 1), (0, 1) 87 )89 a_tuple = (0, 1, 2, 'n') 90 a_list = [0, 1, 2, 'n'] 91 self.assertEqual( 92 positional_arguments(a_tuple, a_list), 93 (a_tuple, a_list) 94 )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 106def test_keyword_arguments():I add a git commit message in the other terminal
git commit -am \ 'move test_positional_arguments to TestFunctions'
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
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 keyword_arguments = ( 107 src.functions.keyword_arguments 108 ) 109 first, last = 'first', 'last'111 assert_equal( 112 keyword_arguments( 113 first_input=first, last_input=last, 114 ), 115 (first, last) 116 ) 117 assert_equal( 118 keyword_arguments( 119 last_input=last, first_input=first, 120 ), 121 (first, last) 122 )124 assert_equal( 125 keyword_arguments( 126 last_input=0, first_input=1, 127 ), 128 (1, 0) 129 )131 a_tuple = (0, 1, 2, 'n') 132 a_list = [0, 1, 2, 'n'] 133 assert_equal( 134 keyword_arguments( 135 first_input=a_tuple, 136 last_input=a_list, 137 ), 138 (a_tuple, a_list) 139 )141 a_set = {0, 1, 2, 'n'} 142 a_dictionary = {'key': 'value'} 143 assert_equal( 144 src.functions.positional_arguments( 145 last_input=a_dictionary, 146 first_input=a_set, 147 ), 148 (a_set, a_dictionary) 149 ) 150 151 152def test_args_and_kwargs():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 class (
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
112 assert_equal( 113 keyword_arguments( 114 first_input=first, last_input=last, 115 ), 116 (first, last) 117 ) 118 self.assertNotEqual( 119 keyword_arguments( 120 first_input=first, last_input=last, 121 ), 122 (first, last) 123 ) 124 assert_equal( 125 keyword_arguments( 126 last_input=last, first_input=first, 127 ), 128 (first, last) 129 ) 130 self.assertNotEqual( 131 keyword_arguments( 132 last_input=last, first_input=first, 133 ), 134 (first, last) 135 )137 assert_equal( 138 keyword_arguments( 139 last_input=0, first_input=1, 140 ), 141 (1, 0) 142 ) 143 self.assertNotEqual( 144 keyword_arguments( 145 last_input=0, first_input=1, 146 ), 147 (1, 0) 148 )150 a_tuple = (0, 1, 2, 'n') 151 a_list = [0, 1, 2, 'n'] 152 assert_equal( 153 keyword_arguments( 154 first_input=a_tuple, 155 last_input=a_list, 156 ), 157 (a_tuple, a_list) 158 ) 159 self.assertNotEqual( 160 keyword_arguments( 161 first_input=a_tuple, 162 last_input=a_list, 163 ), 164 (a_tuple, a_list) 165 )167 a_set = {0, 1, 2, 'n'} 168 a_dictionary = {'key': 'value'} 169 assert_equal( 170 src.functions.positional_arguments( 171 last_input=a_dictionary, 172 first_input=a_set, 173 ), 174 (a_set, a_dictionary) 175 ) 176 self.assertNotEqual( 177 src.functions.positional_arguments( 178 last_input=a_dictionary, 179 first_input=a_set, 180 ), 181 (a_set, a_dictionary) 182 ) 183 184 185def test_args_and_kwargs():the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_keyword_arguments
112 assert_equal( 113 keyword_arguments( 114 first_input=first, last_input=last, 115 ), 116 (first, last) 117 ) 118 # self.assertNotEqual( 119 self.assertEqual( 120 keyword_arguments( 121 first_input=first, last_input=last, 122 ), 123 (first, last) 124 ) 125 assert_equal( 126 keyword_arguments( 127 last_input=last, first_input=first, 128 ), 129 (first, last) 130 ) 131 # self.assertNotEqual( 132 self.assertEqual( 133 keyword_arguments( 134 last_input=last, first_input=first, 135 ), 136 (first, last) 137 )139 assert_equal( 140 keyword_arguments( 141 last_input=0, first_input=1, 142 ), 143 (1, 0) 144 ) 145 # self.assertNotEqual( 146 self.assertEqual( 147 keyword_arguments( 148 last_input=0, first_input=1, 149 ), 150 (1, 0) 151 )153 a_tuple = (0, 1, 2, 'n') 154 a_list = [0, 1, 2, 'n'] 155 assert_equal( 156 keyword_arguments( 157 first_input=a_tuple, 158 last_input=a_list, 159 ), 160 (a_tuple, a_list) 161 ) 162 # self.assertNotEqual( 163 self.assertEqual( 164 keyword_arguments( 165 first_input=a_tuple, 166 last_input=a_list, 167 ), 168 (a_tuple, a_list) 169 )171 a_set = {0, 1, 2, 'n'} 172 a_dictionary = {'key': 'value'} 173 assert_equal( 174 src.functions.positional_arguments( 175 last_input=a_dictionary, 176 first_input=a_set, 177 ), 178 (a_set, a_dictionary) 179 ) 180 # self.assertNotEqual( 181 self.assertEqual( 182 src.functions.positional_arguments( 183 last_input=a_dictionary, 184 first_input=a_set, 185 ), 186 (a_set, a_dictionary) 187 ) 188 189 190def test_args_and_kwargs():the test passes.
I remove the commented lines and the calls to my assert_equal function from 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(self): 106 keyword_arguments = ( 107 src.functions.keyword_arguments 108 ) 109 first, last = 'first', 'last'111 self.assertEqual( 112 keyword_arguments( 113 first_input=first, last_input=last, 114 ), 115 (first, last) 116 ) 117 self.assertEqual( 118 keyword_arguments( 119 last_input=last, first_input=first, 120 ), 121 (first, last) 122 )124 self.assertEqual( 125 keyword_arguments( 126 last_input=0, first_input=1, 127 ), 128 (1, 0) 129 )131 a_tuple = (0, 1, 2, 'n') 132 a_list = [0, 1, 2, 'n'] 133 self.assertEqual( 134 keyword_arguments( 135 first_input=a_tuple, 136 last_input=a_list, 137 ), 138 (a_tuple, a_list) 139 )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 152def test_args_and_kwargs():I add a git commit message in the other terminal
git commit -am \ 'move test_keyword_arguments to TestFunctions'
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
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 first, last = 'first', 'last' 153 154 assert_equal( 155 src.functions.args_and_kwargs( 156 first, last_input=last 157 ), 158 (first, last) 159 ) 160 161 162def test_optional_arguments():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 class (
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
151 # def test_args_and_kwargs(): 152 def test_args_and_kwargs(self): 153 first, last = 'first', 'last' 154 155 assert_equal( 156 src.functions.args_and_kwargs( 157 first, last_input=last 158 ), 159 (first, last) 160 ) 161 self.assertNotEqual( 162 src.functions.args_and_kwargs( 163 first, last_input=last 164 ), 165 (first, last) 166 ) 167 168 169def test_optional_arguments():the terminal is my friend, and shows AssertionError
AssertionError: ('first', 'last') == ('first', 'last')I change the call from assertNotEqual to assertEqual in test_args_and_kwargs
155 assert_equal( 156 src.functions.args_and_kwargs( 157 first, last_input=last 158 ), 159 (first, last) 160 ) 161 # self.assertNotEqual( 162 self.assertEqual( 163 src.functions.args_and_kwargs( 164 first, last_input=last 165 ), 166 (first, last) 167 )the test passes.
I add variables for the call to
src.functions.args_and_kwargsand my expectation155 assert_equal( 156 src.functions.args_and_kwargs( 157 first, last_input=last 158 ), 159 (first, last) 160 ) 161 # self.assertNotEqual( 162 self.assertEqual( 163 src.functions.args_and_kwargs( 164 first, last_input=last 165 ), 166 (first, last) 167 )I remove the commented lines and the call to my assert_equal function from 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(self): 152 first, last = 'first', 'last' 153 154 self.assertEqual( 155 src.functions.args_and_kwargs( 156 first, last_input=last 157 ), 158 (first, last) 159 ) 160 161 162def test_optional_arguments():I add a git commit message in the other terminal
git commit -am \ 'move test_args_and_kwargs to TestFunctions'
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
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 optional_arguments = ( 163 src.functions.optional_arguments 164 ) 165 166 first_name, last_name = 'jane', 'doe' 167 assert_equal( 168 optional_arguments( 169 first_name, 170 ), 171 (first_name, last_name) 172 )174 first_name, blow = 'joe', 'blow' 175 assert_equal( 176 optional_arguments( 177 first_name, blow 178 ), 179 (first_name, blow) 180 )182 first_name = 'john' 183 assert_equal( 184 optional_arguments( 185 first_input=first_name, 186 ), 187 (first_name, last_name) 188 )190 last_name = 'smith' 191 assert_equal( 192 optional_arguments( 193 last_input=last_name, 194 first_input=first_name, 195 ), 196 (first_name, last_name) 197 ) 198 199 200def test_unknown_number_of_arguments():the terminal is my friend, and shows TypeError
TypeError: TestFunctions.test_optional_arguments() takes 0 positional arguments but 1 was given
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
167 first_name, last_name = 'jane', 'doe' 168 assert_equal( 169 optional_arguments( 170 first_name, 171 ), 172 (first_name, last_name) 173 ) 174 self.assertNotEqual( 175 optional_arguments( 176 first_name, 177 ), 178 (first_name, last_name) 179 )181 first_name, blow = 'joe', 'blow' 182 assert_equal( 183 optional_arguments( 184 first_name, blow 185 ), 186 (first_name, blow) 187 ) 188 self.assertNotEqual( 189 optional_arguments( 190 first_name, blow 191 ), 192 (first_name, blow) 193 )195 first_name = 'john' 196 assert ( 197 optional_arguments( 198 first_input=first_name 199 ) 200 == (first_name, last_name) 201 ) 202 self.assertNotEqual( 203 optional_arguments( 204 first_input=first_name 205 ), 206 (first_name, last_name) 207 )209 last_name = 'smith' 210 assert ( 211 optional_arguments( 212 last_input=last_name, 213 first_input=first_name, 214 ) 215 == (first_name, last_name) 216 ) 217 self.assertNotEqual( 218 optional_arguments( 219 last_input=last_name, 220 first_input=first_name, 221 ), 222 (first_name, last_name) 223 ) 224 225 226def test_unknown_number_of_arguments():the terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_optional_arguments
167 first_name, last_name = 'jane', 'doe' 168 assert_equal( 169 optional_arguments( 170 first_name, 171 ), 172 (first_name, last_name) 173 ) 174 # self.assertNotEqual( 175 self.assertEqual( 176 optional_arguments( 177 first_name, 178 ), 179 (first_name, last_name) 180 )182 first_name, blow = 'joe', 'blow' 183 assert_equal( 184 optional_arguments( 185 first_name, blow 186 ), 187 (first_name, blow) 188 ) 189 # self.assertNotEqual( 190 self.assertEqual( 191 optional_arguments( 192 first_name, blow 193 ), 194 (first_name, blow) 195 )197 first_name = 'john' 198 assert_equal( 199 optional_arguments( 200 first_input=first_name, 201 ), 202 (first_name, last_name) 203 ) 204 # self.assertNotEqual( 205 self.assertEqual( 206 optional_arguments( 207 first_input=first_name, 208 ), 209 (first_name, last_name) 210 )212 last_name = 'smith' 213 assert_equal( 214 optional_arguments( 215 last_input=last_name, 216 first_input=first_name, 217 ), 218 (first_name, last_name) 219 ) 220 # self.assertNotEqual( 221 self.assertEqual( 222 optional_arguments( 223 last_input=last_name, 224 first_input=first_name, 225 ), 226 (first_name, last_name) 227 ) 228 229 230 def test_unknown_number_of_arguments():the test passes.
I remove the commented lines and the calls to my assert_equal function from 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(self): 162 optional_arguments = ( 163 src.functions.optional_arguments 164 ) 165 166 first_name, last_name = 'jane', 'doe' 167 self.assertEqual( 168 optional_arguments( 169 first_name, 170 ), 171 (first_name, last_name) 172 )174 first_name, blow = 'joe', 'blow' 175 self.assertEqual( 176 optional_arguments( 177 first_name, blow 178 ), 179 (first_name, blow) 180 )182 first_name = 'john' 183 self.assertEqual( 184 optional_arguments( 185 first_input=first_name, 186 ), 187 (first_name, last_name) 188 )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 200def test_unknown_number_of_arguments():I add a git commit message in the other terminal
git commit -am \ 'move test_optional_arguments to TestFunctions'
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
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 unknown_number_of_arguments = ( 201 src.functions.unknown_number_of_arguments 202 ) 203 204 a_tuple = (0, 1) 205 a_dictionary = {'a': 2, 'b': 3} 206 assert_equal( 207 unknown_number_of_arguments( 208 *a_tuple, **a_dictionary 209 ), 210 (a_tuple, a_dictionary) 211 )213 a_dictionary = {'a': 2, 'b': 3, 'c': 4} 214 assert_equal( 215 unknown_number_of_arguments( 216 *a_tuple, **a_dictionary, 217 ), 218 (a_tuple, a_dictionary) 219 )221 a_tuple = (0, 1, 2) 222 a_dictionary = {'a': 3, 'b': 4, 'c': 5} 223 assert_equal( 224 unknown_number_of_arguments( 225 *a_tuple, **a_dictionary 226 ), 227 (a_tuple, a_dictionary) 228 )230 a_tuple = (0, 1, 2, 'n') 231 assert_equal( 232 unknown_number_of_arguments(*a_tuple), 233 (a_tuple, {}) 234 )236 a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'} 237 assert_equal( 238 unknown_number_of_arguments(**a_dictionary), 239 ((), a_dictionary) 240 ) 241 242 assert_equal( 243 unknown_number_of_arguments(), ((), {}) 244 ) 245 246 247# Exceptions seenthe 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 class (
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
205 a_tuple = (0, 1) 206 a_dictionary = {'a': 2, 'b': 3} 207 assert_equal( 208 unknown_number_of_arguments( 209 *a_tuple, **a_dictionary 210 ), 211 (a_tuple, a_dictionary) 212 ) 213 self.assertNotEqual( 214 unknown_number_of_arguments( 215 *a_tuple, **a_dictionary 216 ), 217 (a_tuple, a_dictionary) 218 )220 a_dictionary = {'a': 2, 'b': 3, 'c': 4} 221 assert_equal( 222 unknown_number_of_arguments( 223 *a_tuple, **a_dictionary, 224 ), 225 (a_tuple, a_dictionary) 226 ) 227 self.assertNotEqual( 228 unknown_number_of_arguments( 229 *a_tuple, **a_dictionary 230 ), 231 (a_tuple, a_dictionary) 232 )234 a_tuple = (0, 1, 2) 235 a_dictionary = {'a': 3, 'b': 4, 'c': 5} 236 assert_equal( 237 unknown_number_of_arguments( 238 *a_tuple, **a_dictionary 239 ), 240 (a_tuple, a_dictionary) 241 ) 242 self.assertNotEqual( 243 unknown_number_of_arguments( 244 *a_tuple, **a_dictionary 245 ), 246 (a_tuple, a_dictionary) 247 )249 a_tuple = (0, 1, 2, 'n') 250 assert_equal( 251 unknown_number_of_arguments(*a_tuple), 252 (a_tuple, {}) 253 ) 254 self.assertNotEqual( 255 unknown_number_of_arguments(*a_tuple), 256 (a_tuple, {}) 257 )259 a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'} 260 assert_equal( 261 unknown_number_of_arguments(**a_dictionary), 262 ((), a_dictionary) 263 ) 264 self.assertNotEqual( 265 unknown_number_of_arguments(**a_dictionary), 266 ((), a_dictionary) 267 ) 268 269 assert_equal( 270 unknown_number_of_arguments(), ((), {}) 271 ) 272 self.assertNotEqual( 273 unknown_number_of_arguments(), ((), {}) 274 ) 275 276 277# Exceptions seenthe terminal is my friend, and shows AssertionError.
I change the calls from assertNotEqual to assertEqual in test_unknown_number_of_arguments
205 a_tuple = (0, 1) 206 a_dictionary = {'a': 2, 'b': 3} 207 assert_equal( 208 unknown_number_of_arguments( 209 *a_tuple, **a_dictionary 210 ), 211 (a_tuple, a_dictionary) 212 ) 213 # self.assertNotEqual( 214 self.assertEqual( 215 unknown_number_of_arguments( 216 *a_tuple, **a_dictionary 217 ), 218 (a_tuple, a_dictionary) 219 )221 a_dictionary = {'a': 2, 'b': 3, 'c': 4} 222 assert_equal( 223 unknown_number_of_arguments( 224 *a_tuple, **a_dictionary, 225 ), 226 (a_tuple, a_dictionary) 227 ) 228 # self.assertNotEqual( 229 self.assertEqual( 230 unknown_number_of_arguments( 231 *a_tuple, **a_dictionary 232 ), 233 (a_tuple, a_dictionary) 234 )236 a_tuple = (0, 1, 2) 237 a_dictionary = {'a': 3, 'b': 4, 'c': 5} 238 assert_equal( 239 unknown_number_of_arguments( 240 *a_tuple, **a_dictionary 241 ), 242 (a_tuple, a_dictionary) 243 ) 244 # self.assertNotEqual( 245 self.assertEqual( 246 unknown_number_of_arguments( 247 *a_tuple, **a_dictionary 248 ), 249 (a_tuple, a_dictionary) 250 )252 a_tuple = (0, 1, 2, 'n') 253 assert_equal( 254 unknown_number_of_arguments(*a_tuple), 255 (a_tuple, {}) 256 ) 257 # self.assertNotEqual( 258 self.assertEqual( 259 unknown_number_of_arguments(*a_tuple), 260 (a_tuple, {}) 261 )263 a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'} 264 assert_equal( 265 unknown_number_of_arguments(**a_dictionary), 266 ((), a_dictionary) 267 ) 268 # self.assertNotEqual( 269 self.assertEqual( 270 unknown_number_of_arguments(**a_dictionary), 271 ((), a_dictionary) 272 ) 273 274 assert_equal( 275 unknown_number_of_arguments(), ((), {}) 276 ) 277 # self.assertNotEqual( 278 self.assertEqual( 279 unknown_number_of_arguments(), ((), {}) 280 ) 281 282 283# Exceptions seenthe test passes.
I remove the commented lines and the calls to my assert_equal function from 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(self): 200 unknown_number_of_arguments = ( 201 src.functions.unknown_number_of_arguments 202 ) 203 204 a_tuple = (0, 1) 205 a_dictionary = {'a': 2, 'b': 3} 206 self.assertEqual( 207 unknown_number_of_arguments( 208 *a_tuple, **a_dictionary 209 ), 210 (a_tuple, a_dictionary) 211 )213 a_dictionary = {'a': 2, 'b': 3, 'c': 4} 214 self.assertEqual( 215 unknown_number_of_arguments( 216 *a_tuple, **a_dictionary 217 ), 218 (a_tuple, a_dictionary) 219 )221 a_tuple = (0, 1, 2) 222 a_dictionary = {'a': 3, 'b': 4, 'c': 5} 223 self.assertEqual( 224 unknown_number_of_arguments( 225 *a_tuple, **a_dictionary 226 ), 227 (a_tuple, a_dictionary) 228 )230 a_tuple = (0, 1, 2, 'n') 231 self.assertEqual( 232 unknown_number_of_arguments(*a_tuple), 233 (a_tuple, {}) 234 )236 a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'} 237 self.assertEqual( 238 unknown_number_of_arguments(**a_dictionary), 239 ((), a_dictionary) 240 ) 241 242 self.assertEqual( 243 unknown_number_of_arguments(), ((), {}) 244 ) 245 246 247# Exceptions seen 248# AssertionError 249# NameError 250# TypeError 251# SyntaxError 252# AttributeErrorI 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):I add a git commit message in the other terminal
git commit -am \ 'move test_unknown_number_of_arguments to TestFunctions'
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'9class TestFunctions(unittest.TestCase): 10 11 first = 'first' 12 last = 'last' 13 14 def test_making_a_function_w_pass(self):I use the object attributes for
firstandlastin test_positional_arguments69 def test_positional_arguments(self): 70 positional_arguments = ( 71 src.functions.positional_arguments 72 ) 73 # first, last = 'first', 'last' 74 75 self.assertEqual( 76 # positional_arguments(first, last), 77 positional_arguments(self.first, self.last), 78 # (first, last) 79 (self.first, self.last) 80 ) 81 self.assertEqual( 82 # positional_arguments(last, first), 83 positional_arguments(self.last, self.first), 84 # (last, first) 85 (self.last, self.first) 86 )the test is still green.
I use the object attributes for
firstandlastin test_keyword_arguments108 def test_keyword_arguments(self): 109 keyword_arguments = ( 110 src.functions.keyword_arguments 111 ) 112 # first, last = 'first', 'last' 113 114 self.assertEqual( 115 keyword_arguments( 116 # first_input=first, last_input=last, 117 first_input=self.first, 118 last_input=self.last, 119 ), 120 # (first, last) 121 (self.first, self.last) 122 ) 123 self.assertEqual( 124 keyword_arguments( 125 # last_input=last, first_input=first, 126 last_input=self.last, 127 first_input=self.first, 128 ), 129 # (first, last) 130 (self.first, self.last) 131 )still green.
I use the object attributes for
firstandlastin test_args_and_kwargs160 def test_args_and_kwargs(self): 161 # first, last = 'first', 'last' 162 163 self.assertEqual( 164 src.functions.args_and_kwargs( 165 # first, last_input=last 166 self.first, last_input=self.last 167 ), 168 # (first, last) 169 (self.first, self.last) 170 ) 171 172 def test_optional_arguments(self):green.
I remove the commented lines from test_args_and_kwargs
160 def test_args_and_kwargs(self): 161 self.assertEqual( 162 src.functions.args_and_kwargs( 163 self.first, last_input=self.last 164 ), 165 (self.first, self.last) 166 ) 167 168 def test_optional_arguments(self):I add a git commit message in the other terminal
git commit -am \ 'extract first, last object attributes'
extract a_tuple object attribute
I add a object attribute for
(0, 1, 2, 'n')9class TestFunctions(unittest.TestCase): 10 11 first = 'first' 12 last = 'last' 13 a_tuple = (0, 1, 2, 'n') 14 15 def test_making_a_function_w_pass(self):I use the new object attribute for
a_tuplein test_positional_arguments89 self.assertEqual( 90 positional_arguments(0, 1), (0, 1) 91 ) 92 93 # a_tuple = (0, 1, 2, 'n') 94 a_list = [0, 1, 2, 'n'] 95 self.assertEqual( 96 # positional_arguments(a_tuple, a_list), 97 positional_arguments( 98 self.a_tuple, a_list 99 ), 100 # (a_tuple, a_list) 101 (self.a_tuple, a_list) 102 ) 103 104 a_set = {0, 1, 2, 'n'}still green.
I use the new object attribute for
a_tuplein test_keyword_arguments138 self.assertEqual( 139 keyword_arguments( 140 last_input=0, first_input=1, 141 ), 142 (1, 0) 143 ) 144 145 # a_tuple = (0, 1, 2, 'n') 146 a_list = [0, 1, 2, 'n'] 147 self.assertEqual( 148 keyword_arguments( 149 # first_input=a_tuple, 150 first_input=self.a_tuple, 151 last_input=a_list, 152 ), 153 # (a_tuple, a_list) 154 (self.a_tuple, a_list) 155 ) 156 157 a_set = {0, 1, 2, 'n'}the test is still green.
I add a git commit message in the other terminal
git commit -am \ 'extract a_tuple object attribute'
extract a_list object attribute
I add a object attribute for
[0, 1, 2, 'n']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 16 def test_making_a_function_w_pass(self):I use the new object attribute for
a_listin test_positional_arguments94 # a_tuple = (0, 1, 2, 'n') 95 # a_list = [0, 1, 2, 'n'] 96 self.assertEqual( 97 # positional_arguments(a_tuple, a_list), 98 positional_arguments( 99 # self.a_tuple, a_list 100 self.a_tuple, self.a_list 101 ), 102 # (a_tuple, a_list) 103 # (self.a_tuple, a_list) 104 (self.a_tuple, self.a_list) 105 ) 106 107 a_set = {0, 1, 2, 'n'}still green.
I use the new object attribute for
a_listin test_keyword_arguments148 # a_tuple = (0, 1, 2, 'n') 149 # a_list = [0, 1, 2, 'n'] 150 self.assertEqual( 151 keyword_arguments( 152 # first_input=a_tuple, 153 first_input=self.a_tuple, 154 # last_input=a_list, 155 last_input=self.a_list, 156 ), 157 # (a_tuple, a_list) 158 # (self.a_tuple, a_list) 159 (self.a_tuple, self.a_list) 160 ) 161 162 a_set = {0, 1, 2, 'n'}green.
I add a git commit message in the other terminal
git commit -am \ 'extract a_list object attribute'
extract a_set object attribute
I add a object attribute for
{0, 1, 2, 'n'}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 17 def test_making_a_function_w_pass(self):I use the new object attribute to remove repetition of
{0, 1, 2, 'n'}from test_positional_arguments108 # a_set = {0, 1, 2, 'n'} 109 a_dictionary = {'key': 'value'} 110 self.assertEqual( 111 src.functions.keyword_arguments( 112 # a_set, a_dictionary 113 self.a_set, a_dictionary 114 ), 115 # (a_set, a_dictionary) 116 (self.a_set, a_dictionary) 117 ) 118 119 def test_keyword_arguments(self):still green.
I use the new object attributes to remove repetition of
{0, 1, 2, 'n'}from test_keyword_arguments165 # a_set = {0, 1, 2, 'n'} 166 a_dictionary = {'key': 'value'} 167 self.assertEqual( 168 src.functions.positional_arguments( 169 last_input=a_dictionary, 170 # first_input=a_set, 171 first_input=self.a_set, 172 ), 173 # (a_set, a_dictionary) 174 (self.a_set, a_dictionary) 175 ) 176 177 def test_args_and_kwargs(self):still green
I add a git commit message in the other terminal
git commit -am \ 'extract a_set object attribute'
extract a_dictionary object attribute
I add a object attribute for
a_dictionary9class 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'} 17 18 def test_making_a_function_w_pass(self):I use the new object attribute for
a_dictionaryin test_positional_arguments109 # a_set = {0, 1, 2, 'n'} 110 # a_dictionary = {'key': 'value'} 111 self.assertEqual( 112 src.functions.keyword_arguments( 113 # a_set, a_dictionary 114 # self.a_set, a_dictionary 115 self.a_set, self.a_dictionary 116 ), 117 # (a_set, a_dictionary) 118 # (self.a_set, a_dictionary) 119 (self.a_set, self.a_dictionary) 120 ) 121 122 def test_keyword_arguments(self):the test is still green.
I remove the commented lines from test_positional_arguments
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 )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 ) 104 105 def test_keyword_arguments(self):I use the new object attributes for
a_dictionaryin test_keyword_arguments151 # a_set = {0, 1, 2, 'n'} 152 # a_dictionary = {'key': 'value'} 153 self.assertEqual( 154 src.functions.positional_arguments( 155 # last_input=a_dictionary, 156 last_input=self.a_dictionary, 157 # first_input=a_set, 158 first_input=self.a_set, 159 ), 160 # (a_set, a_dictionary) 161 # (self.a_set, a_dictionary) 162 (self.a_set, self.a_dictionary) 163 ) 164 165 def test_args_and_kwargs(self):the test is still green.
I remove the commented lines from test_keyword_arguments
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 )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 ) 147 148 def test_args_and_kwargs(self):I add a git commit message in the other terminal
git commit -am \ 'extract a_dictionary object attribute'
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 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.