how to make a person with loops
I have a problem in person/tests/test_person.py
test_joe, test_jane, test_john and test_mary have the same assertions.
test_when_year_of_birth_is_not_an_integer also has assertions that are basically the same, the only thing that changes are the values for the
year_of_birthparameter.
This means that if I add more people or more cases where year_of_birth is not an integer I would have to add more tests.
I want to use one test for each of the three tests for each person, and one test for every case where year_of_birth is not an integer. I can do this with for loops.
A for loop is a way to repeat the same command over an iterable (a collection of items), it is written like this
for item in collection:
do something
preview
I have these tests by the end of the chapter
1import datetime
2import src.person
3import unittest
4
5
6class TestPerson(unittest.TestCase):
7
8 this_year = datetime.date.today().year
9 younger_than_18 = this_year - 17
10 not_younger_than_18 = this_year - 18
11
12 first_name = 'first_name'
13 last_name = 'last_name'
14 sex = 'M'
16 people = (
17 (
18 # is_citizen, passed_test
19 not_younger_than_18, True, True,
20 True, True,
21 ),
22 (
23 # is_citizen, not passed_test
24 not_younger_than_18, True, False,
25 True, False,
26 ),
27 (
28 # not is_citizen, passed_test
29 not_younger_than_18, False, True,
30 False, True,
31 ),
32 (
33 # not is_citizen, not passed_test
34 not_younger_than_18, False, False,
35 False, False,
36 ),
37 (
38 # underage, is_citizen, passed_test
39 younger_than_18, True, True,
40 False, False,
41 ),
42 (
43 # underage, not is_citizen, passed_test
44 younger_than_18, False, True,
45 False, False,
46 ),
47 (
48 # underage, is_citizen, not passed_test
49 younger_than_18, True, False,
50 False, False,
51 ),
52 (
53 # underage, not is_citizen, not passed_test
54 younger_than_18, False, False,
55 False, False,
56 ),
57 # year_of_birth is not an integer
58 (None, False, False,),
59 (2026.0, False, False,),
60 ('2026', False, False,),
61 ((2026,), False, False,),
62 )
63
64 def calculate_age(self, year_of_birth):
65 return self.this_year - year_of_birth
67 def test_person_object(self):
68 for a_person in self.people:
69 year_of_birth = a_person[0]
70
71 with self.subTest(a_person):
72 try:
73 an_instance = src.person.Person(
74 first_name=self.first_name,
75 last_name=self.last_name,
76 sex=self.sex,
77 year_of_birth=year_of_birth,
78 is_citizen=a_person[1],
79 passed_test=a_person[2],
80 )
81 except TypeError as error:
82 self.assertEqual(
83 error.args[0],
84 f"'{year_of_birth}' is not an integer"
85 )
86 else:
87 self.assertEqual(
88 an_instance.say_hello(),
89 (
90 f'Hello, my name is {self.first_name}'
91 f' {self.last_name} and I am'
92 f' {self.calculate_age(year_of_birth)}.'
93 )
94 )
95 self.assertEqual(an_instance.can_vote(), a_person[3])
96 self.assertEqual(
97 an_instance.can_get_license(), a_person[4]
98 )
99
100 def test_say_hello_function(self):
101 for a_person in self.people:
102 year_of_birth = a_person[0]
103
104 with self.subTest(a_person):
105 try:
106 reality = src.person.say_hello(
107 first_name=self.first_name,
108 last_name=self.last_name,
109 year_of_birth=year_of_birth,
110 )
111 except TypeError as error:
112 self.assertEqual(
113 error.args[0],
114 f"'{year_of_birth}' is not an integer"
115 )
116 else:
117 self.assertEqual(
118 reality,
119 (
120 f'Hello, my name is {self.first_name}'
121 f' {self.last_name} and I am'
122 f' {self.calculate_age(year_of_birth)}.'
123 )
124 )
126 def test_person_factory(self):
127 for a_person in self.people:
128 year_of_birth = a_person[0]
129
130 with self.subTest(a_person):
131 self.assertEqual(
132 src.person.person(
133 first_name=self.first_name,
134 last_name=self.last_name,
135 sex=self.sex,
136 year_of_birth=year_of_birth,
137 ),
138 (
139 f'{self.first_name}, {self.last_name},'
140 f' {self.sex}, {year_of_birth}'
141 )
142 )
143
144 def test_if_person_is_not_alive(self):
145 for year_of_birth in (
146 self.this_year+1,
147 self.this_year-121,
148 ):
149 age = self.calculate_age(year_of_birth)
150
151 with (
152 self.subTest(year_of_birth),
153 self.assertRaisesRegex(
154 ValueError,
155 f'a {age} year old person is not alive.'
156 ),
157 ):
158 src.person.Person(
159 first_name=self.first_name,
160 last_name=self.last_name,
161 sex=self.sex,
162 year_of_birth=year_of_birth,
163 )
165 def test_dir_person_object(self):
166 self.assertEqual(
167 dir(src.person.Person),
168 [
169 '__class__', '__delattr__', '__dict__',
170 '__dir__', '__doc__', '__eq__',
171 '__firstlineno__', '__format__', '__ge__',
172 '__getattribute__', '__getstate__', '__gt__',
173 '__hash__', '__init__', '__init_subclass__',
174 '__le__', '__lt__', '__module__', '__ne__',
175 '__new__', '__reduce__', '__reduce_ex__',
176 '__repr__', '__setattr__', '__sizeof__',
177 '__static_attributes__', '__str__',
178 '__subclasshook__', '__weakref__',
179 'can_get_license', 'can_vote', 'check_age',
180 'say_hello',
181 ]
182 )
184 def test_dir_person_instance(self):
185 self.assertEqual(
186 dir(
187 src.person.Person(
188 first_name='first_name',
189 last_name='last_name',
190 sex='M',
191 year_of_birth=2026,
192 )
193 ),
194 [
195 '__class__', '__delattr__', '__dict__',
196 '__dir__', '__doc__', '__eq__',
197 '__firstlineno__', '__format__', '__ge__',
198 '__getattribute__', '__getstate__', '__gt__',
199 '__hash__', '__init__', '__init_subclass__',
200 '__le__', '__lt__', '__module__', '__ne__',
201 '__new__', '__reduce__', '__reduce_ex__',
202 '__repr__', '__setattr__', '__sizeof__',
203 '__static_attributes__', '__str__',
204 '__subclasshook__', '__weakref__',
205 'age', 'can_get_license', 'can_vote',
206 'check_age', 'first_name', 'is_citizen',
207 'last_name', 'passed_test', 'say_hello',
208 'sex', 'year_of_birth',
209 ]
210 )
open the project
I open a terminal
I change directory to the project
I open
test_person.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
extract test_person_factory
The tests for the person function in test_joe, test_jane, test_john and test_mary make a call to the person function then compare the result with a string.
self.assertEqual(
src.person.person(
first_name=first_name,
last_name=last_name,
sex=sex,
year_of_birth=year_of_birth,
),
(
f'{first_name}, {last_name},'
f' {sex}, {year_of_birth}'
)
)
I can make one function with a for loop to remove the repetition.
I add a test for the person function to
tests/test_person.py47 def assert_person_can_say_hello( 48 self,first_name, last_name, 49 sex, year_of_birth, 50 ): 51 self.assertEqual( 52 src.person.Person( 53 first_name=first_name, 54 last_name=last_name, 55 sex=sex, 56 year_of_birth=year_of_birth, 57 ).say_hello(), 58 ( 59 f'Hello, my name is {first_name}' 60 f' {last_name} and I am' 61 f' {self.calculate_age(year_of_birth)}.' 62 ) 63 )65 def test_person_factory(self): 66 people = ( 67 ('joe', 'blow', 'M', 1996), 68 ) 69 for a_person in people: 70 self.assert_person_factory_works( 71 first_name=a_person[0], 72 last_name=a_person[1], 73 sex=a_person[2], 74 year_of_birth=a_person[3], 75 ) 76 77 def test_joe(self):the test is still green.
I made a tuple named
peoplethat contains a tuple forjoepeople = ( ('joe', 'blow', 'M', 1996), )I use a for loop to repeat the same commands for each item in the
peopletuple, in this case there is only one item - the tuple forjoefor a_person in ( ('joe', 'blow', 'M', 1996), ):I use the index of each item in
joefor the parameters when the test calls the person functionfor a_person in people: ├── a_person = ('joe', 'blow', 'M', 1996) └── self.assert_person_factory_works( first_name=a_person[0], # 'joe' last_name=a_person[1], # 'blow' sex=a_person[2], # 'M' year_of_birth=a_person[3], # 1996 ) └── def assert_person_factory_works( self, first_name, last_name, sex, year_of_birth ): ├── first_name = 'joe' # a_person[0] ├── last_name = 'blow' # a_person[1] ├── sex = 'M' # a_person[2] ├── year_of_birth = 1996 # a_person[3] └── self.assertEqual( ┌────── src.person.person( │ first_name=first_name, # 'joe' │ last_name=last_name, # 'blow' │ sex=sex, # 'M' │ year_of_birth=year_of_birth, # 1996 │ ), │ ( │ f'{first_name}, {last_name},' │ f' {sex}, {year_of_birth}' │ ) │ ) └────── src/person/__init__.py └── def person( first_name, last_name, sex, year_of_birth, ): ├── first_name = 'joe' # a_person[0] ├── last_name = 'blow' # a_person[1] ├── sex = 'M' # a_person[2] ├── year_of_birth = 1996 # a_person[3] └── return ( f'{first_name}, {last_name},' f' {sex}, {year_of_birth}' ) return 'joe, blow, M, 1996'
I add
janeto the tuple ofpeople65 def test_person_factory(self): 66 people = ( 67 ('jane', 'doe', 'F', 1991), 68 ('joe', 'blow', 'M', 1996), 69 ) 70 for a_person in people:still green, because the result when the person function is called with
'jane','doe','F'and'1991'is'jane, doe, F, 1991'.for a_person in ( ┌───┴── ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ): ├────── a_person = ('jane', 'doe', 'F', 1991) └────── self.assert_person_factory_works( first_name=a_person[0], # 'jane' last_name=a_person[1], # 'doe' sex=a_person[2], # 'F' year_of_birth=a_person[3], # 1991 ) └── def assert_person_factory_works( self, first_name, last_name, sex, year_of_birth ): └── self.assertEqual( ┌────── src.person.person( │ first_name=first_name, # 'jane' │ last_name=last_name, # 'doe' │ sex=sex, # 'F' │ year_of_birth=year_of_birth, # 1991 │ ), │ ( │ f'{first_name}, {last_name},' │ f' {sex}, {year_of_birth}' │ ) │ ) └────── src/person/__init__.py └── def person( first_name, last_name, sex, year_of_birth, ): ├── first_name = 'jane' # a_person[0] ├── last_name = 'doe' # a_person[1] ├── sex = 'F' # a_person[2] ├── year_of_birth = 1991 # a_person[3] └── return ( f'{first_name}, {last_name},' f' {sex}, {year_of_birth}' ) return 'jane, doe, M, 1991'I add a tuple for
mary65 def test_person_factory(self): 66 people = ( 67 ('jane', 'doe', 'F', 1991), 68 ('joe', 'blow', 'M', 1996), 69 ('mary', 'public', 'F', 2000), 70 ) 71 for a_person in people:still green.
for a_person in ( │ ('joe', 'blow', 'M', 1996), │ ('jane', 'doe', 'F', 1991), ┌───┴── ('mary', 'public', 'F', 2000), │ ): ├────── a_person = ('mary', 'public', 'F', 2000) └────── self.assert_person_factory_works( first_name=a_person[0], # 'mary' last_name=a_person[1], # 'public' sex=a_person[2], # 'F' year_of_birth=a_person[3], # 2000 ) └── def assert_person_factory_works( self, first_name, last_name, sex, year_of_birth ): └── self.assertEqual( ┌────── src.person.person( │ first_name=first_name, # 'mary' │ last_name=last_name, # 'public' │ sex=sex, # 'F' │ year_of_birth=year_of_birth, # 2000 │ ), │ ( │ f'{first_name}, {last_name},' │ f' {sex}, {year_of_birth}' │ ) │ ) └────── src/person/__init__.py └── def person( first_name, last_name, sex, year_of_birth, ): ├── first_name = 'mary' # a_person[0] ├── last_name = 'public' # a_person[1] ├── sex = 'F' # a_person[2] ├── year_of_birth = 2000 # a_person[3] └── return ( f'{first_name}, {last_name},' f' {sex}, {year_of_birth}' ) return 'mary, public, M, 2000'I add a tuple for
john65 def test_person_factory(self): 66 people = ( 67 ('jane', 'doe', 'F', 1991), 68 ('joe', 'blow', 'M', 1996), 69 ('mary', 'public', 'F', 2000), 70 ('john', 'smith', 'M', 1980), 71 ) 72 for a_person in people:green, showing that for each tuple in the
peopletuple, the assertion is True.for a_person in ( │ ('joe', 'blow', 'M', 1996), │ ('jane', 'doe', 'F', 1991), │ ('mary', 'public', 'F', 2000), ┌───┴── ('john', 'smith', 'M', 1980), │ ): ├────── a_person = ('john', 'smith', 'M', 1980) └────── self.assert_person_factory_works( first_name=a_person[0], # 'john' last_name=a_person[1], # 'smith' sex=a_person[2], # 'M' year_of_birth=a_person[3], # 1980 ) └── def assert_person_factory_works( self, first_name, last_name, sex, year_of_birth ): └── self.assertEqual( ┌────── src.person.person( │ first_name=first_name, # 'john' │ last_name=last_name, # 'smith' │ sex=sex, # 'M' │ year_of_birth=year_of_birth, # 1980 │ ), │ ( │ f'{first_name}, {last_name},' │ f' {sex}, {year_of_birth}' │ ) │ ) └────── src/person/__init__.py └── def person( first_name, last_name, sex, year_of_birth, ): ├── first_name = 'john' # a_person[0] ├── last_name = 'smith' # a_person[1] ├── sex = 'M' # a_person[2] ├── year_of_birth = 1980 # a_person[3] └── return ( f'{first_name}, {last_name},' f' {sex}, {year_of_birth}' ) return 'john, smith, M, 1980'I add a tuple for a person with a string as the
year_of_birth15 def test_person_factory(self): 16 people = ( 17 ('jane', 'doe', 'F', 1991), 18 ('joe', 'blow', 'M', 1996), 19 ('mary', 'public', 'F', 2000), 20 ('john', 'smith', 'M', 1980), 21 ('first_name', 'last_name', 'F', 'a string'), 22 ) 23 for a_person in people:the test is still green, because the person function returns a string with the inputs it gets.
I remove the call to the assert_person_factory_works method from test_joe since it is now a repetition
I remove the call to the assert_person_factory_works method from test_jane since it is now a repetition
I remove the call to the assert_person_factory_works method from test_john since it is now a repetition
I remove the call to the assert_person_factory_works method from test_mary since it is now a repetition
I no longer need the assert_person_factory_works method because it is only used by test_person_factory
65 def test_person_factory(self): 66 people = ( 67 ('jane', 'doe', 'F', 1991), 68 ('joe', 'blow', 'M', 1996), 69 ('mary', 'public', 'F', 2000), 70 ('john', 'smith', 'M', 1980), 71 ('first_name', 'last_name', 'F', 'a_string') 72 ) 73 for a_person in people: 74 self.assert_person_factory_works( 75 first_name=a_person[0], 76 last_name=a_person[1], 77 sex=a_person[2], 78 year_of_birth=a_person[3], 79 )81 first_name = a_person[0] 82 last_name = a_person[1] 83 sex = a_person[2] 84 year_of_birth = a_person[3] 85 86 self.assertEqual( 87 src.person.person( 88 first_name=first_name, 89 last_name=last_name, 90 sex=sex, 91 year_of_birth=year_of_birth, 92 ), 93 None 94 ) 95 96 def test_joe(self):the terminal is my friend, and shows AssertionError
AssertionError: 'jane, doe, F, 1991' != NoneI change the expectation of the assertion to
'jane, doe, F, 1991'86 self.assertEqual( 87 src.person.person( 88 first_name=first_name, 89 last_name=last_name, 90 sex=sex, 91 year_of_birth=year_of_birth, 92 ), 93 'jane, doe, F, 1991' 94 ) 95 96 def test_joe(self):the terminal shows AssertionError
AssertionError: 'joe, blow, M, 1996' != 'jane, doe, F, 1991'because the for loop moved to the next item in the
peopletuple ('joe')I change the expectation to the f-string from the assert_person_factory_works method
86 self.assertEqual( 87 src.person.person( 88 first_name=first_name, 89 last_name=last_name, 90 sex=sex, 91 year_of_birth=year_of_birth, 92 ), 93 ( 94 f'{first_name}, {last_name},' 95 f' {sex}, {year_of_birth}' 96 ) 97 ) 98 99 def test_joe(self):the test passes.
I remove the call to the assert_person_factory_works method from test_person_factory
65 def test_person_factory(self): 66 people = ( 67 ('jane', 'doe', 'F', 1991), 68 ('joe', 'blow', 'M', 1996), 69 ('mary', 'public', 'F', 2000), 70 ('john', 'smith', 'M', 1980), 71 ('first_name', 'last_name', 'F', 'a_string') 72 ) 73 for a_person in people: 74 first_name = a_person[0] 75 last_name = a_person[1] 76 sex = a_person[2] 77 year_of_birth = a_person[3]79 self.assertEqual( 80 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 f'{first_name}, {last_name},' 88 f' {sex}, {year_of_birth}' 89 ) 90 ) 91 92 def test_joe(self):the tests are still green.
I remove the assert_person_factory_works method from the TestPerson object
I add a git commit message
The for loop allows me to test any number of people with the same test. I no longer have to write one test for each person.
extract test_say_hello_function
The tests for the say_hello function in test_joe, test_jane, test_john and test_mary make a call to the say_hello function then compare the result with a string.
self.assertEqual(
src.person.say_hello(
first_name=first_name,
last_name=last_name,
year_of_birth=year_of_birth,
),
(
f'Hello, my name is {first_name}'
f' {last_name} and I am'
f' {self.calculate_age(year_of_birth)}.'
)
)
I can make one function with a for loop to remove the repetition.
RED: make it fail
I add a test for the say_hello function
30 def assert_person_can_say_hello(
31 self,first_name, last_name,
32 sex, year_of_birth,
33 ):
34 self.assertEqual(
35 src.person.Person(
36 first_name=first_name,
37 last_name=last_name,
38 sex=sex,
39 year_of_birth=year_of_birth,
40 ).say_hello(),
41 (
42 f'Hello, my name is {first_name}'
43 f' {last_name} and I am'
44 f' {self.calculate_age(year_of_birth)}.'
45 )
46 )
48 def test_say_hello_function(self):
49 people = (
50 ('jane', 'doe', 'F', 1991),
51 ('joe', 'blow', 'M', 1996),
52 ('mary', 'public', 'F', 2000),
53 ('john', 'smith', 'M', 1980),
54 ('first_name', 'last_name', 'F', 'a_string')
55 )
56 for a_person in people:
57 first_name = a_person[0]
58 last_name = a_person[1]
59 year_of_birth = a_person[3]
60
61 self.assert_say_hello_works(
62 first_name=first_name,
63 last_name=last_name,
64 year_of_birth=year_of_birth,
65 )
66
67 def test_person_factory(self):
the terminal is my friend, and shows TypeError
E TypeError
because
the the say_hello function calls the calculate_age function
the calculate_age function raises TypeError when
year_of_birthis not an integer.year_of_birthis a string in the last tuple in thepeopletuple (('first_name', 'last_name', 'F', 'a_string'))for person in ( │ ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), │ ('john', 'smith', 'M', 1980), ┌───┴── ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── a_person = ('first_name', 'last_name', 'F', 'a string') ├────── first_name = a_person[0] # 'first_name' ├────── last_name = a_person[1] # 'last_name' ├────── year_of_birth = a_person[3] # 'a string' └────── self.assert_say_hello_works( first_name=first_name, last_name=last_name, year_of_birth=year_of_birth, ) └── def assert_person_factory_works( self, first_name, last_name, sex, year_of_birth ): └── self.assertEqual( ┌────── src.person.say_hello( │ first_name=first_name, │ last_name=last_name, │ year_of_birth=year_of_birth, │ ), │ ( │ f'Hello, my name is {first_name}' │ f' {last_name} and I am' │ f' {self.calculate_age(year_of_birth)}.' │ ) │ ) └────── src/person/__init__.py └── def say_hello( first_name, last_name, year_of_birth ): ├── first_name = 'first_name' # a_person[0] ├── last_name = 'last_name' # a_person[1] ├── year_of_birth = 'a string' # a_person[3] └── return ( f'Hello, my name is {first_name}' f' {last_name} and I am' f' {calculate_age(year_of_birth)}.' ) │ └── def calculate_age(year_of_birth): ├── year_of_birth = 'a string' └── if not isinstance( year_of_birth, int ): └── raise TypeError ...
GREEN: make it pass
I add a message to the calculate_age function for when TypeError is raised, in
src/person/__init__.py37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError( 40 f"'{year_of_birth}' is not an integer" 41 )the terminal is my friend, and shows TypeError
TypeError: 'a string' is not an integerbetter.
I change the statements in the assert_say_hello_works method to a try statement with an else clause in
tests/test_person.py13 def assert_say_hello_works( 14 self, first_name, last_name, 15 year_of_birth, 16 ): 17 try: 18 reality = src.person.say_hello( 19 first_name=first_name, 20 last_name=last_name, 21 year_of_birth=year_of_birth, 22 ) 23 except TypeError: 24 pass 25 else: 26 self.assertEqual( 27 reality, 28 # src.person.say_hello( 29 # first_name=first_name, 30 # last_name=last_name, 31 # year_of_birth=year_of_birth, 32 # ), 33 ( 34 f'Hello, my name is {first_name}' 35 f' {last_name} and I am' 36 f' {self.calculate_age(year_of_birth)}.' 37 ) 38 ) 39 40 def assert_person_can_say_hello( 41 self,first_name, last_name, 42 sex, year_of_birth, 43 ):the test passes.
REFACTOR: make it better
I want to test the error message to make sure the assert_say_hello_works method only catches TypeError with this specific message. I can use the Exception in the except block as an object to do this
17 try: 18 reality = src.person.say_hello( 19 first_name=first_name, 20 last_name=last_name, 21 year_of_birth=year_of_birth, 22 ) 23 except TypeError as error: 24 pass 25 else:the test is still green.
I add assertEqual to the except block with the dir built-in function to see the attributes and methods of the Exception object in the except block
23 except TypeError as error: 24 self.assertEqual(dir(error), []) 25 else:the terminal is my friend, and shows AssertionError with the list of attributes and methods. Three of the names stand out because they do not have double underscores (
__) before and after -add_note,argsandwith_traceback.I change the assertion to see what
add_noteis23 except TypeError as error: 24 # self.assertEqual(dir(error), []) 25 self.assertEqual(error.add_note, None) 26 else:the terminal is my friend, and shows AssertionError
AssertionError: <built-in method add_note of TypeError object at 0xffff7ed65c43> != Nonebecause
add_noteis a method.-
23 except TypeError as error: 24 # self.assertEqual(dir(error), []) 25 self.assertEqual(error.add_note(), None) 26 else:the terminal is my friend, and shows TypeError
TypeError: BaseException.add_note() takes exactly one argument (0 given)this is not what I want, on to the next one.
I change the assertion to see what
argsis23 except TypeError as error: 24 # self.assertEqual(dir(error), []) 25 # self.assertEqual(error.add_note(), None) 26 self.assertEqual(error.args, None) 27 else:the terminal is my friend, and shows AssertionError
AssertionError: ("'a string' is not an integer",) != Nonefantastic!
argsis a tuple that has the error message as its first and only item.I use the index of the error message with an f-string
23 except TypeError as error: 24 # self.assertEqual(dir(error), []) 25 # self.assertEqual(error.add_note(), None) 26 self.assertEqual( 27 error.args[0], 28 f"'{year_of_birth}' is not an integer" 29 ) 30 else:the test passes.
I change the message for TypeError in the calculate_age function in
src/person/__init__.pyto test my change37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError('BOOM!!!') 40 raise TypeError( 41 f"'{year_of_birth}' is not an integer" 42 )the terminal is my friend, and shows AssertionError
AssertionError: 'BOOM!!!' != "'a string' is not an integer"because the error message is not correct
for a_person in ( │ ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), │ ('john', 'smith', 'M', 1980), ┌───┴── ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── first_name = a_person[0] # 'first_name' ├────── last_name = a_person[1] # 'last_name' ├────── year_of_birth = a_person[3] # 'a string' └────── self.assert_say_hello_works( first_name=first_name, last_name=last_name, year_of_birth=year_of_birth, ) └── def assert_say_hello_works( self, first_name, last_name, year_of_birth, ): └── try: reality = src.person.say_hello( first_name=first_name, last_name=last_name, year_of_birth=year_of_birth, ) └── src/person/__init__.py └── def say_hello( first_name, last_name, year_of_birth, ): └── return ( ├── f'Hello, my name is {first_name}' ├── f' {last_name} and I am' └── f' {calculate_age(year_of_birth)}.' ) │ └── def calculate_age(year_of_birth): ├── year_of_birth = 'a string' └── if not isinstance( year_of_birth, int ): ┌───────────────────────────────┴── raise TypeError('BOOM!!!') └── except TypeError as error: └── self.assertEqual( error.args[0], f"'{year_of_birth}' is not an integer" ) └── raise AssertionError else: ...I undo the change
37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError( 40 f"'{year_of_birth}' is not an integer" 41 ) 42 43 age = ( 44 datetime.date.today().year 45 - year_of_birth 46 ) 47 48 if age < 0 or age > 120: 49 raise ValueError 50 return agethe test is green again.
I remove the call to the assert_say_hello_works method from test_joe since it is now a repetition
I remove the call to the assert_say_hello_works method from test_jane since it is now a repetition
I remove the call to the assert_say_hello_works method from test_john since it is now a repetition
I remove the call to the assert_say_hello_works method from test_mary since it is now a repetition
I no longer need the assert_say_hello_works method because it is only used by test_say_hello_function
63 def test_say_hello_function(self): 64 people = ( 65 ('jane', 'doe', 'F', 1991), 66 ('joe', 'blow', 'M', 1996), 67 ('mary', 'public', 'F', 2000), 68 ('john', 'smith', 'M', 1980), 69 ('first_name', 'last_name', 'F', 'a_string') 70 ) 71 for a_person in people: 72 first_name = a_person[0] 73 last_name = a_person[1] 74 year_of_birth = a_person[3] 75 76 self.assert_say_hello_works( 77 first_name=first_name, 78 last_name=last_name, 79 year_of_birth=year_of_birth, 80 )82 try: 83 reality = src.person.say_hello( 84 first_name=first_name, 85 last_name=last_name, 86 year_of_birth=year_of_birth, 87 ) 88 except TypeError as error: 89 self.assertEqual( 90 error.args[0], 91 f"'{year_of_birth}' is not an integer" 92 ) 93 else: 94 self.assertEqual( 95 reality, 96 None 97 ) 98 99 def test_person_factory(self):the terminal is my friend, and shows AssertionError
AssertionError: 'Hello, my name is jane doe and I am YY.' != NoneI change the expectation to
'Hello, my name is jane doe and I am YY.'93 else: 94 self.assertEqual( 95 reality, 96 'Hello, my name is jane doe and I am YY.' 97 ) 98 99 def test_person_factory(self):the terminal shows AssertionError
AssertionError: 'Hello, my name is joe blow and I am XX.' != 'Hello, my name is jane doe and I am YY.'because the for loop moved to the next item in the
peopletuple ('joe')I change the expectation to the f-string from the assert_say_hello_works method
93 else: 94 self.assertEqual( 95 reality, 96 ( 97 f'Hello, my name is {first_name}' 98 f' {last_name} and I am' 99 f' {self.calculate_age(year_of_birth)}.' 100 ) 101 ) 102 103 def test_person_factory(self):the test passes.
I remove the call to the assert_say_hello_works method from test_say_hello_function
63 def test_say_hello_function(self): 64 people = ( 65 ('jane', 'doe', 'F', 1991), 66 ('joe', 'blow', 'M', 1996), 67 ('mary', 'public', 'F', 2000), 68 ('john', 'smith', 'M', 1980), 69 ('first_name', 'last_name', 'F', 'a_string') 70 ) 71 for a_person in people: 72 first_name = a_person[0] 73 last_name = a_person[1] 74 year_of_birth = a_person[3]76 try: 77 reality = src.person.say_hello( 78 first_name=first_name, 79 last_name=last_name, 80 year_of_birth=year_of_birth, 81 ) 82 except TypeError as error: 83 self.assertEqual( 84 error.args[0], 85 f"'{year_of_birth}' is not an integer" 86 ) 87 else: 88 self.assertEqual( 89 reality, 90 ( 91 f'Hello, my name is {first_name}' 92 f' {last_name} and I am' 93 f' {self.calculate_age(year_of_birth)}.' 94 ) 95 ) 96 97 def test_person_factory(self):I remove the assert_say_hello_works method from the TestPerson object
I add a git commit message
For each person in the people tuple, this test calls the say_hello function
If the call raises TypeError, it asserts that the error message is correct
If the error message is not correct it raises AssertionError
If the error message is correct the test passes
for a_person in ( │ ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), │ ('john', 'smith', 'M', 1980), ┌───┴── ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── first_name = a_person[0] # 'first_name' ├────── last_name = a_person[1] # 'last_name' ├────── year_of_birth = a_person[3] # 'a string' └────── try: reality = src.person.say_hello( first_name=first_name, last_name=last_name, year_of_birth=year_of_birth, ) └── src/person/__init__.py └── def say_hello( first_name, last_name, year_of_birth, ): ├── first_name = 'first_name' # a_person[0] ├── last_name = 'last_name' # a_person[1] ├── year_of_birth = 'a string' # a_person[3] └── return ( ├── f'Hello, my name is {first_name}' ├── f' {last_name} and I am' └── f' {calculate_age(year_of_birth)}.' ) │ └── def calculate_age(year_of_birth): ├── year_of_birth = 'a string' └── if not isinstance( year_of_birth, int ): ┌─────────────────────────────────┴── raise TypeError( │ f"'{year_of_birth}'" │ " is not an integer" │ ) └── except TypeError as error: └── self.assertEqual( error.args[0], f"'{year_of_birth}' is not an integer" ) else: ...
If the call to the calculate_age function from the say_hello function does not raise TypeError, it asserts that the result of the call matches the expectation
for person in ( │ ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), ┌───┴── ('john', 'smith', 'M', 1980), │ ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── first_name = a_person[0] # 'john' ├────── last_name = a_person[1] # 'smith' ├────── year_of_birth = a_person[3] # 1980 └───┬── try: │ reality = src.person.say_hello( │ first_name=first_name, │ last_name=last_name, │ year_of_birth=year_of_birth, │ ) │ └── src/person/__init__.py │ └── def say_hello( │ first_name, last_name, year_of_birth, │ ): │ ├── first_name = 'john' # a_person[0] │ ├── last_name = 'smith' # a_person[1] │ ├── year_of_birth = 1980 # a_person[3] │ └── return ( │ ├── f'Hello, my name is {first_name}' │ ├── f' {last_name} and I am' │ └── f' {calculate_age(year_of_birth)}.' │ ) │ │ └── def calculate_age(year_of_birth): │ ├── year_of_birth = 1980 # a_person[3] │ ├── ... │ └── return age │ except TypeError as error: │ ... └── else: └── self.assertEqual( reality, ( f'Hello, my name is {first_name}' f' {last_name} and I am' f' {self.calculate_age(year_of_birth)}.' ) │ ) └── def calculate_age(self, year_of_birth): ├── year_of_birth = 1980 # a_person[3] └── return self.this_year - year_of_birth
extract test_say_hello_method
The tests in test_joe, test_jane, test_john and test_mary make an instance of the Person object, then call its say_hello method comparing the results in assertions
self.assertEqual(
src.person.Person(
first_name=first_name,
last_name=last_name,
sex=sex,
year_of_birth=year_of_birth,
).say_hello(),
(
f'Hello, my name is {first_name}'
f' {last_name} and I am'
f' {self.calculate_age(year_of_birth)}.'
)
)
I can make one function with a for loop to remove the repetition.
RED: make it fail
I add a test for the say_hello method of the Person object
13 def assert_person_can_say_hello(
14 self,first_name, last_name,
15 sex, year_of_birth,
16 ):
17 self.assertEqual(
18 src.person.Person(
19 first_name=first_name,
20 last_name=last_name,
21 sex=sex,
22 year_of_birth=year_of_birth,
23 ).say_hello(),
24 (
25 f'Hello, my name is {first_name}'
26 f' {last_name} and I am'
27 f' {self.calculate_age(year_of_birth)}.'
28 )
29 )
31 def test_say_hello_method(self):
32 people = (
33 ('jane', 'doe', 'F', 1991),
34 ('joe', 'blow', 'M', 1996),
35 ('mary', 'public', 'F', 2000),
36 ('john', 'smith', 'M', 1980),
37 ('first_name', 'last_name', 'F', 'a_string')
38 )
39 for a_person in people:
40 first_name = a_person[0]
41 last_name = a_person[1]
42 sex = a_person[2]
43 year_of_birth = a_person[3]
44
45 self.assert_person_can_say_hello(
46 first_name=first_name,
47 last_name=last_name,
48 sex=sex,
49 year_of_birth=year_of_birth
50 )
51
52 def test_say_hello_function(self):
the terminal is my friend, and shows TypeError
E TypeError: 'a_string' is not an integer
this is the correct Exception for when the year_of_birth is not an integer. I need a better way to know which item in the people tuple raised the Exception.
the subTest method
unittest.TestCase has a method that I can use to show what items in a loop cause a failure in a test. It gives me a way to name each sub test in each loop.
I add a call to the unittest.TestCase.subTest method in test_say_hello_method
31 def test_say_hello_method(self): 32 people = ( 33 ('jane', 'doe', 'F', 1991), 34 ('joe', 'blow', 'M', 1996), 35 ('mary', 'public', 'F', 2000), 36 ('john', 'smith', 'M', 1980), 37 ('first_name', 'last_name', 'F', 'a_string') 38 ) 39 for a_person in people: 40 first_name = a_person[0] 41 last_name = a_person[1] 42 sex = a_person[2] 43 year_of_birth = a_person[3] 44 45 with self.subTest(first_name=first_name): 46 self.assert_person_can_say_hello( 47 first_name=first_name, 48 last_name=last_name, 49 sex=sex, 50 year_of_birth=year_of_birth 51 ) 52 53 def test_say_hello_function(self):the terminal is my friend, and shows TypeError
SUBFAILED(first_name='first_name') ...test_say_hello_method - TypeError: 'a string' is not an integerbetter.
SUBFAILED(first_name='first_name')shows the value I gave inwith self.subTest(first_name=first_name)as the label for the sub test. I can use any name and values I want.I add
year_of_birthto the call to the subTest method39 for a_person in people: 40 first_name = a_person[0] 41 last_name = a_person[1] 42 sex = a_person[2] 43 year_of_birth = a_person[3] 44 45 with self.subTest( 46 first_name=first_name, 47 year_of_birth=year_of_birth, 48 ): 49 self.assert_person_can_say_hello( 50 first_name=first_name, 51 last_name=last_name, 52 sex=sex, 53 year_of_birth=year_of_birth 54 )the terminal still shows AssertionError with the extra name and value.
SUBFAILED(first_name='first_name', year_of_birth='a string') ... test_say_hello_method - TypeError: 'a string' is not an integerthis is clearer.
GREEN: make it pass
I move the statements in the assert_person_can_say_hello method to a try…except…else clause like I did for test_say_hello_function
13 def assert_person_can_say_hello( 14 self,first_name, last_name, 15 sex, year_of_birth, 16 ): 17 try: 18 reality = src.person.Person( 19 first_name=first_name, 20 last_name=last_name, 21 sex=sex, 22 year_of_birth=year_of_birth, 23 ).say_hello() 24 except TypeError as error: 25 self.assertEqual( 26 error.args[0], 27 f"'{year_of_birth}' is not an integer" 28 ) 29 else: 30 self.assertEqual( 31 reality, 32 # src.person.Person( 33 # first_name=first_name, 34 # last_name=last_name, 35 # sex=sex, 36 # year_of_birth=year_of_birth, 37 # ).say_hello(), 38 ( 39 f'Hello, my name is {first_name}' 40 f' {last_name} and I am' 41 f' {self.calculate_age(year_of_birth)}.' 42 ) 43 ) 44 45 def test_say_hello_method(self):the test passes.
REFACTOR: make it better
I change the error message for if the value of the
year_of_birthparameter is not an integer in the calculate_age function, insrc/person/__init__.py37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError('BAP!!!') 40 raise TypeError( 41 f"'{year_of_birth}' is not an integer" 42 )the terminal is my friend, and shows ASsertionError
SUBFAILED(first_name='first_name', year_of_birth='a string') ... test_say_hello_method - AssertionError: 'BAP!!!' != "'a string' is not an integer" FAILED tests/test_person.py... test_say_hello_function - AssertionError: 'BAP!!!' != "'a string' is not an integer"because the error messages do not match
for a_person in ( │ ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), │ ('john', 'smith', 'M', 1980), ┌───┴── ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── first_name = a_person[0] # 'first_name' ├────── last_name = a_person[1] # 'last_name' ├────── sex = a_person[2] # 'F' ├────── year_of_birth = a_person[3] # 'a string' └────── with self.subTest( first_name=first_name, year_of_birth=year_of_birth, ): └── self.assert_person_can_say_hello( first_name=first_name, last_name=last_name, sex=sex, year_of_birth=year_of_birth ) └── def assert_person_can_say_hello( self,first_name, last_name, sex, year_of_birth, ): └── try: reality = src.person.Person( first_name=first_name, last_name=last_name, sex=sex, year_of_birth=year_of_birth, ).say_hello() └── src/person/__init__.py └── class Person: └── def __init__( self, first_name, last_name, sex, year_of_birth=None, is_citizen=True, passed_test=False, ): ├── self.first_name = first_name ├── self.last_name = last_name ├── self.year_of_birth = year_of_birth ├── self.sex = sex ├── self.is_citizen = is_citizen ├── self.passed_test = passed_test └── self.age = calculate_age( year_of_birth ) └── def calculate_age(year_of_birth): ├── year_of_birth = 'a string' └── if not isinstance( year_of_birth, int ): ┌─────────────────────────────────┴── raise TypeError('BAP!!!') └── except TypeError as error: └── self.assertEqual( error.args[0], f"'{year_of_birth}' is not an integer" ) └── raise AssertionError else: ...I want test_say_hello_function to also show which person raises an Exception. I add a call to the subTest method from test_say_hello_function in
tests/test_person.py70 def test_say_hello_function(self): 71 people = ( 72 ('jane', 'doe', 'F', 1991), 73 ('joe', 'blow', 'M', 1996), 74 ('mary', 'public', 'F', 2000), 75 ('john', 'smith', 'M', 1980), 76 ('first_name', 'last_name', 'F', 'a_string') 77 ) 78 for a_person in people: 79 first_name = a_person[0] 80 last_name = a_person[1] 81 year_of_birth = a_person[3]83 with self.subTest( 84 first_name=first_name, 85 year_of_birth=year_of_birth, 86 ): 87 try: 88 reality = src.person.say_hello( 89 first_name=first_name, 90 last_name=last_name, 91 year_of_birth=year_of_birth, 92 ) 93 except TypeError as error: 94 self.assertEqual( 95 error.args[0], 96 f"'{year_of_birth}' is not an integer" 97 ) 98 else: 99 self.assertEqual( 100 reality, 101 ( 102 f'Hello, my name is {first_name}' 103 f' {last_name} and I am' 104 f' {self.calculate_age(year_of_birth)}.' 105 ) 106 ) 107 108 def test_person_factory(self):the terminal shows AssertionError with the first name and year_of_birth of the person that raised the Exception
SUBFAILED(first_name='first_name', year_of_birth='a string') ... test_say_hello_method - AssertionError: 'BAP!!!' != "'a string' is not an integer" SUBFAILED(first_name='first_name', year_of_birth='a string') ... test_say_hello_function - AssertionError: 'BAP!!!' != "'a string' is not an integer"I change the error message in the calculate_age function for if the value of the
year_of_birthparameter is not an integer back to the correct message, insrc/person/__init__.py37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError( 40 f"'{year_of_birth}' is not an integer" 41 )the tests are green again.
I remove the call to the assert_person_can_say_hello from test_joe
135 def test_joe(self): 136 first_name = 'joe' 137 last_name = 'blow' 138 sex = 'M' 139 year_of_birth = 1996 140 141 joe = src.person.Person( 142 first_name=first_name, 143 last_name=last_name, 144 sex=sex, 145 year_of_birth=year_of_birth, 146 ) 147 self.assertEqual(joe.can_vote(), True) 148 self.assertEqual(joe.can_get_license(), False) 149 150 def test_jane(self):I remove the call to the assert_person_can_say_hello from test_jane
150 def test_jane(self): 151 first_name = 'jane' 152 last_name = 'doe' 153 sex = 'F' 154 year_of_birth = 1991 155 156 jane = src.person.Person( 157 first_name=first_name, 158 last_name=last_name, 159 sex=sex, 160 year_of_birth=year_of_birth, 161 passed_test=True, 162 ) 163 self.assertEqual(jane.can_vote(), True) 164 self.assertEqual(jane.can_get_license(), True) 165 166 def test_john(self):I remove the call to the assert_person_can_say_hello from test_john
166 def test_john(self): 167 first_name = 'john' 168 last_name = 'smith' 169 sex = 'M' 170 year_of_birth = 1980 171 172 john = src.person.Person( 173 first_name=first_name, 174 last_name=last_name, 175 sex=sex, 176 year_of_birth=year_of_birth, 177 is_citizen=False, 178 passed_test=False, 179 ) 180 self.assertEqual(john.can_vote(), False) 181 self.assertEqual(john.can_get_license(), False) 182 183 def test_mary(self):I remove the call to the assert_person_can_say_hello from test_mary
183 def test_mary(self): 184 first_name = 'mary' 185 last_name = 'public' 186 sex = 'F' 187 year_of_birth = 2000 188 189 mary = src.person.Person( 190 first_name=first_name, 191 last_name=last_name, 192 sex=sex, 193 year_of_birth=year_of_birth, 194 is_citizen=False, 195 passed_test=True, 196 ) 197 self.assertEqual(mary.can_vote(), False) 198 self.assertEqual(mary.can_get_license(), True) 199 200 def test_underage_citizen(self):I no longer need the assert_person_can_say_hello method because it is only used by test_say_hello_method
45 def test_say_hello_method(self): 46 people = ( 47 ('jane', 'doe', 'F', 1991), 48 ('joe', 'blow', 'M', 1996), 49 ('mary', 'public', 'F', 2000), 50 ('john', 'smith', 'M', 1980), 51 ('first_name', 'last_name', 'F', 'a_string') 52 ) 53 for a_person in people: 54 first_name = a_person[0] 55 last_name = a_person[1] 56 sex = a_person[2] 57 year_of_birth = a_person[3]59 with self.subTest( 60 first_name=first_name, 61 year_of_birth=year_of_birth, 62 ): 63 self.assert_person_can_say_hello( 64 first_name=first_name, 65 last_name=last_name, 66 sex=sex, 67 year_of_birth=year_of_birth 68 )70 try: 71 reality = src.person.Person( 72 first_name=first_name, 73 last_name=last_name, 74 sex=sex, 75 year_of_birth=year_of_birth, 76 ).say_hello() 77 except TypeError as error: 78 self.assertEqual( 79 error.args[0], 80 f"'{year_of_birth}' is not an integer" 81 ) 82 else: 83 self.assertEqual( 84 reality, 85 None 86 ) 87 88 def test_say_hello_function(self):the terminal is my friend, and shows AssertionError for each one of the people with an integer for
year_of_birthSUBFAILED(first_name='jane', year_of_birth=1991) ... - AssertionError: 'Hello, my name is jane doe and I am YY.' != None SUBFAILED(first_name='joe', year_of_birth=1996) ... - AssertionError: 'Hello, my name is joe blow and I am XX.' != None SUBFAILED(first_name='mary', year_of_birth=2000) ... - AssertionError: 'Hello, my name is mary public and I am ZZ.' != None SUBFAILED(first_name='john', year_of_birth=1980) ... - AssertionError: 'Hello, my name is john smith and I am AA.' != NoneI change the expectation of the assertion to
'Hello, my name is jane doe and I am YY.'82 else: 83 self.assertEqual( 84 reality, 85 'Hello, my name is jane doe and I am YY.' 86 ) 87 88 def test_say_hello_function(self):the terminal shows AssertionError
SUBFAILED(first_name='joe', year_of_birth=1996) ...test_say_hello_method - AssertionError: 'Hello, my name is joe blow and I am XX.' != 'Hello, my name is jane doe and I am YY.' SUBFAILED(first_name='mary', year_of_birth=2000) ...test_say_hello_method - AssertionError: 'Hello, my name is mary public and I am ZZ.' != 'Hello, my name is jane doe and I am YY.' SUBFAILED(first_name='john', year_of_birth=1980) ...test_say_hello_method - AssertionError: 'Hello, my name is john smith and I am AA.' != 'Hello, my name is jane doe and I am YY.'.the assertion now fails for the other people in the
peopletuple.I change the expectation to the f-string from the assert_person_can_say_hello method
82 else: 83 self.assertEqual( 84 reality, 85 ( 86 f'Hello, my name is {first_name}' 87 f' {last_name} and I am' 88 f' {self.calculate_age(year_of_birth)}.' 89 ) 90 ) 91 92 def test_say_hello_function(self):the test passes.
I remove the call to the assert_person_factory_works method from test_say_hello_method
45 def test_say_hello_method(self): 46 people = ( 47 ('jane', 'doe', 'F', 1991), 48 ('joe', 'blow', 'M', 1996), 49 ('mary', 'public', 'F', 2000), 50 ('john', 'smith', 'M', 1980), 51 ('first_name', 'last_name', 'F', 'a_string') 52 ) 53 for a_person in people: 54 first_name = a_person[0] 55 last_name = a_person[1] 56 sex = a_person[2] 57 year_of_birth = a_person[3]59 with self.subTest( 60 first_name=first_name, 61 year_of_birth=year_of_birth, 62 ): 63 try: 64 reality = src.person.Person( 65 first_name=first_name, 66 last_name=last_name, 67 sex=sex, 68 year_of_birth=year_of_birth, 69 ).say_hello() 70 except TypeError as error: 71 self.assertEqual( 72 error.args[0], 73 f"'{year_of_birth}' is not an integer" 74 ) 75 else: 76 self.assertEqual( 77 reality, 78 ( 79 f'Hello, my name is {first_name}' 80 f' {last_name} and I am' 81 f' {self.calculate_age(year_of_birth)}.' 82 ) 83 ) 84 85 def test_say_hello_function(self):I remove the assert_person_can_say_hello method from the TestPerson object
I add a git commit message
For each person in the people tuple, this test makes an instance of the Person object which makes an age attribute by calling the calculate_age function with the given year_of_birth parameter.
If the call raises TypeError, it asserts that the error message is correct
If the error message is not correct, it raises AssertionError
If the error message is correct, the test passes
for a_person in ( │ ('jane', 'doe', 'F', 1991), │ ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), │ ('john', 'smith', 'M', 1980), ┌───┴── ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── first_name = a_person[0] # 'first_name' ├────── last_name = a_person[1] # 'last_name' ├────── sex = a_person[2] # 'F' ├────── year_of_birth = a_person[3] # 'a string' └────── with self.subTest( first_name=first_name, year_of_birth=year_of_birth, ): └── try: reality = src.person.Person( first_name=first_name, last_name=last_name, sex=sex, year_of_birth=year_of_birth, ).say_hello() └── src/person/__init__.py └── class Person: └── def __init__( self, first_name, last_name, sex, year_of_birth=None, is_citizen=True, passed_test=False, ): ├── self.first_name = first_name ├── self.last_name = last_name ├── self.year_of_birth = year_of_birth ├── self.sex = sex ├── self.is_citizen = is_citizen ├── self.passed_test = passed_test └── self.age = calculate_age( year_of_birth ) └── def calculate_age(year_of_birth): ├── year_of_birth = 'a string' └── if not isinstance( year_of_birth, int ): ┌─────────────────────────────────┴── raise TypeError( │ f"'{year_of_birth}'" │ " is not an integer" │ ) └── except TypeError as error: └── self.assertEqual( error.args[0], f"'{year_of_birth}' is not an integer" ) else: ...
If the call to the calculate_age function from the __init__ method of the Person object does not raise TypeError, it asserts that the result of the call to the say_hello method matches the expectation
for person in ( │ ('jane', 'doe', 'F', 1991), ┌───┴── ('joe', 'blow', 'M', 1996), │ ('mary', 'public', 'F', 2000), │ ('john', 'smith', 'M', 1980), │ ('first_name', 'last_name', 'F', 'a string'), │ ): ├────── first_name = a_person[0] # 'joe' ├────── last_name = a_person[1] # 'blow' ├────── sex = a_person[2] # 'M' ├────── year_of_birth = a_person[3] # 1996 └────── with self.subTest( first_name=first_name, year_of_birth=year_of_birth, ): ├── try: │ reality = src.person.Person( │ first_name=first_name, │ last_name=last_name, │ sex=sex, │ year_of_birth=year_of_birth, │ ).say_hello() │ ├── src/person/__init__.py │ │ └── class Person: │ │ └── def __init__( │ │ self, first_name, last_name, │ │ sex, year_of_birth=None, │ │ is_citizen=True, passed_test=False, │ │ ): │ │ ├── self.first_name = first_name │ │ ├── self.last_name = last_name │ │ ├── self.year_of_birth = year_of_birth │ │ ├── self.sex = sex │ │ ├── self.is_citizen = is_citizen │ │ ├── self.passed_test = passed_test │ │ └── self.age = calculate_age( │ │ year_of_birth │ │ ) │ │ └── def calculate_age(year_of_birth): │ │ ├── year_of_birth = 'a string' │ │ └── if not isinstance( │ │ year_of_birth, int │ │ ): │ │ └── def calculate_age(year_of_birth): │ │ ├── year_of_birth = 1996 │ │ ├── ... │ │ └── return age │ └── def say_hello(self): │ └── return ( │ f'Hello, my name is {self.first_name}' │ f' {self.last_name} and I am {self.age}.' │ ) ├── except TypeError as error: │ ... └── else: └── self.assertEqual( reality, ( f'Hello, my name is {first_name}' f' {last_name} and I am' f' {self.calculate_age(year_of_birth)}.' ) ) │ └── def calculate_age(self, year_of_birth): ├── year_of_birth = 1996 └── return self.this_year - year_of_birth
extract people object attribute
test_person_factory, test_say_hello_function and test_say_hello_method all use the same tuple of persons.
I add an object attribute for the
peopletuple to the TestPerson object6class TestPerson(unittest.TestCase): 7 8 this_year = datetime.date.today().year 9 people = ( 10 ('jane', 'doe', 'F', 1991), 11 ('joe', 'blow', 'M', 1996), 12 ('mary', 'public', 'F', 2000), 13 ('john', 'smith', 'M', 1980), 14 ('first_name', 'last_name', 'F', 'a_string') 15 ) 16 17 def calculate_age(self, year_of_birth):I use the object attribute for
peoplein the for loop in test_say_hello_method20 def test_say_hello_method(self): 21 # people = ( 22 # ('jane', 'doe', 'F', 1991), 23 # ('joe', 'blow', 'M', 1996), 24 # ('mary', 'public', 'F', 2000), 25 # ('john', 'smith', 'M', 1980), 26 # ('first_name', 'last_name', 'F', 'a_string') 27 # ) 28 # for a_person in people: 29 for a_person in self.people: 30 first_name = a_person[0]the test is still green.
I remove the commented lines from test_say_hello_method
I use the object attribute for
peoplein the for loop in test_say_hello_function53 def test_say_hello_function(self): 54 # people = ( 55 # ('jane', 'doe', 'F', 1991), 56 # ('joe', 'blow', 'M', 1996), 57 # ('mary', 'public', 'F', 2000), 58 # ('john', 'smith', 'M', 1980), 59 # ('first_name', 'last_name', 'F', 'a_string') 60 # ) 61 # for a_person in people: 62 for a_person in self.people: 63 first_name = a_person[0]still green.
I remove the commented lines from test_say_hello_function
I use the object attribute for
peoplein the for loop in test_person_factory84 def test_person_factory(self): 85 # people = ( 86 # ('jane', 'doe', 'F', 1991), 87 # ('joe', 'blow', 'M', 1996), 88 # ('mary', 'public', 'F', 2000), 89 # ('john', 'smith', 'M', 1980), 90 # ('first_name', 'last_name', 'F', 'a_string') 91 # ) 92 # for a_person in people: 93 for a_person in self.people: 94 first_name = a_person[0]the test is still green.
I remove the commented lines from test_person_factory
I add a git commit message
extract test_if_person_can_vote
The tests in test_joe, test_jane, test_john, test_mary and test_underage_citizen make an instance of the Person object, then call its can_vote method comparing the results in assertions.
an_instance = src.person.Person(
first_name=first_name,
last_name=last_name,
sex=sex,
year_of_birth=year_of_birth,
is_citizen=False | True,
passed_test=False | True,
)
self.assertEqual(an_instance.can_vote(), False | True)
I can make one function with a for loop to remove the repetition.
The can_vote method of the Person object sends two parameters (age and response) when it calls the check_age method to return False or True for if a person cannot vote or can vote based on the following inputs
is the person younger than 18?
is the person a citizen?
The truth table for the can_vote method is
age < 18 |
is citizen |
can vote |
|---|---|---|
True |
True |
False |
True |
False |
False |
False |
True |
True |
False |
False |
False |
RED: make it fail
I add a test with an assertion for if a person is younger than 18 AND is a citizen
age < 18 |
is citizen |
can vote |
|---|---|---|
True |
True |
False |
84 def test_person_factory(self):
85 for a_person in self.people:
86 first_name = a_person[0]
87 last_name = a_person[1]
88 sex = a_person[2]
89 year_of_birth = a_person[3]
90
91 self.assertEqual(
92 src.person.person(
93 first_name=first_name,
94 last_name=last_name,
95 sex=sex,
96 year_of_birth=year_of_birth,
97 ),
98 (
99 f'{first_name}, {last_name},'
100 f' {sex}, {year_of_birth}'
101 )
102 )
104 def test_if_person_can_vote(self):
105 truth_table = (
106 (datetime.date.today().year-17, True),
107 )
108 for row in truth_table:
109 with self.subTest(row=row):
110 an_instance = src.person.Person(
111 first_name='first_name',
112 last_name='last_name',
113 sex='F',
114 year_of_birth=row[0],
115 is_citizen=row[1],
116 )
117 self.assertEqual(an_instance.can_vote(), True)
118
119 def test_joe(self):
the terminal is my friend, and shows AssertionError
SUBFAILED(row=(2009, True)) ...AssertionError: False != True
GREEN: make it pass
I change True to False in the assertion for if a person is younger than 18 AND is a citizen
104 def test_if_person_can_vote(self):
105 truth_table = (
106 (datetime.date.today().year-17, True),
107 )
108 for row in truth_table:
109 with self.subTest(row=row):
110 an_instance = src.person.Person(
111 first_name='first_name',
112 last_name='last_name',
113 sex='F',
114 year_of_birth=row[0],
115 is_citizen=row[1],
116 )
117 self.assertEqual(an_instance.can_vote(), False)
118
119 def test_joe(self):
the test passes.
REFACTOR: make it better
I add a tuple to the
truth_tabletuple for if a person is younger than18AND is NOT a citizenage < 18
is citizen
can vote
True
False
False
104 def test_if_person_can_vote(self): 105 truth_table = ( 106 (datetime.date.today().year-17, True), 107 (datetime.date.today().year-17, False), 108 ) 109 for row in truth_table:the test is still green.
I add a tuple to the
truth_tabletuple for if a person is NOT younger than18AND is a citizenage < 18
is citizen
can vote
False
True
True
104 def test_if_person_can_vote(self): 105 truth_table = ( 106 (datetime.date.today().year-17, True), 107 (datetime.date.today().year-17, False), 108 (datetime.date.today().year-18, True), 109 ) 110 for row in truth_table:the terminal is my friend, and shows AssertionError
SUBFAILED(row=(2008, True)) ...test_if_person_can_vote - AssertionError: True != FalseI add the expectation to the tuples for the rows in the
truth_tabletupleI use the index of the new values as the expectation of the assertion
110 for row in truth_table: 111 with self.subTest(row=row): 112 an_instance = src.person.Person( 113 first_name='first_name', 114 last_name='last_name', 115 sex='F', 116 year_of_birth=row[0], 117 is_citizen=row[1], 118 ) 119 self.assertEqual(an_instance.can_vote(), row[3]) 120 121def test_joe(self):the terminal is my friend, and shows IndexError
SUBFAILED(row=(2009, True, False)) ...test_if_person_can_vote - IndexError: tuple index out of range SUBFAILED(row=(2009, False, False)) ...test_if_person_can_vote - IndexError: tuple index out of range SUBFAILED(row=(2008, True, True)) ...test_if_person_can_vote - IndexError: tuple index out of rangeI change the index to the right number
the test passes.
I add a tuple to the
truth_tabletuple for if a person is NOT younger than18AND is NOT a citizenage < 18
is citizen
can vote
False
False
False
104 def test_if_person_can_vote(self): 105 truth_table = ( 106 (datetime.date.today().year-17, True, False), 107 (datetime.date.today().year-17, False, False), 108 (datetime.date.today().year-18, True, True), 109 (datetime.date.today().year-18, False, False), 110 ) 111 for row in truth_table: 112 with self.subTest(row=row): 113 an_instance = src.person.Person( 114 first_name='first_name', 115 last_name='last_name', 116 sex='F', 117 year_of_birth=row[0], 118 is_citizen=row[1], 119 ) 120 self.assertEqual( 121 an_instance.can_vote(), row[2] 122 ) 123 124 def test_joe(self):still green.
I remove the assertion for the can_vote method from test_joe
122 def test_joe(self): 123 first_name = 'joe' 124 last_name = 'blow' 125 sex = 'M' 126 year_of_birth = 1996 127 128 joe = 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 self.assertEqual(joe.can_get_license(), False) 135 136 def test_jane(self):I remove the assertion for the can_vote method from test_jane
136 def test_jane(self): 137 first_name = 'jane' 138 last_name = 'doe' 139 sex = 'F' 140 year_of_birth = 1991 141 142 jane = src.person.Person( 143 first_name=first_name, 144 last_name=last_name, 145 sex=sex, 146 year_of_birth=year_of_birth, 147 passed_test=True, 148 ) 149 self.assertEqual(jane.can_get_license(), True) 150 151 def test_john(self):I remove the assertion for the can_vote method from test_john
151 def test_john(self): 152 first_name = 'john' 153 last_name = 'smith' 154 sex = 'M' 155 year_of_birth = 1980 156 157 john = src.person.Person( 158 first_name=first_name, 159 last_name=last_name, 160 sex=sex, 161 year_of_birth=year_of_birth, 162 is_citizen=False, 163 passed_test=False, 164 ) 165 self.assertEqual(john.can_get_license(), False) 166 167 def test_mary(self):I remove the assertion for the can_vote method from test_mary
167 def test_mary(self): 168 first_name = 'mary' 169 last_name = 'public' 170 sex = 'F' 171 year_of_birth = 2000 172 173 mary = src.person.Person( 174 first_name=first_name, 175 last_name=last_name, 176 sex=sex, 177 year_of_birth=year_of_birth, 178 is_citizen=False, 179 passed_test=True, 180 ) 181 self.assertEqual(mary.can_get_license(), True) 182 183 def test_underage_citizen(self):I remove the assertion for the can_vote method from test_underage_citizen
183 def test_underage_citizen(self): 184 underage = src.person.Person( 185 first_name='first_name', 186 last_name='last_name', 187 sex='M', 188 year_of_birth=self.this_year-17, 189 is_citizen=True, 190 passed_test=True, 191 ) 192 self.assertEqual(underage.can_get_license(), False) 193 194 def test_when_person_is_older_than_120(self):I add a git commit message
extract test_if_person_can_get_license
The tests in test_joe, test_jane, test_john, test_mary and test_underage_citizen make an instance of the Person object, then call its can_get_license method comparing the results in assertions.
an_instance = src.person.Person(
first_name=first_name,
last_name=last_name,
sex=sex,
year_of_birth=year_of_birth,
is_citizen=False | True,
passed_test=False | True,
)
self.assertEqual(
an_instance.can_get_license(), False | True
)
I can make one function with a for loop to remove the repetition.
The can_get_license method of the Person object also sends two parameters when it calls the check_age method to return False or True for if a person cannot get a license or can get a license based on the following inputs
is the person younger than 18?
did the person pass the test?
The truth table for the can_get_license is
age < 18 |
passed test |
can get license |
|---|---|---|
True |
True |
False |
True |
False |
False |
False |
True |
True |
False |
False |
False |
RED: make it fail
I add a test with an assertion for if a person is NOT younger than 18 AND has NOT passed the test
age < 18 |
passed test |
can get license |
|---|---|---|
False |
False |
False |
120 self.assertEqual(an_instance.can_vote(), row[2])
121
122 def test_if_person_can_get_license(self):
123 truth_table = (
124 (datetime.date.today().year-18, False, False),
125 )
126 for row in truth_table:
127 with self.subTest(row=row):
128 an_instance = src.person.Person(
129 first_name='first_name',
130 last_name='last_name',
131 sex='M',
132 year_of_birth=row[0],
133 passed_test=row[1],
134 )
135 self.assertEqual(
136 an_instance.can_get_license(), True
137 )
138
139 def test_joe(self):
the terminal is my friend, and shows AssertionError
SUBFAILED(row=(2008, False, False)) ...test_if_person_can_get_license
- AssertionError: False != True
GREEN: make it pass
I change the expectation of the assertion
135 self.assertEqual(
136 an_instance.can_get_license(), False
137 )
the test passes.
REFACTOR: make it better
I add a tuple to the
truth_tabletuple for if a person is NOT younger than18AND has passed the testage < 18
passed test
can get license
False
True
True
122 def test_if_person_can_get_license(self): 123 truth_table = ( 124 (datetime.date.today().year-18, True, True), 125 (datetime.date.today().year-18, False, False), 126 ) 127 for row in truth_table:the terminal is my friend, and shows AssertionError
SUBFAILED(row=(2008, True, True)) ... test_if_person_can_get_license - AssertionError: True != FalseI use the index to change the expectation to the last item of the
rowtuple136 self.assertEqual( 137 an_instance.can_get_license(), row[-1] 138 ) 139 140 def test_joe(self):the test passes.
I add a tuple to the
truth_tabletuple for if a person is younger than18AND has NOT passed the testage < 18
passed test
can get license
True
False
False
124 def test_if_person_can_get_license(self): 125 truth_table = ( 126 (datetime.date.today().year-17, False, False), 127 (datetime.date.today().year-18, True, True), 128 (datetime.date.today().year-18, False, False), 129 ) 130 for row in truth_table:the test is still green.
I add a tuple to the
truth_tabletuple for if a person is younger than18AND has passed the testage < 18
passed test
can get license
True
True
False
124 def test_if_person_can_get_license(self): 125 truth_table = ( 126 (datetime.date.today().year-17, True, False), 127 (datetime.date.today().year-17, False, False), 128 (datetime.date.today().year-18, True, True), 129 (datetime.date.today().year-18, False, False), 130 ) 131 for row in truth_table:still green.
I remove test_joe, test_jane, test_john, test_mary and test_underage_citizen since they are now repetitions of test_if_person_can_get_license
122 def test_if_person_can_get_license(self): 123 truth_table = ( 124 (datetime.date.today().year-17, True, False), 125 (datetime.date.today().year-17, False, False), 126 (datetime.date.today().year-18, True, True), 127 (datetime.date.today().year-18, False, False), 128 ) 129 for row in truth_table: 130 with self.subTest(row=row): 131 an_instance = src.person.Person( 132 first_name='first_name', 133 last_name='last_name', 134 sex='M', 135 year_of_birth=row[0], 136 passed_test=row[1], 137 ) 138 self.assertEqual( 139 an_instance.can_get_license(), row[-1] 140 ) 141 142 def test_when_person_is_older_than_120(self):I add a git commit message
extract truth_table object attribute
The truth_table tuple is used in both test_if_person_can_vote and test_if_person_can_get_license. I can make an object attribute to remove the repetition.
I add an object attribute for the
truth_tabletuple to the TestPerson object6class TestPerson(unittest.TestCase): 7 8 this_year = datetime.date.today().year 9 people = ( 10 ('jane', 'doe', 'F', 1991), 11 ('joe', 'blow', 'M', 1996), 12 ('mary', 'public', 'F', 2000), 13 ('john', 'smith', 'M', 1980), 14 ('first_name', 'last_name', 'F', 'a_string') 15 ) 16 truth_table = ( 17 (datetime.date.today().year-17, True, False), 18 (datetime.date.today().year-17, False, False), 19 (datetime.date.today().year-18, True, True), 20 (datetime.date.today().year-18, False, False), 21 ) 22 23 def calculate_age(self, year_of_birth):I use the object attribute for
truth_tablein the for loop in test_if_person_can_vote110 def test_if_person_can_vote(self): 111 # truth_table = ( 112 # (datetime.date.today().year-17, True, False), 113 # (datetime.date.today().year-17, False, False), 114 # (datetime.date.today().year-18, True, True), 115 # (datetime.date.today().year-18, False, False), 116 # ) 117 # for row in truth_table: 118 for row in self.truth_table: 119 with self.subTest(row=row):the test is still green.
I remove the commented lines from test_if_person_can_vote
110 def test_if_person_can_vote(self): 111 for row in self.truth_table: 112 with self.subTest(row=row): 113 an_instance = src.person.Person( 114 first_name='first_name', 115 last_name='last_name', 116 sex='F', 117 year_of_birth=row[0], 118 is_citizen=row[1], 119 ) 120 self.assertEqual(an_instance.can_vote(), row[2]) 121 122 def test_if_person_can_get_license(self):I use the object attribute for
truth_tablein the for loop in test_if_person_can_get_licensestill green.
I remove the commented lines from test_if_person_can_vote
122 def test_if_person_can_get_license(self): 123 for row in self.truth_table: 124 with self.subTest(row=row): 125 an_instance = src.person.Person( 126 first_name='first_name', 127 last_name='last_name', 128 sex='M', 129 year_of_birth=row[0], 130 passed_test=row[1], 131 ) 132 self.assertEqual( 133 an_instance.can_get_license(), row[-1] 134 ) 135 136 def test_when_person_is_older_than_120(self):I add a git commit message
extract test_person_object
test_say_hello_method, test_if_person_can_vote and test_if_person_can_get_license make an instance of the Person object, then call methods and compare the results in assertions
reality = src.person.Person(
first_name=first_name,
last_name=last_name,
sex=sex,
year_of_birth=year_of_birth,
).say_hello()
self.assertEqual(
reality,
(
f'Hello, my name is {first_name}'
f' {last_name} and I am'
f' {self.calculate_age(year_of_birth)}.'
)
)
an_instance = src.person.Person(
first_name='first_name',
last_name='last_name',
sex='M',
year_of_birth=row[0],
passed_test=row[1],
)
self.assertEqual(an_instance.can_vote(), row[2])
self.assertEqual(an_instance.can_get_license(), row[-1])
I can make one function with a for loop to remove the repetition.
RED: make it fail
I add a test with the code from test_say_hello_method for the Person object
23 def calculate_age(self, year_of_birth):
24 return self.this_year - year_of_birth
25
26 def test_person_object(self):
27 for a_person in self.people:
28 first_name = a_person[0]
29 last_name = a_person[1]
30 sex = a_person[2]
31 year_of_birth = a_person[3]
33 with self.subTest(
34 first_name=first_name,
35 year_of_birth=year_of_birth,
36 ):
37 try:
38 an_instance = src.person.Person(
39 first_name=first_name,
40 last_name=last_name,
41 sex=sex,
42 year_of_birth=year_of_birth,
43 )
44 except TypeError as error:
45 self.assertEqual(
46 error.args[0],
47 f"'{year_of_birth}' is not an integer"
48 )
49 else:
50 self.assertEqual(
51 an_instance.boom(),
52 (
53 f'Hello, my name is {first_name}'
54 f' {last_name} and I am'
55 f' {self.calculate_age(year_of_birth)}.'
56 )
57 )
58
59 def test_say_hello_method(self):
the terminal is my friend, and shows AttributeError
SUBFAILED(first_name='jane', year_of_birth=1991)
AttributeError: 'Person' object has no attribute 'boom'
SUBFAILED(first_name='joe', year_of_birth=1996)
AttributeError: 'Person' object has no attribute 'boom'
SUBFAILED(first_name='mary', year_of_birth=2000)
AttributeError: 'Person' object has no attribute 'boom'
SUBFAILED(first_name='john', year_of_birth=1980)
AttributeError: 'Person' object has no attribute 'boom'
GREEN: make it pass
I change boom to say_hello in the assertion
49 else:
50 self.assertEqual(
51 an_instance.say_hello(),
52 (
53 f'Hello, my name is {first_name}'
54 f' {last_name} and I am'
55 f' {self.calculate_age(year_of_birth)}.'
56 )
57 )
58
59 def test_say_hello_method(self):
the test passes.
REFACTOR: make it better
I remove test_say_hello_method because it is now a repetition
I add an assertion for the can_vote method to test_person_object
49 else: 50 self.assertEqual( 51 an_instance.say_hello(), 52 ( 53 f'Hello, my name is {first_name}' 54 f' {last_name} and I am' 55 f' {self.calculate_age(year_of_birth)}.' 56 ) 57 ) 58 self.assertEqual(an_instance.can_vote(), False) 59 60 def test_say_hello_function(self):the terminal is my friend, and shows AssertionError
SUBFAILED(first_name='jane', year_of_birth=1991) AssertionError: True != False SUBFAILED(first_name='joe', year_of_birth=1996) AssertionError: True != False SUBFAILED(first_name='mary', year_of_birth=2000) AssertionError: True != False SUBFAILED(first_name='john', year_of_birth=1980) AssertionError: True != FalseI add values for
is_citizento thepeopleobject attribute of the TestPerson object6class TestPerson(unittest.TestCase): 7 8 this_year = datetime.date.today().year 9 people = ( 10 ( 11 'jane', 'doe', 'F', 1991, 12 True, 13 ), 14 ( 15 'joe', 'blow', 'M', 1996, 16 True, 17 ), 18 ( 19 'mary', 'public', 'F', 2000, 20 False, 21 ), 22 ( 23 'john', 'smith', 'M', 1980, 24 False, 25 ), 26 ( 27 'first_name', 'last_name', 'F', 'a_string', 28 False, 29 ) 30 )I use the values for the
is_citizenparameter when making an instance of the Person object in test_person_object41 def test_person_object(self): 42 for a_person in self.people: 43 first_name = a_person[0] 44 last_name = a_person[1] 45 sex = a_person[2] 46 year_of_birth = a_person[3] 47 is_citizen = a_person[4]49 with self.subTest( 50 first_name=first_name, 51 year_of_birth=year_of_birth, 52 ): 53 try: 54 an_instance = src.person.Person( 55 first_name=first_name, 56 last_name=last_name, 57 sex=sex, 58 year_of_birth=year_of_birth, 59 is_citizen=is_citizen, 60 ) 61 except TypeError as error:the terminal still shows AssertionError
I change the expectation of the assertion for the can_vote method to use the value of the
is_citizenvariable66 else: 67 self.assertEqual( 68 an_instance.say_hello(), 69 ( 70 f'Hello, my name is {first_name}' 71 f' {last_name} and I am' 72 f' {self.calculate_age(year_of_birth)}.' 73 ) 74 ) 75 self.assertEqual(an_instance.can_vote(), is_citizen) 76 77 def test_say_hello_function(self):the test passes.
I add an assertion for the can_get_license method to test_person_object
66 else: 67 self.assertEqual( 68 an_instance.say_hello(), 69 ( 70 f'Hello, my name is {first_name}' 71 f' {last_name} and I am' 72 f' {self.calculate_age(year_of_birth)}.' 73 ) 74 ) 75 self.assertEqual(an_instance.can_vote(), is_citizen) 76 self.assertEqual(an_instance.can_get_license(), True) 77 78 def test_say_hello_function(self):the terminal is my friend, and shows AssertionError
SUBFAILED(first_name='jane', year_of_birth=1991) AssertionError: False != True SUBFAILED(first_name='joe', year_of_birth=1996) AssertionError: False != True SUBFAILED(first_name='mary', year_of_birth=2000) AssertionError: False != True SUBFAILED(first_name='john', year_of_birth=1980) AssertionError: False != TrueI add values for
passed_testto thepeopleobject attribute of the TestPerson object9 people = ( 10 ( 11 'jane', 'doe', 'F', 1991, 12 True, True, 13 ), 14 ( 15 'joe', 'blow', 'M', 1996, 16 True, False, 17 ), 18 ( 19 'mary', 'public', 'F', 2000, 20 False, True, 21 ), 22 ( 23 'john', 'smith', 'M', 1980, 24 False, False, 25 ), 26 ( 27 'first_name', 'last_name', 'F', 'a_string', 28 False, False, 29 ) 30 )I use the values for the
passed_testparameter when making an instance of the Person object in test_person_object41 def test_person_object(self): 42 for a_person in self.people: 43 first_name = a_person[0] 44 last_name = a_person[1] 45 sex = a_person[2] 46 year_of_birth = a_person[3] 47 is_citizen = a_person[4] 48 passed_test = a_person[5]49 with self.subTest( 50 first_name=first_name, 51 year_of_birth=year_of_birth, 52 ): 53 try: 54 an_instance = src.person.Person( 55 first_name=first_name, 56 last_name=last_name, 57 sex=sex, 58 year_of_birth=year_of_birth, 59 is_citizen=is_citizen, 60 passed_test=passed_test, 61 ) 62 except TypeError as error:the terminal still shows AssertionError
I change the expectation of the assertion for the can_get_license method to use the value of the
passed_testvariable68 else: 69 self.assertEqual( 70 an_instance.say_hello(), 71 ( 72 f'Hello, my name is {first_name}' 73 f' {last_name} and I am' 74 f' {self.calculate_age(year_of_birth)}.' 75 ) 76 ) 77 self.assertEqual(an_instance.can_vote(), is_citizen) 78 self.assertEqual( 79 an_instance.can_get_license(), passed_test 80 ) 81 82 def test_say_hello_function(self):the test passes.
test_person_object is missing the cases for if a person younger than
18age < 18
is citizen
can vote
True
True
False
True
False
False
age < 18
passed test
can get license
True
True
False
True
False
False
which gives this truth table when put together
age < 18
is citizen
passed_test
can vote
can_get_license
True
True
True
False
False
True
False
True
False
False
True
True
False
False
False
True
False
False
False
False
I add tuples for these cases to the
peopleobject attribute9people = ( 10 ( 11 'jane', 'doe', 'F', 1991, 12 True, True, 13 ), 14 ( 15 'joe', 'blow', 'M', 1996, 16 True, False, 17 ), 18 ( 19 'mary', 'public', 'F', 2000, 20 False, True, 21 ), 22 ( 23 'john', 'smith', 'M', 1980, 24 False, False, 25 ),26 ( 27 # underage, is_citizen, passed_test 28 'first_name', 'last_name', 'M', 29 datetime.date.today().year-17, 30 True, True, 31 ), 32 ( 33 # underage, not is_citizen, passed_test 34 'first_name', 'last_name', 'M', 35 datetime.date.today().year-17, 36 False, True, 37 ), 38 ( 39 # underage, is_citizen, not passed_test 40 'first_name', 'last_name', 'M', 41 datetime.date.today().year-17, 42 True, False, 43 ), 44 ( 45 # underage, not is_citizen, not passed_test 46 'first_name', 'last_name', 'M', 47 datetime.date.today().year-17, 48 False, False, 49 ), 50 ( 51 'first_name', 'last_name', 'F', 'a_string', 52 False, False, 53 ) 54)the terminal is my friend, and shows AssertionError
SUBFAILED(first_name='first_name', year_of_birth=YYYY) ... AssertionError: False != True SUBFAILED(first_name='first_name', year_of_birth=YYYY) ... AssertionError: False != True SUBFAILED(first_name='first_name', year_of_birth=YYYY) ... AssertionError: False != TrueI need a better error message, so I know which tuples caused the failures.
I change the names in the subTest method to show the entire tuple in test_person_object
65 def test_person_object(self): 66 for a_person in self.people: 67 first_name = a_person[0] 68 last_name = a_person[1] 69 sex = a_person[2] 70 year_of_birth = a_person[3] 71 is_citizen = a_person[4] 72 passed_test = a_person[5] 73 74 with self.subTest(a_person): 75 try:the terminal shows
SUBFAILED[('first_name', 'last_name', 'M', YYYY, True, True)] ... AssertionError: False != True SUBFAILED[('first_name', 'last_name', 'M', YYYY, False, True)] ... AssertionError: False != True SUBFAILED[('first_name', 'last_name', 'M', YYYY, True, False)] ... AssertionError: False != Truebecause I used the value for the
is_citizenvariable as the expectation of the assertion for the can_vote method which is wrong when the person is younger than18, the expectation should always be False.I add the expectation for all the cases of the can_vote method to the
peopleobject attribute9 people = ( 10 ( 11 'jane', 'doe', 'F', 1991, 12 True, True, True, 13 ), 14 ( 15 'joe', 'blow', 'M', 1996, 16 True, False, True, 17 ), 18 ( 19 'mary', 'public', 'F', 2000, 20 False, True, False, 21 ),22 ( 23 'john', 'smith', 'M', 1980, 24 False, False, False, 25 ), 26 ( 27 # underage, is_citizen, passed_test 28 'first_name', 'last_name', 'M', 29 datetime.date.today().year-17, 30 True, True, False, 31 ), 32 ( 33 # underage, not is_citizen, passed_test 34 'first_name', 'last_name', 'M', 35 datetime.date.today().year-17, 36 False, True, False, 37 ),38 ( 39 # underage, is_citizen, not passed_test 40 'first_name', 'last_name', 'M', 41 datetime.date.today().year-17, 42 True, False, False, 43 ), 44 ( 45 # underage, not is_citizen, not passed_test 46 'first_name', 'last_name', 'M', 47 datetime.date.today().year-17, 48 False, False, False, 49 ), 50 ( 51 'first_name', 'last_name', 'F', 'a_string', 52 False, False, False, 53 ) 54 )I remove the
is_citizenvariable then change the expectation of the assertion for the can_vote method in test_person_object to the new item I added to the tuples in thepeopleobject attribute65 def test_person_object(self): 66 for a_person in self.people: 67 first_name = a_person[0] 68 last_name = a_person[1] 69 sex = a_person[2] 70 year_of_birth = a_person[3] 71 # is_citizen = a_person[4] 72 passed_test = a_person[5]74 with self.subTest(a_person): 75 try: 76 an_instance = src.person.Person( 77 first_name=first_name, 78 last_name=last_name, 79 sex=sex, 80 year_of_birth=year_of_birth, 81 # is_citizen=is_citizen, 82 is_citizen=a_person[4], 83 passed_test=passed_test, 84 ) 85 except TypeError as error:90 else: 91 self.assertEqual( 92 an_instance.say_hello(), 93 ( 94 f'Hello, my name is {first_name}' 95 f' {last_name} and I am' 96 f' {self.calculate_age(year_of_birth)}.' 97 ) 98 ) 99 # self.assertEqual(an_instance.can_vote(), is_citizen) 100 self.assertEqual(an_instance.can_vote(), a_person[6]) 101 self.assertEqual( 102 an_instance.can_get_license(), passed_test 103 ) 104 105 def test_say_hello_function(self):the terminal is my friend, and shows AssertionError
SUBFAILED[('first_name', 'last_name', 'M', YYYY, True, True, False)] ... AssertionError: False != True SUBFAILED[('first_name', 'last_name', 'M', YYYY, False, True, False)] ... AssertionError: False != Truebecause I used the value for the
passed_testvariable as the expectation of the assertion for the can_get_license method which is wrong when the person is younger than18, the expectation should always be False.I add the expectation for all the cases of the can_get_license method to the
peopleobject attribute9 people = ( 10 ( 11 'jane', 'doe', 'F', 1991, 12 True, True, True, True, 13 ), 14 ( 15 'joe', 'blow', 'M', 1996, 16 True, False, True, False, 17 ), 18 ( 19 'mary', 'public', 'F', 2000, 20 False, True, False, True, 21 ),22 ( 23 'john', 'smith', 'M', 1980, 24 False, False, False, False, 25 ), 26 ( 27 # underage, is_citizen, passed_test 28 'first_name', 'last_name', 'M', 29 datetime.date.today().year-17, 30 True, True, False, False, 31 ), 32 ( 33 # underage, not is_citizen, passed_test 34 'first_name', 'last_name', 'M', 35 datetime.date.today().year-17, 36 False, True, False, False, 37 ),38 ( 39 # underage, is_citizen, not passed_test 40 'first_name', 'last_name', 'M', 41 datetime.date.today().year-17, 42 True, False, False, False, 43 ), 44 ( 45 # underage, not is_citizen, not passed_test 46 'first_name', 'last_name', 'M', 47 datetime.date.today().year-17, 48 False, False, False, False, 49 ), 50 ( 51 'first_name', 'last_name', 'F', 'a_string', 52 False, False, False, False, 53 ) 54 ) 55 truth_table = ( 56 (datetime.date.today().year-17, True, False), 57 (datetime.date.today().year-17, False, False), 58 (datetime.date.today().year-18, True, True), 59 (datetime.date.today().year-18, False, False), 60 ) 61 62 def calculate_age(self, year_of_birth):I remove the
passed_testvariable then change the expectation of the assertion for the can_get_license method in test_person_object to the new item I added to the tuples in thepeopleobject attribute65 def test_person_object(self): 66 for a_person in self.people: 67 first_name = a_person[0] 68 last_name = a_person[1] 69 sex = a_person[2] 70 year_of_birth = a_person[3] 71 # is_citizen = a_person[4] 72 # passed_test = a_person[5]74 with self.subTest(a_person): 75 try: 76 an_instance = src.person.Person( 77 first_name=first_name, 78 last_name=last_name, 79 sex=sex, 80 year_of_birth=year_of_birth, 81 # is_citizen=is_citizen, 82 is_citizen=a_person[4], 83 # passed_test=passed_test, 84 passed_test=a_person[5], 85 ) 86 except TypeError as error:91 else: 92 self.assertEqual( 93 an_instance.say_hello(), 94 ( 95 f'Hello, my name is {first_name}' 96 f' {last_name} and I am' 97 f' {self.calculate_age(year_of_birth)}.' 98 ) 99 ) 100 # self.assertEqual(an_instance.can_vote(), is_citizen) 101 self.assertEqual(an_instance.can_vote(), a_person[6]) 102 self.assertEqual( 103 # an_instance.can_get_license(), passed_test 104 an_instance.can_get_license(), a_person[7] 105 ) 106 107 def test_say_hello_function(self):the test passes.
I remove the commented lines from test_person_object
65 def test_person_object(self): 66 for a_person in self.people: 67 first_name = a_person[0] 68 last_name = a_person[1] 69 sex = a_person[2] 70 year_of_birth = a_person[3]72 with self.subTest(a_person): 73 try: 74 an_instance = src.person.Person( 75 first_name=first_name, 76 last_name=last_name, 77 sex=sex, 78 year_of_birth=year_of_birth, 79 is_citizen=a_person[4], 80 passed_test=a_person[5], 81 ) 82 except TypeError as error: 83 self.assertEqual( 84 error.args[0], 85 f"'{year_of_birth}' is not an integer" 86 )87 else: 88 self.assertEqual( 89 an_instance.say_hello(), 90 ( 91 f'Hello, my name is {first_name}' 92 f' {last_name} and I am' 93 f' {self.calculate_age(year_of_birth)}.' 94 ) 95 ) 96 self.assertEqual(an_instance.can_vote(), a_person[6]) 97 self.assertEqual( 98 an_instance.can_get_license(), a_person[7] 99 ) 100 101 def test_say_hello_function(self):I remove test_if_person_can_vote because it is now a repetition
I remove test_if_person_can_get_license because it is now a repetition
132 def test_person_factory(self): 133 for a_person in self.people: 134 first_name = a_person[0] 135 last_name = a_person[1] 136 sex = a_person[2] 137 year_of_birth = a_person[3] 138 139 self.assertEqual( 140 src.person.person( 141 first_name=first_name, 142 last_name=last_name, 143 sex=sex, 144 year_of_birth=year_of_birth, 145 ), 146 ( 147 f'{first_name}, {last_name},' 148 f' {sex}, {year_of_birth}' 149 ) 150 ) 151 152 def test_when_person_is_older_than_120(self):I remove the
truth_tableobject_attribute because it is no longer usedI add a git commit message
extract object attributes
The people object attribute can be described with the following truth table
If a person is younger than
18age < 18
is citizen
passed_test
can vote
can_get_license
True
True
True
False
False
True
False
True
False
False
True
True
False
False
False
True
False
False
False
False
If a person is NOT younger than
18age < 18
is citizen
passed_test
can vote
can_get_license
person
False
True
True
True
True
jane
False
False
True
False
True
mary
False
True
False
True
False
joe
False
False
False
False
False
john
I make an object attribute for
datetime.date.today().year-17I change the comments then use the new object attribute for
datetime.date.today().year-17from thepeopleobject attribute27 ( 28 # is_citizen, passed_test 29 'first_name', 'last_name', 'M', 30 younger_than_18, 31 True, True, False, False, 32 ), 33 ( 34 # not is_citizen, passed_test 35 'first_name', 'last_name', 'M', 36 younger_than_18, 37 False, True, False, False, 38 ), 39 ( 40 # is_citizen, not passed_test 41 'first_name', 'last_name', 'M', 42 younger_than_18, 43 True, False, False, False, 44 ), 45 ( 46 # not is_citizen, not passed_test 47 'first_name', 'last_name', 'M', 48 younger_than_18, 49 False, False, False, False, 50 ), 51 ( 52 'first_name', 'last_name', 'F', 'a_string', 53 False, False, False, False, 54 ) 55 )the tests are still green.
I add a new object attribute for if a person is NOT younger than
18I add comments then use the new object attribute for the
year_of_birthvalues ofjane,joe,maryandjohnin thepeopleobject attribute11 people = ( 12 ( 13 # is_citizen, passed_test 14 'jane', 'doe', 'F', 15 not_younger_than_18, 16 True, True, True, True, 17 ), 18 ( 19 # is_citizen, not passed_test 20 'joe', 'blow', 'M', 21 not_younger_than_18, 22 True, False, True, False, 23 ), 24 ( 25 # not is_citizen, passed_test 26 'mary', 'public', 'F', 27 not_younger_than_18, 28 False, True, False, True, 29 ), 30 ( 31 # not is_citizen, not passed_test 32 'john', 'smith', 'M', 33 not_younger_than_18, 34 False, False, False, False, 35 ),still green.
I change the values for
first_name,last_nameandsexto'first_name', 'last_name', 'M',for all the tuples in thepeopleobject attribute11 people = ( 12 ( 13 # is_citizen, passed_test 14 'first_name', 'last_name', 'M', 15 not_younger_than_18, 16 True, True, True, True, 17 ), 18 ( 19 # not is_citizen, not passed_test 20 'first_name', 'last_name', 'M', 21 not_younger_than_18, 22 True, False, True, False, 23 ), 24 ( 25 # not is_citizen, passed_test 26 'first_name', 'last_name', 'M', 27 not_younger_than_18, 28 False, True, False, True, 29 ), 30 ( 31 # not is_citizen, not passed_test 32 'first_name', 'last_name', 'M', 33 not_younger_than_18, 34 False, False, False, False, 35 ),green.
I add object attributes for
first_name,last_nameandsexI use the new object attributes for
first_name,last_nameandsex, move them out of the for loop in test_person_object74 def test_person_object(self): 75 first_name = self.first_name 76 last_name = self.last_name 77 sex = self.sex 78 79 for a_person in self.people: 80 year_of_birth = a_person[3] 81 82 with self.subTest(a_person):still green.
I use the new object attributes for
first_nameandlast_name, then move them out of the for loop in test_say_hello_function111 def test_say_hello_function(self): 112 first_name = self.first_name 113 last_name = self.last_name 114 115 for a_person in self.people: 116 year_of_birth = a_person[3] 117 118 with self.subTest(a_person): 119 try:the test is still green.
I use the new object attributes for
first_name,last_nameandsex, then move them out of the for loop in test_person_factory140 def test_person_factory(self): 141 first_name = self.first_name 142 last_name = self.last_name 143 sex = self.sex 144 145 for a_person in self.people: 146 year_of_birth = a_person[3]148 with self.subTest(a_person): 149 self.assertEqual( 150 src.person.person( 151 first_name=first_name, 152 last_name=last_name, 153 sex=sex, 154 year_of_birth=year_of_birth, 155 ), 156 ( 157 f'{first_name}, {last_name},' 158 f' {sex}, {year_of_birth}' 159 ) 160 ) 161 162 def test_when_person_is_older_than_120(self):still green.
I use the object attributes directly in test_person_factory (I do not need the variables)
140 def test_person_factory(self): 141 for a_person in self.people: 142 year_of_birth = a_person[3] 143 144 with self.subTest(a_person): 145 self.assertEqual( 146 src.person.person( 147 first_name=self.first_name, 148 last_name=self.last_name, 149 sex=self.sex, 150 year_of_birth=year_of_birth, 151 ), 152 ( 153 f'{self.first_name}, {self.last_name},' 154 f' {self.sex}, {year_of_birth}' 155 ) 156 ) 157 158 def test_when_person_is_older_than_120(self):green.
I use the object attributes directly in test_say_hello_function
111 def test_say_hello_function(self): 112 for a_person in self.people: 113 year_of_birth = a_person[3] 114 115 with self.subTest(a_person): 116 try: 117 reality = src.person.say_hello( 118 first_name=self.first_name, 119 last_name=self.last_name, 120 year_of_birth=year_of_birth, 121 )122 except TypeError as error: 123 self.assertEqual( 124 error.args[0], 125 f"'{year_of_birth}' is not an integer" 126 ) 127 else: 128 self.assertEqual( 129 reality, 130 ( 131 f'Hello, my name is {self.first_name}' 132 f' {self.last_name} and I am' 133 f' {self.calculate_age(year_of_birth)}.' 134 ) 135 ) 136 137 def test_person_factory(self):I use the object attributes directly in test_person_object
74 def test_person_object(self): 75 for a_person in self.people: 76 year_of_birth = a_person[3] 77 78 with self.subTest(a_person): 79 try: 80 an_instance = src.person.Person( 81 first_name=self.first_name, 82 last_name=self.last_name, 83 sex=self.sex, 84 year_of_birth=year_of_birth, 85 is_citizen=a_person[4], 86 passed_test=a_person[5], 87 )88 except TypeError as error: 89 self.assertEqual( 90 error.args[0], 91 f"'{year_of_birth}' is not an integer" 92 ) 93 else: 94 self.assertEqual( 95 an_instance.say_hello(), 96 ( 97 f'Hello, my name is {self.first_name}' 98 f' {self.last_name} and I am' 99 f' {self.calculate_age(year_of_birth)}.' 100 ) 101 ) 102 self.assertEqual(an_instance.can_vote(), a_person[6]) 103 self.assertEqual( 104 an_instance.can_get_license(), a_person[7] 105 ) 106 107 def test_say_hello_function(self)I remove the values of
first_name,last_nameamdsexfrom the tuples in thepeopleobject attribute16 people = ( 17 ( 18 # is_citizen, passed_test 19 # 'first_name', 'last_name', 'M', 20 not_younger_than_18, 21 True, True, True, True, 22 ),the terminal is my friend, and shows IndexError and ValueError
SUBFAILED[(2008, True, True, True, True)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2008, True, True, True, True)] ...test_say_hello_function - ValueErrorbecause the positions changed when I changed the tuple. I would have to change all the indexes if I change the tuple, there has to be a better way
I undo the change
16 people = ( 17 ( 18 # is_citizen, passed_test 19 'first_name', 'last_name', 'M', 20 not_younger_than_18, 21 True, True, True, True, 22 ),the tests are green again.
I change the values for the first three items of the tuples in the
peopleobject attribute to theyear_of_birthvalue and the boolean foris_citizenandpassed_test16 people = ( 17 ( 18 # is_citizen, passed_test 19 not_younger_than_18, True, True, 20 not_younger_than_18, 21 True, True, True, True, 22 ), 23 ( 24 # is_citizen, not passed_test 25 not_younger_than_18, True, False, 26 not_younger_than_18, 27 True, False, True, False, 28 ), 29 ( 30 # not is_citizen, passed_test 31 not_younger_than_18, False, True, 32 not_younger_than_18, 33 False, True, False, True, 34 ),35 ( 36 # not is_citizen, not passed_test 37 not_younger_than_18, False, False, 38 not_younger_than_18, 39 False, False, False, False, 40 ), 41 ( 42 # underage, is_citizen, passed_test 43 younger_than_18, True, True, 44 younger_than_18, 45 True, True, False, False, 46 ), 47 ( 48 # underage, not is_citizen, passed_test 49 younger_than_18, False, True, 50 younger_than_18, 51 False, True, False, False, 52 ),53 ( 54 # underage, is_citizen, not passed_test 55 younger_than_18, True, False, 56 younger_than_18, 57 True, False, False, False, 58 ), 59 ( 60 # underage, not is_citizen, not passed_test 61 younger_than_18, False, False, 62 younger_than_18, 63 False, False, False, False, 64 ), 65 ( 66 'a_string', False, False, 67 'a_string', 68 False, False, False, False, 69 ) 70 ) 71 72 def calculate_age(self, year_of_birth):the tests are still green.
I change the indexes for
year_of_birth,is_citizenandpassed_testin test_person_object75 def test_person_object(self): 76 for a_person in self.people: 77 year_of_birth = a_person[0] 78 79 with self.subTest(a_person): 80 try: 81 person = src.person.Person( 82 first_name=self.first_name, 83 last_name=self.last_name, 84 sex=self.sex, 85 year_of_birth=year_of_birth, 86 is_citizen=a_person[1], 87 passed_test=a_person[2], 88 ) 89 except TypeError as error:still green.
I change the index for
year_of_birthin test_say_hello_function108 def test_say_hello_function(self): 109 for a_person in self.people: 110 year_of_birth = a_person[0] 111 112 with self.subTest(a_person):green.
I change the index for
year_of_birthin test_person_factory134 def test_person_factory(self): 135 for a_person in self.people: 136 year_of_birth = a_person[0] 137 138 with self.subTest(a_person):still green.
I remove the repeated values from the tuples in the
peopleobject attribute16 people = ( 17 ( 18 # is_citizen, passed_test 19 not_younger_than_18, True, True, 20 True, True, 21 ), 22 ( 23 # is_citizen, not passed_test 24 not_younger_than_18, True, False, 25 True, False, 26 ), 27 ( 28 # not is_citizen, passed_test 29 not_younger_than_18, False, True, 30 False, True, 31 ),32 ( 33 # not is_citizen, not passed_test 34 not_younger_than_18, False, False, 35 False, False, 36 ), 37 ( 38 # underage, is_citizen, passed_test 39 younger_than_18, True, True, 40 False, False, 41 ), 42 ( 43 # underage, not is_citizen, passed_test 44 younger_than_18, False, True, 45 False, False, 46 ),47 ( 48 # underage, is_citizen, not passed_test 49 younger_than_18, True, False, 50 False, False, 51 ), 52 ( 53 # underage, not is_citizen, not passed_test 54 younger_than_18, False, False, 55 False, False, 56 ), 57 ( 58 'a_string', False, False, 59 False, False, 60 ) 61 ) 62 63 def calculate_age(self, year_of_birth):the terminal is my friend, and shows IndexError
SUBFAILED[(2008, True, True, True, True)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2008, True, False, True, False)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2008, False, True, False, True)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2008, False, False, False, False)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2009, True, True, False, False)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2009, False, True, False, False)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2009, True, False, False, False)] ...test_person_object - IndexError: tuple index out of range SUBFAILED[(2009, False, False, False, False)] ...test_person_object - IndexError: tuple index out of rangeI change the indexes for the expectations of the results to calls of the can_vote and can_get_license methods in test_person_object
85 else: 86 self.assertEqual( 87 an_instance.say_hello(), 88 ( 89 f'Hello, my name is {self.first_name}' 90 f' {self.last_name} and I am' 91 f' {self.calculate_age(year_of_birth)}.' 92 ) 93 ) 94 self.assertEqual(an_instance.can_vote(), a_person[3]) 95 self.assertEqual( 96 an_instance.can_get_license(), a_person[4] 97 ) 98 99 def test_say_hello_function(self):the test is green again.
I add a git commit message
use assertRaises with test_when_person_is_older_than_120
RED: make it fail
I use the assertRaises method to replace the try statement in test_when_person_is_older_than_120
143 def test_when_person_is_older_than_120(self):
144 # try:
145 with self.assertRaises(TypeError):
146 src.person.Person(
147 first_name='first_name',
148 last_name='last_name',
149 sex='M',
150 year_of_birth=self.this_year-121
151 )
152 # except ValueError:
153 # pass
154
155 def test_when_year_of_birth_is_the_future(self):
the terminal is my friend, and shows ValueError
E ValueError
GREEN: make it pass
I change the Exception to match
143 def test_when_person_is_older_than_120(self):
144 # try:
145 with self.assertRaises(ValueError):
the test passes.
REFACTOR: make it better
I change assertRaises to assertRaisesRegex to test the message of the Exception
143 def test_when_person_is_older_than_120(self): 144 # try: 145 year_of_birth = self.this_year - 121 146 age = self.calculate_age(year_of_birth) 147 148 with self.assertRaisesRegex( 149 ValueError, 150 f'a {age} year old person is too old to be alive.' 151 ): 152 src.person.Person( 153 first_name='first_name', 154 last_name='last_name', 155 sex='M', 156 # year_of_birth=self.this_year-121, 157 year_of_birth=year_of_birth, 158 ) 159 # except ValueError: 160 # passthe terminal is my friend, and shows AssertionError
AssertionError: "a 121 year old person is too old to be alive." does not match ""I add a message to the Exception in the calculate_age function in
src/person/__init__.py37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError( 40 f"'{year_of_birth}' is not an integer" 41 ) 42 43 age = ( 44 datetime.date.today().year 45 - year_of_birth 46 ) 47 48 if age < 0 or age > 120: 49 raise ValueError( 50 f'a {age} year old person is too old to be alive.' 51 ) 52 return agethe test passes.
I remove the commented lines from test_when_person_is_older_than_120 in
tests/test_person.py143 def test_when_person_is_older_than_120(self): 144 year_of_birth = self.this_year - 121 145 age = self.calculate_age(year_of_birth) 146 147 with self.assertRaisesRegex( 148 ValueError, 149 f'a {age} year old person is too old to be alive.' 150 ): 151 src.person.Person( 152 first_name='first_name', 153 last_name='last_name', 154 sex='M', 155 year_of_birth=year_of_birth, 156 ) 157 158 def test_when_year_of_birth_is_the_future(self):I add a git commit message
use assertRaises with test_when_year_of_birth_is_the_future
RED: make it fail
I use the assertRaisesRegex method to replace the try statement in test_when_year_of_birth_is_the_future
158 def test_when_year_of_birth_is_the_future(self):
159 # try:
160 with self.assertRaisesRegex(
161 ValueError, 'BOOM!!!'
162 ):
163 src.person.Person(
164 first_name='first_name',
165 last_name='last_name',
166 sex='F',
167 year_of_birth=self.this_year+1,
168 )
169 # except ValueError:
170 # pass
171
172 def test_when_year_of_birth_is_not_an_integer(self):
the terminal is my friend, and shows AssertionError
AssertionError:
"BOOM!!!" does not match
"a -1 year old person is too old to be alive."
GREEN: make it pass
I change the Exception to match
158 def test_when_year_of_birth_is_the_future(self):
159 # try:
160 with self.assertRaises(ValueError):
the test passes.
REFACTOR: make it better
I change assertRaises to assertRaisesRegex to test the message of the Exception
151 def test_when_year_of_birth_is_the_future(self): 152 # try: 153 with self.assertRaisesRegex( 154 ValueError, 'BOOM!!!' 155 ): 156 src.person.Person( 157 first_name='first_name', 158 last_name='last_name', 159 sex='F', 160 year_of_birth=self.this_year+1, 161 ) 162 # except ValueError: 163 # pass 164 165 def test_when_year_of_birth_is_not_an_integer(self):the terminal is my friend, and shows AssertionError
AssertionError: "BOOM!!!" does not match "a -1 year old person is too old to be alive."I need a better message for if a person is too old to be alive or too young to be alive.
I change the message of the Exception in the calculate_age function in
src/person/__init__.py37def calculate_age(year_of_birth): 38 if not isinstance(year_of_birth, int): 39 raise TypeError( 40 f"'{year_of_birth}' is not an integer" 41 ) 42 43 age = ( 44 datetime.date.today().year 45 - year_of_birth 46 ) 47 48 if age < 0 or age > 120: 49 raise ValueError( 50 f'a {age} year old person is not alive.' 51 ) 52 return agethe terminal is my friend, and shows AssertionError
FAILED ...test_when_person_is_older_than_120 - AssertionError: "a 121 year old person is too old to be alive." does not match "a 121 year old person is not alive." FAILED ...test_when_year_of_birth_is_the_future - AssertionError: "BOOM!!!" does not match "a -1 year old person is not alive."I change the error message in test_when_person_is_older_than_120 in
tests/test_person.py143 def test_when_person_is_older_than_120(self): 144 year_of_birth = self.this_year - 121 145 age = self.calculate_age(year_of_birth) 146 147 with self.assertRaisesRegex( 148 ValueError, 149 f'a {age} year old person is not alive.' 150 ): 151 src.person.Person( 152 first_name='first_name', 153 last_name='last_name', 154 sex='M', 155 year_of_birth=year_of_birth, 156 ) 157 158 def test_when_year_of_birth_is_the_future(self):the terminal still shows AssertionError
I change the error message in test_when_year_of_birth_is_the_future
158 def test_when_year_of_birth_is_the_future(self): 159 # try: 160 year_of_birth = self.this_year + 1 161 age = self.calculate_age(year_of_birth) 162 163 with self.assertRaisesRegex( 164 ValueError, 165 f'a {age} year old person is not alive.' 166 ): 167 src.person.Person( 168 first_name='first_name', 169 last_name='last_name', 170 sex='F', 171 # year_of_birth=self.this_year+1, 172 year_of_birth=year_of_birth, 173 ) 174 # except ValueError: 175 # passthe test passes.
I remove the commented lines from test_when_year_of_birth_is_the_future
158 def test_when_year_of_birth_is_the_future(self): 159 year_of_birth = self.this_year + 1 160 age = self.calculate_age(year_of_birth) 161 162 with self.assertRaisesRegex( 163 ValueError, 164 f'a {age} year old person is not alive.' 165 ): 166 src.person.Person( 167 first_name='first_name', 168 last_name='last_name', 169 sex='F', 170 year_of_birth=year_of_birth, 171 ) 172 173 def test_when_year_of_birth_is_not_an_integer(self):I add a git commit message
extract test_if_person_is_not_alive
test_when_person_is_older_than_120 and test_when_year_of_birth_is_the_future both check if ValueError is raised if year_of_birth makes the person older than 120 or younger than 0
age = self.calculate_age(year_of_birth)
with self.assertRaisesRegex(
ValueError,
f'a {age} year old person is not alive.'
):
src.person.Person(
first_name='first_name',
last_name='last_name',
sex='M',
year_of_birth=year_of_birth,
)
I can make one function with a for loop to remove the repetition.
RED: make it fail
I add a test with an assertion for if a person is born in the future or is older than 120
125 def test_person_factory(self):
126 for a_person in self.people:
127 year_of_birth = a_person[0]
128
129 with self.subTest(a_person):
130 self.assertEqual(
131 src.person.person(
132 first_name=self.first_name,
133 last_name=self.last_name,
134 sex=self.sex,
135 year_of_birth=year_of_birth,
136 ),
137 (
138 f'{self.first_name}, {self.last_name},'
139 f' {self.sex}, {year_of_birth}'
140 )
141 )
143 def test_if_person_is_not_alive(self):
144 for year_of_birth in (
145 self.this_year+1,
146 self.this_year-121,
147 ):
148 age = self.calculate_age(year_of_birth)
149
150 with self.subTest(year_of_birth):
151 with self.assertRaisesRegex(
152 ValueError,
153 'BOOM!!!'
154 ):
155 src.person.Person(
156 first_name=self.first_name,
157 last_name=self.last_name,
158 sex=self.sex,
159 year_of_birth=year_of_birth,
160 )
161
162 def test_when_person_is_older_than_120(self):
the terminal is my friend, and shows AssertionError
SUBFAILED(year_of_birth=XXXX) ...AssertionError:
"BOOM!!!" does not match "a -1 year old person is not alive."
SUBFAILED(year_of_birth=YYYY) ...AssertionError:
"BOOM!!!" does not match "a 121 year old person is not alive."
GREEN: make it pass
I change the error message in extract test_if_person_is_not_alive to match
143 def test_if_person_is_not_alive(self):
144 for year_of_birth in (
145 self.this_year+1,
146 self.this_year-121,
147 ):
148 age = self.calculate_age(year_of_birth)
149
150 with self.subTest(year_of_birth):
151 with self.assertRaisesRegex(
152 ValueError,
153 f'a {age} year old person is not alive.'
154 ):
155 src.person.Person(
156 first_name=self.first_name,
157 last_name=self.last_name,
158 sex=self.sex,
159 year_of_birth=year_of_birth,
160 )
the test passes.
REFACTOR: make it better
I put the two with statements together
143 def test_if_person_is_not_alive(self): 144 for year_of_birth in ( 145 self.this_year+1, 146 self.this_year-121, 147 ): 148 age = self.calculate_age(year_of_birth) 149 150 with ( 151 self.subTest(year_of_birth), 152 self.assertRaisesRegex( 153 ValueError, 154 f'a {age} year old person is not alive.' 155 ), 156 ): 157 src.person.Person( 158 first_name=self.first_name, 159 last_name=self.last_name, 160 sex=self.sex, 161 year_of_birth=year_of_birth, 162 ) 163 164 def test_when_person_is_older_than_120(self):the test is still green.
I remove test_when_person_is_older_than_120 because it is now covered by test_if_person_is_not_alive
I remove test_when_year_of_birth_is_the_future because it is now covered by test_if_person_is_not_alive
I add a git commit message
move test_when_year_of_birth_is_not_an_integer to test_person_object
test_when_year_of_birth_is_not_an_integer has multiple try statements for if year_of_birth is not an integer. It is also the last tuple in the people object attribute - its year_of_birth values are not integers. I can use the object attributes to remove the repetition
I add tuples for the cases in test_when_year_of_birth_is_not_an_integer to the
peopleobject attribute52 ( 53 # underage, not is_citizen, not passed_test 54 younger_than_18, False, False, 55 False, False, 56 ), 57 # year_of_birth is not an integer 58 (None, False, False,), 59 (2026.0, False, False,), 60 ('2026', False, False,), 61 ((2026,), False, False,), 62 ( 63 'a_string', False, False, 64 False, False, 65 ), 66 ) 67 68 def calculate_age(self, year_of_birth):the tests are still green.
I change the expected Exception in test_person_object to test my change
76 try: 77 person = src.person.Person( 78 first_name=self.first_name, 79 last_name=self.last_name, 80 sex=self.sex, 81 year_of_birth=year_of_birth, 82 is_citizen=a_person[1], 83 passed_test=a_person[2], 84 ) 85 except IndexError as error: 86 self.assertEqual( 87 error.args[0], 88 f"'{year_of_birth}' is not an integer" 89 ) 90 else:the terminal is my friend, and shows TypeError
SUBFAILED[(None, False, False)] ...test_person_object - TypeError: 'None' is not an integer SUBFAILED[(2026.0, False, False)] ...test_person_object - TypeError: '2026.0' is not an integer SUBFAILED[('2026', False, False)] ...test_person_object - TypeError: '2026' is not an integer SUBFAILED[((2026,), False, False)] ...test_person_object - TypeError: '(2026,)' is not an integer SUBFAILED[('a_string', False, False, False, False)] ...test_person_object - TypeError: 'a_string' is not an integergood, the test works.
I undo the change
85 except TypeError as error: 86 self.assertEqual( 87 error.args[0], 88 f"'{year_of_birth}' is not an integer" 89 ) 90 else:the test is green again.
I remove the last tuple from the
peopleobject attribute because it is a repetition of whenyear_of_birthis a string (('2026', False, False,),)I remove test_when_year_of_birth_is_not_an_integer because it is now covered by test_person_object
144 def test_if_person_is_not_alive(self): 145 for year_of_birth in ( 146 self.this_year+1, 147 self.this_year-121, 148 ): 149 age = self.calculate_age(year_of_birth) 150 151 with ( 152 self.subTest(year_of_birth), 153 self.assertRaisesRegex( 154 ValueError, 155 f'a {age} year old person is not alive.' 156 ), 157 ): 158 src.person.Person( 159 first_name=self.first_name, 160 last_name=self.last_name, 161 sex=self.sex, 162 year_of_birth=year_of_birth, 163 ) 164 165 def test_dir_person_object(self):I add a git commit message
close the project
I close
test_person.pyandsrc/person/__init__.pyI use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
person
review
I can use for loops for things that repeat.
My tests still have problems:
The attribute tests - test_dir_person_object and test_dir_person_instance catch changes to the attributes and methods of the Person object and they are a problem to maintain. There has to be a better way.
The
peopleobject attribute is confusing, anyone who reads it without going through the process of making it would not know what the booleans stands for and the last few tuples I added are a different length than the others. There has to be a better way.
code from the chapter
what is next?
I know how to make a Python Test Driven Development environment manually.
I know how to make a Python Test Driven Development environment automatically.
I know how to make a Python Test Driven Development environment automatically with variables.
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.