test person with unittest
I want to use the unittest library in the person project.
preview
I have these tests by the end of the chapter
1import unittest
2import src.person
3
4
5class TestPerson(unittest.TestCase):
6
7 def test_joe(self):
8 first_name = 'joe'
9 last_name = 'blow'
10 sex = 'M'
11 year_of_birth = 1996
12
13 reality = src.person.factory(
14 first_name=first_name,
15 last_name=last_name,
16 sex=sex,
17 year_of_birth=year_of_birth,
18 )
19 my_expectation = (
20 f'{first_name}, {last_name},'
21 f' {sex}, {year_of_birth}'
22 )
23 assert reality == my_expectation
24 self.assertEqual(reality, my_expectation)
25
26 reality = src.person.say_hello(
27 first_name=first_name,
28 last_name=last_name,
29 year_of_birth=year_of_birth,
30 )
31 my_expectation = (
32 f'Hello, my name is {first_name}'
33 f' {last_name} and I am'
34 f' {2026-year_of_birth}.'
35 )
36 assert reality == my_expectation
37 self.assertEqual(reality, my_expectation)
38
39 joe = src.person.Person(
40 first_name=first_name,
41 last_name=last_name,
42 sex=sex,
43 year_of_birth=year_of_birth,
44 )
45
46 reality = joe.say_hello()
47 assert reality == my_expectation
48 self.assertEqual(reality, my_expectation)
49
50 def test_jane(self):
51 first_name = 'jane'
52 last_name = 'doe'
53 sex = 'F'
54 year_of_birth = 1991
55
56 reality = src.person.factory(
57 first_name=first_name,
58 last_name=last_name,
59 sex=sex,
60 year_of_birth=year_of_birth,
61 )
62 my_expectation = (
63 f'{first_name}, {last_name},'
64 f' {sex}, {year_of_birth}'
65 )
66 assert reality == my_expectation
67 self.assertEqual(reality, my_expectation)
68
69 reality = src.person.say_hello(
70 first_name=first_name,
71 last_name=last_name,
72 year_of_birth=year_of_birth,
73 )
74 my_expectation = (
75 f'Hello, my name is {first_name}'
76 f' {last_name} and I am'
77 f' {2026-year_of_birth}.'
78 )
79 assert reality == my_expectation
80 self.assertEqual(reality, my_expectation)
81
82 jane = src.person.Person(
83 first_name=first_name,
84 last_name=last_name,
85 sex=sex,
86 year_of_birth=year_of_birth,
87 )
88
89 reality = jane.say_hello()
90 assert reality == my_expectation
91 self.assertEqual(reality, my_expectation)
92
93 def test_john(self):
94 first_name = 'john'
95 last_name = 'smith'
96 sex = 'M'
97 year_of_birth = 1580
98
99 reality = src.person.factory(
100 first_name=first_name,
101 last_name=last_name,
102 sex=sex,
103 year_of_birth=year_of_birth,
104 )
105 my_expectation = (
106 f'{first_name}, {last_name},'
107 f' {sex}, {year_of_birth}'
108 )
109 assert reality == my_expectation
110 self.assertEqual(reality, my_expectation)
111
112 reality = src.person.say_hello(
113 first_name=first_name,
114 last_name=last_name,
115 year_of_birth=year_of_birth,
116 )
117 my_expectation = (
118 f'Hello, my name is {first_name}'
119 f' {last_name} and I am'
120 f' {2026-year_of_birth}.'
121 )
122 assert reality == my_expectation
123 self.assertEqual(reality, my_expectation)
124
125 john = src.person.Person(
126 first_name=first_name,
127 last_name=last_name,
128 sex=sex,
129 year_of_birth=year_of_birth,
130 )
131
132 reality = john.say_hello()
133 assert reality == my_expectation
134 self.assertEqual(reality, my_expectation)
135
136 def test_mary(self):
137 first_name = 'mary'
138 last_name = 'public'
139 sex = 'F'
140 year_of_birth = 2000
141
142 reality = src.person.factory(
143 first_name=first_name,
144 last_name=last_name,
145 sex=sex,
146 year_of_birth=year_of_birth,
147 )
148 my_expectation = (
149 f'{first_name}, {last_name},'
150 f' {sex}, {year_of_birth}'
151 )
152 assert reality == my_expectation
153 self.assertEqual(reality, my_expectation)
154
155 reality = src.person.say_hello(
156 first_name=first_name,
157 last_name=last_name,
158 year_of_birth=year_of_birth,
159 )
160 my_expectation = (
161 f'Hello, my name is {first_name}'
162 f' {last_name} and I am'
163 f' {2026-year_of_birth}.'
164 )
165 assert reality == my_expectation
166 self.assertEqual(reality, my_expectation)
167
168 mary = src.person.Person(
169 first_name=first_name,
170 last_name=last_name,
171 sex=sex,
172 year_of_birth=year_of_birth,
173 )
174
175 reality = mary.say_hello()
176 assert reality == my_expectation
177 self.assertEqual(reality, my_expectation)
178
179 def test_dir_person_class(self):
180 reality = dir(src.person.Person)
181 my_expectation = [
182 '__class__',
183 '__delattr__',
184 '__dict__',
185 '__dir__',
186 '__doc__',
187 '__eq__',
188 '__firstlineno__',
189 '__format__',
190 '__ge__',
191 '__getattribute__',
192 '__getstate__',
193 '__gt__',
194 '__hash__',
195 '__init__',
196 '__init_subclass__',
197 '__le__',
198 '__lt__',
199 '__module__',
200 '__ne__',
201 '__new__',
202 '__reduce__',
203 '__reduce_ex__',
204 '__repr__',
205 '__setattr__',
206 '__sizeof__',
207 '__static_attributes__',
208 '__str__',
209 '__subclasshook__',
210 '__weakref__',
211 'say_hello'
212 ]
213 assert reality == my_expectation
214 self.assertEqual(reality, my_expectation)
215
216 def test_dir_person_instance(self):
217 an_instance_of_person = src.person.Person(
218 first_name='first_name',
219 last_name='last_name',
220 sex='M',
221 year_of_birth=2026,
222 )
223
224 reality = dir(an_instance_of_person)
225 my_expectation = [
226 '__class__',
227 '__delattr__',
228 '__dict__',
229 '__dir__',
230 '__doc__',
231 '__eq__',
232 '__firstlineno__',
233 '__format__',
234 '__ge__',
235 '__getattribute__',
236 '__getstate__',
237 '__gt__',
238 '__hash__',
239 '__init__',
240 '__init_subclass__',
241 '__le__',
242 '__lt__',
243 '__module__',
244 '__ne__',
245 '__new__',
246 '__reduce__',
247 '__reduce_ex__',
248 '__repr__',
249 '__setattr__',
250 '__sizeof__',
251 '__static_attributes__',
252 '__str__',
253 '__subclasshook__',
254 '__weakref__',
255 'first_name',
256 'last_name',
257 'say_hello',
258 'sex',
259 'year_of_birth',
260 ]
261 assert reality == my_expectation
262 self.assertEqual(reality, my_expectation)
263
264
265# Exceptions seen
266# AssertionError
267# NameError
268# TypeError
269# AttributeError
270# SyntaxError
open the project
I open a terminal
I change directory to the project
cd personI open
test_person.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
tests/test_person.py ...... [100%] =================== 6 passed in A.BCs ====================
add TestPerson class
RED: make it fail
I add a class named TestPerson to test_person.py
1import src.person
2
3
4class TestPerson(object):
5
6 def test_failure(self):
7 self.assertEqual(False, True)
8
9
10def test_joe():
the terminal is my friend, and shows AttributeError
AttributeError: 'TestPerson' object
has no attribute 'assertEqual'
GREEN: make it pass
I add unittest.TestCase as the parent class of
TestPerson1import src.person 2 3 4# class TestPerson(object): 5class TestPerson(unittest.TestCase):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 unittest 2import src.person 3 4 5# class TestPerson(object): 6class TestPerson(unittest.TestCase):the terminal is my friend, and shows AssertionError
AssertionError: False != TrueI change False to True in the assertion
5# class TestPerson(object): 6class TestPerson(unittest.TestCase): 7 8 def test_failure(self): 9 # self.assertEqual(False, True) 10 self.assertEqual(True, True) 11 12 13def test_joe():the test passes.
REFACTOR: make it better
I remove the commented lines
1import unittest 2import src.person 3 4 5class TestPerson(unittest.TestCase): 6 7 def test_failure(self): 8 self.assertEqual(True, True) 9 10 11def test_joe():I open a new terminal then make sure I am in the
personfoldercd personI add a git commit message in the new terminal
git commit -am \ 'add TestPerson class'
test_joe with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_joe to make it a method of the TestPerson class and replace
test_failure5class TestPerson(unittest.TestCase): 6 7 def test_joe(): 8 first_name = 'joe' 9 last_name = 'blow' 10 sex = 'M' 11 year_of_birth = 1996 12 13 reality = src.person.factory( 14 first_name=first_name, 15 last_name=last_name, 16 sex=sex, 17 year_of_birth=year_of_birth, 18 ) 19 my_expectation = ( 20 f'{first_name}, {last_name},' 21 f' {sex}, {year_of_birth}' 22 ) 23 assert reality == my_expectation 24 25 reality = src.person.say_hello( 26 first_name=first_name, 27 last_name=last_name, 28 year_of_birth=year_of_birth, 29 ) 30 my_expectation = ( 31 f'Hello, my name is {first_name}' 32 f' {last_name} and I am' 33 f' {2026-year_of_birth}.' 34 ) 35 assert reality == my_expectation 36 37 joe = src.person.Person( 38 first_name=first_name, 39 last_name=last_name, 40 sex=sex, 41 year_of_birth=year_of_birth, 42 ) 43 44 reality = joe.say_hello() 45 assert reality == my_expectation 46 47 48def test_jane():the terminal is my friend, and shows TypeError
TypeError: TestPerson.test_joe() 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_joe
7 # def test_joe():
8 def test_joe(self):
the test passes.
REFACTOR: make it better
I add a call to the assertNotEqual method for the factory function
7 # def test_joe(): 8 def test_joe(self): 9 first_name = 'joe' 10 last_name = 'blow' 11 sex = 'M' 12 year_of_birth = 1996 13 14 reality = src.person.factory( 15 first_name=first_name, 16 last_name=last_name, 17 sex=sex, 18 year_of_birth=year_of_birth, 19 ) 20 my_expectation = ( 21 f'{first_name}, {last_name},' 22 f' {sex}, {year_of_birth}' 23 ) 24 assert reality == my_expectation 25 self.assertNotEqual(reality, my_expectation)the terminal is my friend, and shows AssertionError
AssertionError: 'joe, blow, M, 1996' == 'joe, blow, M, 1996'I change assertNotEqual to assertEqual
24 assert reality == my_expectation 25 # self.assertNotEqual(reality, my_expectation) 26 self.assertEqual(reality, my_expectation) 27 28 reality = src.person.say_hello( 29 first_name=first_name, 30 last_name=last_name, 31 year_of_birth=year_of_birth, 32 ) 33 my_expectation = ( 34 f'Hello, my name is {first_name}' 35 f' {last_name} and I am' 36 f' {2026-year_of_birth}.' 37 ) 38 assert reality == my_expectationthe test passes.
I add a call to the assertNotEqual method for the say_hello function
28 reality = src.person.say_hello( 29 first_name=first_name, 30 last_name=last_name, 31 year_of_birth=year_of_birth, 32 ) 33 my_expectation = ( 34 f'Hello, my name is {first_name}' 35 f' {last_name} and I am' 36 f' {2026-year_of_birth}.' 37 ) 38 assert reality == my_expectation 39 self.assertNotEqual(reality, my_expectation)the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is joe blow and I am 30.' == 'Hello, my name is joe blow and I am 30.'I change assertNotEqual to assertEqual
38 assert reality == my_expectation 39 # self.assertNotEqual(reality, my_expectation) 40 self.assertEqual(reality, my_expectation) 41 42 joe = src.person.Person( 43 first_name=first_name, 44 last_name=last_name, 45 sex=sex, 46 year_of_birth=year_of_birth, 47 ) 48 49 reality = joe.say_hello()the test passes.
I add a call to the assertNotEqual method for the say_hello method of the Person class
49 reality = joe.say_hello() 50 assert reality == my_expectation 51 self.assertNotEqual(reality, my_expectation) 52 53 54def test_jane():the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is joe blow and I am 30.' == 'Hello, my name is joe blow and I am 30.'I change assertNotEqual to assertEqual
49 reality = joe.say_hello() 50 assert reality == my_expectation 51 # self.assertNotEqual(reality, my_expectation) 52 self.assertEqual(reality, my_expectation) 53 54 55def test_jane():the test passes.
I remove the commented lines from test_joe
5class TestPerson(unittest.TestCase): 6 7 def test_joe(self): 8 first_name = 'joe' 9 last_name = 'blow' 10 sex = 'M' 11 year_of_birth = 1996 12 13 reality = src.person.factory( 14 first_name=first_name, 15 last_name=last_name, 16 sex=sex, 17 year_of_birth=year_of_birth, 18 ) 19 my_expectation = ( 20 f'{first_name}, {last_name},' 21 f' {sex}, {year_of_birth}' 22 ) 23 assert reality == my_expectation 24 self.assertEqual(reality, my_expectation) 25 26 reality = src.person.say_hello( 27 first_name=first_name, 28 last_name=last_name, 29 year_of_birth=year_of_birth, 30 ) 31 my_expectation = ( 32 f'Hello, my name is {first_name}' 33 f' {last_name} and I am' 34 f' {2026-year_of_birth}.' 35 ) 36 assert reality == my_expectation 37 self.assertEqual(reality, my_expectation) 38 39 joe = src.person.Person( 40 first_name=first_name, 41 last_name=last_name, 42 sex=sex, 43 year_of_birth=year_of_birth, 44 ) 45 46 reality = joe.say_hello() 47 assert reality == my_expectation 48 self.assertEqual(reality, my_expectation) 49 50 51def test_jane():I add a git commit message in the other terminal
git commit -am 'move test_joe to TestPerson'
test_jane with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_jane to make it a method of the TestPerson class
46 reality = joe.say_hello() 47 assert reality == my_expectation 48 self.assertEqual(reality, my_expectation) 49 50 def test_jane(): 51 first_name = 'jane' 52 last_name = 'doe' 53 sex = 'F' 54 year_of_birth = 1991 55 56 reality = src.person.factory( 57 first_name=first_name, 58 last_name=last_name, 59 sex=sex, 60 year_of_birth=year_of_birth, 61 ) 62 my_expectation = ( 63 f'{first_name}, {last_name},' 64 f' {sex}, {year_of_birth}' 65 ) 66 assert reality == my_expectation 67 68 reality = src.person.say_hello( 69 first_name=first_name, 70 last_name=last_name, 71 year_of_birth=year_of_birth, 72 ) 73 my_expectation = ( 74 f'Hello, my name is {first_name}' 75 f' {last_name} and I am' 76 f' {2026-year_of_birth}.' 77 ) 78 assert reality == my_expectation 79 80 jane = src.person.Person( 81 first_name=first_name, 82 last_name=last_name, 83 sex=sex, 84 year_of_birth=year_of_birth, 85 ) 86 87 reality = jane.say_hello() 88 assert reality == my_expectation 89 90 91def test_john():the terminal is my friend, and shows TypeError
TypeError: TestPerson.test_jane() 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_jane
46 reality = joe.say_hello()
47 assert reality == my_expectation
48 self.assertEqual(reality, my_expectation)
49
50 # def test_jane():
51 def test_jane(self):
the test passes.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_jane
50 # def test_jane(): 51 def test_jane(self): 52 first_name = 'jane' 53 last_name = 'doe' 54 sex = 'F' 55 year_of_birth = 1991 56 57 reality = src.person.factory( 58 first_name=first_name, 59 last_name=last_name, 60 sex=sex, 61 year_of_birth=year_of_birth, 62 ) 63 my_expectation = ( 64 f'{first_name}, {last_name},' 65 f' {sex}, {year_of_birth}' 66 ) 67 assert reality == my_expectation 68 self.assertNotEqual(reality, my_expectation) 69 70 reality = src.person.say_hello( 71 first_name=first_name, 72 last_name=last_name, 73 year_of_birth=year_of_birth, 74 ) 75 my_expectation = ( 76 f'Hello, my name is {first_name}' 77 f' {last_name} and I am' 78 f' {2026-year_of_birth}.' 79 ) 80 assert reality == my_expectation 81 self.assertNotEqual(reality, my_expectation) 82 83 jane = src.person.Person( 84 first_name=first_name, 85 last_name=last_name, 86 sex=sex, 87 year_of_birth=year_of_birth, 88 ) 89 90 reality = jane.say_hello() 91 assert reality == my_expectation 92 self.assertNotEqual(reality, my_expectation) 93 94 95def test_john():the terminal is my friend, and shows AssertionError
AssertionError: 'jane, doe, F, 1991' == 'jane, doe, F, 1991'I change assertNotEqual to assertEqual for the factory function, in test_jane
67 assert reality == my_expectation 68 # self.assertNotEqual(reality, my_expectation) 69 self.assertEqual(reality, my_expectation) 70 71 reality = src.person.say_hello( 72 first_name=first_name, 73 last_name=last_name, 74 year_of_birth=year_of_birth, 75 ) 76 my_expectation = ( 77 f'Hello, my name is {first_name}' 78 f' {last_name} and I am' 79 f' {2026-year_of_birth}.' 80 ) 81 assert reality == my_expectation 82 self.assertNotEqual(reality, my_expectation)the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is jane doe and I am 35.' == 'Hello, my name is jane doe and I am 35.'I change assertNotEqual to assertEqual for the say_hello function, in test_jane
81 assert reality == my_expectation 82 # self.assertNotEqual(reality, my_expectation) 83 self.assertEqual(reality, my_expectation) 84 85 jane = src.person.Person( 86 first_name=first_name, 87 last_name=last_name, 88 sex=sex, 89 year_of_birth=year_of_birth, 90 ) 91 92 reality = jane.say_hello()the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is jane doe and I am 35.' == 'Hello, my name is jane doe and I am 35.'I change assertNotEqual to assertEqual for the say hello method of the Person class, in test_jane
92 reality = jane.say_hello() 93 assert reality == my_expectation 94 # self.assertNotEqual(reality, my_expectation) 95 self.assertEqual(reality, my_expectation) 96 97 98def test_john():the test passes.
I remove the commented lines from test_jane
48 self.assertEqual(reality, my_expectation) 49 50 def test_jane(self): 51 first_name = 'jane' 52 last_name = 'doe' 53 sex = 'F' 54 year_of_birth = 1991 55 56 reality = src.person.factory( 57 first_name=first_name, 58 last_name=last_name, 59 sex=sex, 60 year_of_birth=year_of_birth, 61 ) 62 my_expectation = ( 63 f'{first_name}, {last_name},' 64 f' {sex}, {year_of_birth}' 65 ) 66 assert reality == my_expectation 67 self.assertEqual(reality, my_expectation) 68 69 reality = src.person.say_hello( 70 first_name=first_name, 71 last_name=last_name, 72 year_of_birth=year_of_birth, 73 ) 74 my_expectation = ( 75 f'Hello, my name is {first_name}' 76 f' {last_name} and I am' 77 f' {2026-year_of_birth}.' 78 ) 79 assert reality == my_expectation 80 self.assertEqual(reality, my_expectation) 81 82 jane = src.person.Person( 83 first_name=first_name, 84 last_name=last_name, 85 sex=sex, 86 year_of_birth=year_of_birth, 87 ) 88 89 reality = jane.say_hello() 90 assert reality == my_expectation 91 self.assertEqual(reality, my_expectation) 92 93 94def test_john():I add a git commit message in the other terminal
git commit -am 'move test_jane to TestPerson'
test_john with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_john to make it a method of the TestPerson class
89 reality = jane.say_hello() 90 assert reality == my_expectation 91 self.assertEqual(reality, my_expectation) 92 93 def test_john(): 94 first_name = 'john' 95 last_name = 'smith' 96 sex = 'M' 97 year_of_birth = 1580 98 99 reality = src.person.factory( 100 first_name=first_name, 101 last_name=last_name, 102 sex=sex, 103 year_of_birth=year_of_birth, 104 ) 105 my_expectation = ( 106 f'{first_name}, {last_name},' 107 f' {sex}, {year_of_birth}' 108 ) 109 assert reality == my_expectation 110 111 reality = src.person.say_hello( 112 first_name=first_name, 113 last_name=last_name, 114 year_of_birth=year_of_birth, 115 ) 116 my_expectation = ( 117 f'Hello, my name is {first_name}' 118 f' {last_name} and I am' 119 f' {2026-year_of_birth}.' 120 ) 121 assert reality == my_expectation 122 123 john = src.person.Person( 124 first_name=first_name, 125 last_name=last_name, 126 sex=sex, 127 year_of_birth=year_of_birth, 128 ) 129 130 reality = john.say_hello() 131 assert reality == my_expectation 132 133 134def test_mary():the terminal is my friend, and shows TypeError
TypeError: TestPerson.test_john() 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_john
46 reality = jane.say_hello()
47 assert reality == my_expectation
48 self.assertEqual(reality, my_expectation)
49
50 # def test_john():
51 def test_john(self):
the test passes.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_john
93 # def test_john(): 94 def test_john(self): 95 first_name = 'john' 96 last_name = 'smith' 97 sex = 'M' 98 year_of_birth = 1580 99 100 reality = src.person.factory( 101 first_name=first_name, 102 last_name=last_name, 103 sex=sex, 104 year_of_birth=year_of_birth, 105 ) 106 my_expectation = ( 107 f'{first_name}, {last_name},' 108 f' {sex}, {year_of_birth}' 109 ) 110 assert reality == my_expectation 111 self.assertNotEqual(reality, my_expectation) 112 113 reality = src.person.say_hello( 114 first_name=first_name, 115 last_name=last_name, 116 year_of_birth=year_of_birth, 117 ) 118 my_expectation = ( 119 f'Hello, my name is {first_name}' 120 f' {last_name} and I am' 121 f' {2026-year_of_birth}.' 122 ) 123 assert reality == my_expectation 124 self.assertNotEqual(reality, my_expectation) 125 126 john = src.person.Person( 127 first_name=first_name, 128 last_name=last_name, 129 sex=sex, 130 year_of_birth=year_of_birth, 131 ) 132 133 reality = john.say_hello() 134 assert reality == my_expectation 135 self.assertNotEqual(reality, my_expectation) 136 137 138def test_mary():the terminal is my friend, and shows AssertionError
AssertionError: 'john, smith, M, 1580' == 'john, smith, M, 1580'I change assertNotEqual to assertEqual for the factory function, in test_john
110 assert reality == my_expectation 111 # self.assertNotEqual(reality, my_expectation) 112 self.assertEqual(reality, my_expectation) 113 114 reality = src.person.say_hello( 115 first_name=first_name, 116 last_name=last_name, 117 year_of_birth=year_of_birth, 118 ) 119 my_expectation = ( 120 f'Hello, my name is {first_name}' 121 f' {last_name} and I am' 122 f' {2026-year_of_birth}.' 123 ) 124 assert reality == my_expectation 125 self.assertNotEqual(reality, my_expectation)the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is john smith and I am 446.' == 'Hello, my name is john smith and I am 446.'I change assertNotEqual to assertEqual for the say_hello function, in test_john
124 assert reality == my_expectation 125 # self.assertNotEqual(reality, my_expectation) 126 self.assertEqual(reality, my_expectation) 127 128 john = src.person.Person( 129 first_name=first_name, 130 last_name=last_name, 131 sex=sex, 132 year_of_birth=year_of_birth, 133 ) 134 135 reality = john.say_hello()the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is john smith and I am 446.' == 'Hello, my name is john smith and I am 446.'I change assertNotEqual to assertEqual for the say hello method of the Person class, in test_john
135 reality = john.say_hello() 136 assert reality == my_expectation 137 # self.assertNotEqual(reality, my_expectation) 138 self.assertEqual(reality, my_expectation) 139 140 141def test_mary():the test passes.
I remove the commented lines from test_john
91 self.assertEqual(reality, my_expectation) 92 93 def test_john(self): 94 first_name = 'john' 95 last_name = 'smith' 96 sex = 'M' 97 year_of_birth = 1580 98 99 reality = src.person.factory( 100 first_name=first_name, 101 last_name=last_name, 102 sex=sex, 103 year_of_birth=year_of_birth, 104 ) 105 my_expectation = ( 106 f'{first_name}, {last_name},' 107 f' {sex}, {year_of_birth}' 108 ) 109 assert reality == my_expectation 110 self.assertEqual(reality, my_expectation) 111 112 reality = src.person.say_hello( 113 first_name=first_name, 114 last_name=last_name, 115 year_of_birth=year_of_birth, 116 ) 117 my_expectation = ( 118 f'Hello, my name is {first_name}' 119 f' {last_name} and I am' 120 f' {2026-year_of_birth}.' 121 ) 122 assert reality == my_expectation 123 self.assertEqual(reality, my_expectation) 124 125 john = src.person.Person( 126 first_name=first_name, 127 last_name=last_name, 128 sex=sex, 129 year_of_birth=year_of_birth, 130 ) 131 132 reality = john.say_hello() 133 assert reality == my_expectation 134 self.assertEqual(reality, my_expectation) 135 136 137def test_mary():I add a git commit message in the other terminal
git commit -am 'move test_john to TestPerson'
test_mary with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_mary to make it a method of the TestPerson class
132 reality = john.say_hello() 133 assert reality == my_expectation 134 self.assertEqual(reality, my_expectation) 135 136 def test_mary(): 137 first_name = 'mary' 138 last_name = 'public' 139 sex = 'F' 140 year_of_birth = 2000 141 142 reality = src.person.factory( 143 first_name=first_name, 144 last_name=last_name, 145 sex=sex, 146 year_of_birth=year_of_birth, 147 ) 148 my_expectation = ( 149 f'{first_name}, {last_name},' 150 f' {sex}, {year_of_birth}' 151 ) 152 assert reality == my_expectation 153 154 reality = src.person.say_hello( 155 first_name=first_name, 156 last_name=last_name, 157 year_of_birth=year_of_birth, 158 ) 159 my_expectation = ( 160 f'Hello, my name is {first_name}' 161 f' {last_name} and I am' 162 f' {2026-year_of_birth}.' 163 ) 164 assert reality == my_expectation 165 166 mary = src.person.Person( 167 first_name=first_name, 168 last_name=last_name, 169 sex=sex, 170 year_of_birth=year_of_birth, 171 ) 172 173 reality = mary.say_hello() 174 assert reality == my_expectation 175 176 177def test_dir_person_class():the terminal is my friend, and shows TypeError
TypeError: TestPerson.test_mary() 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_mary
132 reality = john.say_hello()
133 assert reality == my_expectation
134 self.assertEqual(reality, my_expectation)
135
136 # def test_mary():
137 def test_mary(self):
the test passes.
REFACTOR: make it better
I add calls to the assertNotEqual method in test_mary
136 # def test_mary(): 137 def test_mary(self): 138 first_name = 'mary' 139 last_name = 'public' 140 sex = 'F' 141 year_of_birth = 2000 142 143 reality = src.person.factory( 144 first_name=first_name, 145 last_name=last_name, 146 sex=sex, 147 year_of_birth=year_of_birth, 148 ) 149 my_expectation = ( 150 f'{first_name}, {last_name},' 151 f' {sex}, {year_of_birth}' 152 ) 153 assert reality == my_expectation 154 self.assertNotEqual(reality, my_expectation) 155 156 reality = src.person.say_hello( 157 first_name=first_name, 158 last_name=last_name, 159 year_of_birth=year_of_birth, 160 ) 161 my_expectation = ( 162 f'Hello, my name is {first_name}' 163 f' {last_name} and I am' 164 f' {2026-year_of_birth}.' 165 ) 166 assert reality == my_expectation 167 self.assertNotEqual(reality, my_expectation) 168 169 mary = src.person.Person( 170 first_name=first_name, 171 last_name=last_name, 172 sex=sex, 173 year_of_birth=year_of_birth, 174 ) 175 176 reality = mary.say_hello() 177 assert reality == my_expectation 178 self.assertNotEqual(reality, my_expectation) 179 180 181def test_dir_person_class():the terminal is my friend, and shows AssertionError
AssertionError: 'mary, public, F, 2000' == 'mary, public, F, 2000'I change assertNotEqual to assertEqual for the factory function, in test_mary
153 assert reality == my_expectation 154 # self.assertNotEqual(reality, my_expectation) 155 self.assertEqual(reality, my_expectation) 156 157 reality = src.person.say_hello( 158 first_name=first_name, 159 last_name=last_name, 160 year_of_birth=year_of_birth, 161 ) 162 my_expectation = ( 163 f'Hello, my name is {first_name}' 164 f' {last_name} and I am' 165 f' {2026-year_of_birth}.' 166 ) 167 assert reality == my_expectation 168 self.assertNotEqual(reality, my_expectation)the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is mary public and I am 26.' == 'Hello, my name is mary public and I am 26.'I change assertNotEqual to assertEqual for the say_hello function, in test_mary
167 assert reality == my_expectation 168 # self.assertNotEqual(reality, my_expectation) 169 self.assertEqual(reality, my_expectation) 170 171 mary = src.person.Person( 172 first_name=first_name, 173 last_name=last_name, 174 sex=sex, 175 year_of_birth=year_of_birth, 176 ) 177 178 reality = mary.say_hello()the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is mary public and I am 26.' == 'Hello, my name is mary public and I am 26.'I change assertNotEqual to assertEqual for the say hello method of the Person class, in test_mary
178 reality = mary.say_hello() 179 assert reality == my_expectation 180 # self.assertNotEqual(reality, my_expectation) 181 self.assertEqual(reality, my_expectation) 182 183 184def test_dir_person_class():the test passes.
I remove the commented lines from test_mary
134 self.assertEqual(reality, my_expectation) 135 136 def test_mary(self): 137 first_name = 'mary' 138 last_name = 'public' 139 sex = 'F' 140 year_of_birth = 2000 141 142 reality = src.person.factory( 143 first_name=first_name, 144 last_name=last_name, 145 sex=sex, 146 year_of_birth=year_of_birth, 147 ) 148 my_expectation = ( 149 f'{first_name}, {last_name},' 150 f' {sex}, {year_of_birth}' 151 ) 152 assert reality == my_expectation 153 self.assertEqual(reality, my_expectation) 154 155 reality = src.person.say_hello( 156 first_name=first_name, 157 last_name=last_name, 158 year_of_birth=year_of_birth, 159 ) 160 my_expectation = ( 161 f'Hello, my name is {first_name}' 162 f' {last_name} and I am' 163 f' {2026-year_of_birth}.' 164 ) 165 assert reality == my_expectation 166 self.assertEqual(reality, my_expectation) 167 168 mary = src.person.Person( 169 first_name=first_name, 170 last_name=last_name, 171 sex=sex, 172 year_of_birth=year_of_birth, 173 ) 174 175 reality = mary.say_hello() 176 assert reality == my_expectation 177 self.assertEqual(reality, my_expectation) 178 179 180def test_dir_person_class():I add a git commit message in the other terminal
git commit -am 'move test_mary to TestPerson'
test_dir_person_class with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_dir_person_class to make it a method of the TestPerson class
175 reality = mary.say_hello() 176 assert reality == my_expectation 177 self.assertEqual(reality, my_expectation) 178 179 def test_dir_person_class(): 180 reality = dir(src.person.Person) 181 my_expectation = [212 ] 213 assert reality == my_expectation 214 215 216def test_dir_person_instance():the terminal is my friend, and shows TypeError
TypeError: TestPerson.test_dir_person_class() 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_dir_person_class
175 reality = mary.say_hello()
176 assert reality == my_expectation
177 self.assertEqual(reality, my_expectation)
178
179 # def test_dir_person_class():
180 def test_dir_person_class(self):
the test passes.
REFACTOR: make it better
I add a call to the assertNotEqual method in test_dir_person_class
214 assert reality == my_expectation 215 self.assertNotEqual(reality, my_expectation) 216 217 218def test_dir_person_instance():the terminal is my friend, and shows AssertionError.
I change assertNotEqual to assertEqual for the factory function, in test_dir_person_class
214 assert reality == my_expectation 215 # self.assertNotEqual(reality, my_expectation) 216 self.assertEqual(reality, my_expectation) 217 218 219def test_dir_person_instance():the test passes.
I remove the commented lines from test_dir_person_class
175 reality = mary.say_hello() 176 assert reality == my_expectation 177 self.assertEqual(reality, my_expectation) 178 179 def test_dir_person_class(self): 180 reality = dir(src.person.Person) 181 my_expectation = [213 ] 214 assert reality == my_expectation 215 self.assertEqual(reality, my_expectation) 216 217 218def test_dir_person_instance():I add a git commit message in the other terminal
git commit -am 'move test_dir_person_class to TestPerson'
test_dir_person_instance with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_dir_person_instance to make it a method of the TestPerson class
213 assert reality == my_expectation 214 self.assertEqual(reality, my_expectation) 215 216 def test_dir_person_instance(): 217 an_instance_of_person = src.person.Person( 218 first_name='first_name', 219 last_name='last_name', 220 sex='M', 221 year_of_birth=2026, 222 ) 223 224 reality = dir(an_instance_of_person) 225 my_expectation = [260 ] 261 assert reality == my_expectation 262 263 264# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: TestPerson.test_dir_person_instance() takes 0 positional arguments but 1 was givenbecause …
GREEN: make it pass
I add self to the parentheses of test_dir_person_instance
213 assert reality == my_expectation
214 self.assertEqual(reality, my_expectation)
215
216 # def test_dir_person_instance():
217 def test_dir_person_instance(self):
the test passes.
REFACTOR: make it better
I add a call to the assertNotEqual method in test_dir_person_instance
262 assert reality == my_expectation 263 self.assertNotEqual(reality, my_expectation) 264 265 266# Exceptions seenthe terminal is my friend, and shows AssertionError.
I change assertNotEqual to assertEqual for the factory function, in test_dir_person_instance
262 assert reality == my_expectation 263 # self.assertNotEqual(reality, my_expectation) 264 self.assertEqual(reality, my_expectation) 265 266 267# Exceptions seenthe test passes.
I remove the commented lines from test_dir_person_instance
214 self.assertEqual(reality, my_expectation) 215 216 def test_dir_person_instance(self): 217 an_instance_of_person = src.person.Person( 218 first_name='first_name', 219 last_name='last_name', 220 sex='M', 221 year_of_birth=2026, 222 ) 223 224 reality = dir(an_instance_of_person) 225 my_expectation = [260 ] 261 assert reality == my_expectation 262 self.assertEqual(reality, my_expectation) 263 264 265# Exceptions seen 266# AssertionError 267# NameError 268# TypeError 269# AttributeError 270# SyntaxErrorI add a git commit message in the other terminal
git commit -am 'move test_dir_person_instance to TestPerson'
close the project
I close
test_person.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
personcd ..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
My tests for a person still have the problem where they are the same three tests. There has to be a way that I can use one test for all the people.
code from the chapter
what is next?
You know
Would you like to know where the extra attributes and methods of the Person class came from?
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.