test AssertionError with loops
I want to use for loops in the assertion_error project.
preview
I have these tests by the end of the chapter
28class TestAssertionError(unittest.TestCase):
29
30 an_integer = 0
31 a_float = 0.0
32 a_string = ''
33 a_tuple = ()
34 a_list = []
35 a_set = set()
36 a_dictionary = {}
38 def test_assert_keyword(self):
39 reality = 1 + 1
40 my_expectation = 2
41 assert_equal(reality, my_expectation)
42 self.assertEqual(reality, my_expectation)
43
44 reality = '1' + '1'
45 my_expectation = '11'
46 assert_equal(reality, my_expectation)
47 self.assertEqual(reality, my_expectation)
48
49 reality = 'I am' + ' alive'
50 my_expectation = 'I am alive'
51 assert_equal(reality, my_expectation)
52 self.assertEqual(reality, my_expectation)
54 def test_assertion_error_w_none(self):
55 assert None is None
56 self.assertIs(None, None)
57
58 assert_is_not_none(False)
59 self.assertIsNot(False, None)
60
61 assert_is_not_none(True)
62 self.assertIsNot(True, None)
63
64 assert_is_not_none(self.an_integer)
65 self.assertIsNot(self.an_integer, None)
66
67 assert_is_not_none(self.a_float)
68 self.assertIsNot(self.a_float, None)
69
70 assert_is_not_none(self.a_string)
71 self.assertIsNot(self.a_string, None)
72
73 assert_is_not_none(self.a_tuple)
74 self.assertIsNot(self.a_tuple, None)
75
76 assert_is_not_none(self.a_list)
77 self.assertIsNot(self.a_list, None)
78
79 assert_is_not_none(self.a_set)
80 self.assertIsNot(self.a_set, None)
81
82 assert_is_not_none(self.a_dictionary)
83 self.assertIsNot(self.a_dictionary, None)
85 def test_assertion_error_w_false(self):
86 assert False is False
87 self.assertIs(False, False)
88
89 assert_is_not_false(None)
90 self.assertIsNot(None, False)
91
92 assert_is_not_false(True)
93 self.assertIsNot(True, False)
94
95 assert_is_not_false(self.an_integer)
96 self.assertIsNot(self.an_integer, False)
97
98 assert_is_not_false(self.a_float)
99 self.assertIsNot(self.a_float, False)
100
101 assert_is_not_false(self.a_string)
102 self.assertIsNot(self.a_string, False)
103
104 assert_is_not_false(self.a_tuple)
105 self.assertIsNot(self.a_tuple, False)
106
107 assert_is_not_false(self.a_list)
108 self.assertIsNot(self.a_list, False)
109
110 assert_is_not_false(self.a_set)
111 self.assertIsNot(self.a_set, False)
112
113 assert_is_not_false(self.a_dictionary)
114 self.assertIsNot(self.a_dictionary, False)
116 def test_assertion_error_w_true(self):
117 assert True is True
118 self.assertIs(True, True)
119
120 assert_is_not_true(None)
121 self.assertIsNot(None, True)
122
123 assert_is_not_true(False)
124 self.assertIsNot(False, True)
125
126 assert_is_not_true(self.an_integer)
127 self.assertIsNot(self.an_integer, True)
128
129 assert_is_not_true(self.a_float)
130 self.assertIsNot(self.a_float, True)
131
132 assert_is_not_true(self.a_string)
133 self.assertIsNot(self.a_string, True)
134
135 assert_is_not_true(self.a_tuple)
136 self.assertIsNot(self.a_tuple, True)
137
138 assert_is_not_true(self.a_list)
139 self.assertIsNot(self.a_list, True)
140
141 assert_is_not_true(self.a_set)
142 self.assertIsNot(self.a_set, True)
143
144 assert_is_not_true(self.a_dictionary)
145 self.assertIsNot(self.a_dictionary, True)
147 def test_assertion_error_w_equality(self):
148 assert_equal(None, None)
149 self.assertEqual(None, None)
150
151 assert_equal(False, False)
152 self.assertEqual(False, False)
153
154 assert_equal(True, True)
155 self.assertEqual(True, True)
156
157 assert_not_equal(False, None)
158 self.assertNotEqual(False, None)
159
160 assert_not_equal(False, True)
161 self.assertNotEqual(False, True)
162
163 assert_not_equal(True, None)
164 self.assertNotEqual(True, None)
166 def test_assertion_error_w_is_vs_equal(self):
167 assert_is_not(0, 0.0)
168 self.assertIsNot(0, 0.0)
169
170 assert_equal(0, 0.0)
171 self.assertEqual(0, 0.0)
173 def will_not_run():
174 # will not run because
175 # the name does not start with test
176 assert False == True
177 self.assertEqual(False, True)
178
179 def test_failure(self):
180 assert_not_equal(False, True)
181 self.assertEqual(False, False)
182
183
184# NOTES
open the project
I open a terminal
I change directory to the assertion_error folder in the
pumping_pythonfoldercd assertion_errorI open
test_assertion_error.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --now
test_assert_keyword with for loop
RED: make it fail
I add a tuple with a for loop for the items in test_assert_keyword
38 def test_assert_keyword(self):
39 for reality, my_expectation in (
40 (1 + 1, 2),
41 ('1' + '1', '11'),
42 ('I am' + ' alive', 'I am alive')
43 ):
44 with self.subTest((reality, my_expectation)):
45 assert_equal(reality, 'BOOM!!!')
46 self.assertEqual(reality, 'BAP!!!')
47
48 reality = 1 + 1
the terminal is my friend, and shows AssertionError
SUBFAILED[(2, 2)] ...test_assert_keyword -
AssertionError: assert 2 == 'BOOM!!!'
SUBFAILED[('11', '11')] ...test_assert_keyword -
AssertionError: assert '11' == 'BOOM!!!'
SUBFAILED[('I am alive', 'I am alive')] ...test_assert_keyword -
AssertionError: assert 'I am alive' == 'BOOM!!!'
GREEN: make it pass
I change the expectation of
assert_equal(reality, 'BOOM!!!')44 with self.subTest((reality, my_expectation)): 45 assert_equal(reality, my_expectation) 46 self.assertEqual(reality, 'BAP!!!') 47 48 reality = 1 + 1the terminal is my friend, and shows AssertionError
SUBFAILED[(2, 2)] ...test_assert_keyword - AssertionError: 2 != 'BAP!!!' SUBFAILED[('11', '11')] ...test_assert_keyword - AssertionError: '11' != 'BAP!!!' SUBFAILED[('I am alive', 'I am alive')] ...test_assert_keyword - AssertionError: 'I am alive' != 'BAP!!!'I change the expectation of
self.assertEqual(reality, 'BAP!!!')44 with self.subTest((reality, my_expectation)): 45 assert_equal(reality, my_expectation) 46 self.assertEqual(reality, my_expectation) 47 48 reality = 1 + 1the test passes.
REFACTOR: make it better
I remove the other statements from test_assert_keyword because they are now covered by the for loop
38 def test_assert_keyword(self): 39 for reality, my_expectation in ( 40 (1 + 1, 2), 41 ('1' + '1', '11'), 42 ('I am' + ' alive', 'I am alive') 43 ): 44 with self.subTest((reality, my_expectation)): 45 assert_equal(reality, my_expectation) 46 self.assertEqual(reality, my_expectation) 47 48 def test_assertion_error_w_none(self):I open a new terminal then make sure I am in the
assertion_errorfoldercd assertion_errorI add a git commit message in the new terminal
git commit -am 'test_assert_keyword with a for loop'
test_assertion_error_w_none with for loop
RED: make it fail
I add a for loop to test_assertion_error_w_none
48 def test_assertion_error_w_none(self):
49 assert None is None
50 self.assertIs(None, None)
51 self.assertIsNone(None)
52
53 for an_object in (
54 False, True, self.an_integer,
55 self.a_float, self.a_string,
56 self.a_tuple, self.a_list,
57 self.a_set, self.a_dictionary,
58 ):
59 with self.subTest(object=an_object):
60 assert_is_not_none(None)
61 self.assertIsNot(an_object, object)
62 self.assertIsNotNone(None)
63
64 assert_is_not_none(False)
the terminal is my friend, and shows AssertionError
AssertionError: assert None is not None
GREEN: make it pass
I change None to
an_objectinassert_is_not_none(None)59 with self.subTest(object=an_object): 60 assert_is_not_none(an_object) 61 self.assertIsNot(an_object, an_object) 62 self.assertIsNotNone(None)the terminal is my friend, and shows AssertionError
SUBFAILED(object=False) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: False SUBFAILED(object=True) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: True SUBFAILED(object=0) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: 0 SUBFAILED(object=0.0) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: 0.0 SUBFAILED(object='') ::test_assertion_error_w_none - AssertionError: unexpectedly identical: '' SUBFAILED(object=()) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: () SUBFAILED(object=[]) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: [] SUBFAILED(object=set()) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: set() SUBFAILED(object={}) ::test_assertion_error_w_none - AssertionError: unexpectedly identical: {}I change the second
an_objectto None inself.assertIsNot(an_object, an_object)59 with self.subTest(object=an_object): 60 assert_is_not_none(an_object) 61 self.assertIsNot(an_object, None) 62 self.assertIsNotNone(None)the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly NoneI change None to
an_object59 with self.subTest(object=an_object): 60 assert_is_not_none(an_object) 61 self.assertIsNot(an_object, None) 62 self.assertIsNotNone(an_object) 63 64 assert_is_not_none(False)the test passes.
REFACTOR: make it better
I remove the other statements from test_assertion_error_w_none because they are now covered by the for loop
48 def test_assertion_error_w_none(self): 49 assert None is None 50 self.assertIs(None, None) 51 self.assertIsNone(None) 52 53 for an_object in ( 54 False, True, self.an_integer, 55 self.a_float, self.a_string, 56 self.a_tuple, self.a_list, 57 self.a_set, self.a_dictionary, 58 ): 59 with self.subTest(object=an_object): 60 assert_is_not_none(an_object) 61 self.assertIsNot(an_object, None) 62 self.assertIsNotNone(an_object) 63 64 def test_assertion_error_w_false(self):I add a git commit message
git commit -am \ 'test_assertion_error_w_none with a for loop'
test_assertion_error_w_false with a for loop
RED: make it fail
I add a for loop to test_assertion_error_w_false
64 def test_assertion_error_w_false(self):
65 assert False is False
66 self.assertIs(False, False)
67
68 for an_object in (
69 None, True, self.an_integer,
70 self.a_float, self.a_string,
71 self.a_tuple, self.a_list,
72 self.a_set, self.a_dictionary,
73 ):
74 with self.subTest(object=an_object):
75 assert_is_not_false(False)
76 self.assertIsNot(an_object, True)
77
78 assert_is_not_false(None)
the terminal is my friend, and shows AssertionError
AssertionError: assert False is not False
GREEN: make it pass
I change False to
an_objectinassert_is_not_false(False)74 with self.subTest(object=an_object): 75 assert_is_not_false(an_object) 76 self.assertIsNot(an_object, True) 77 78 assert_is_not_false(None)the terminal is my friend, and shows AssertionError
SUBFAILED(object=True) test_assertion_error_w_false - AssertionError: unexpectedly identical: TrueI change True to False in
self.assertIsNot(an_object, True)74 with self.subTest(object=an_object): 75 assert_is_not_false(an_object) 76 self.assertIsNot(an_object, False) 77 78 assert_is_not_false(None)the test passes.
REFACTOR: make it better
I remove the other statements from test_assertion_error_w_false because they are now covered by the for loop
64 def test_assertion_error_w_false(self): 65 assert False is False 66 self.assertIs(False, False) 67 68 for an_object in ( 69 None, True, self.an_integer, 70 self.a_float, self.a_string, 71 self.a_tuple, self.a_list, 72 self.a_set, self.a_dictionary, 73 ): 74 with self.subTest(object=an_object): 75 assert_is_not_false(an_object) 76 self.assertIsNot(an_object, False) 77 78 def test_assertion_error_w_true(self):I add a git commit message
git commit -am \ 'test_assertion_error_w_false with a for loop'
test_assertion_error_w_true with a for loop
RED: make it fail
I add a for loop to test_assertion_error_w_true
78 def test_assertion_error_w_true(self):
79 assert True is True
80 self.assertIs(True, True)
81
82 for an_object in (
83 None, False, self.an_integer,
84 self.a_float, self.a_string,
85 self.a_tuple, self.a_list,
86 self.a_set, self.a_dictionary,
87 ):
88 with self.subTest(object=an_object):
89 assert_is_not_true(True)
90 self.assertIsNot(an_object, False)
91
92 assert_is_not_true(None)
the terminal is my friend, and shows AssertionError
AssertionError: assert True is not True
GREEN: make it pass
I change True to
an_objectinassert_is_not_true(True)88 with self.subTest(object=an_object): 89 assert_is_not_true(an_object) 90 self.assertIsNot(an_object, False) 91 92 assert_is_not_true(None)the terminal is my friend, and shows AssertionError
SUBFAILED(object=False) ...test_assertion_error_w_true - AssertionError: unexpectedly identical: FalseI change False to True in
self.assertIsNot(an_object, False)88 with self.subTest(object=an_object): 89 assert_is_not_true(an_object) 90 self.assertIsNot(an_object, True) 91 92 assert_is_not_true(None)the test passes.
REFACTOR: make it better
I remove the other statements from test_assertion_error_w_false because they are now covered by the for loop
78 def test_assertion_error_w_true(self): 79 assert True is True 80 self.assertIs(True, True) 81 82 for an_object in ( 83 None, False, self.an_integer, 84 self.a_float, self.a_string, 85 self.a_tuple, self.a_list, 86 self.a_set, self.a_dictionary, 87 ): 88 with self.subTest(object=an_object): 89 assert_is_not_true(an_object) 90 self.assertIsNot(an_object, True) 91 92 def test_assertion_error_w_equality(self):I add a git commit message
git commit -am \ 'test_assertion_error_w_true w a for loop'
extract basic_objects object attribute
test_assertion_error_w_true, test_assertion_error_w_false and test_assertion_error_w_none all use a tuple with self.an_integer, self.a_float, self.a_string, self.a_tuple, self.a_list, self.a_set and self.a_dictionary.
I can use an object attribute with a single starred expression to remove the repetition.
RED: make it fail
I add an object attribute to the TestAssertionError object
28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 a_string = '' 33 a_tuple = () 34 a_list = [] 35 a_set = set() 36 a_dictionary = {} 37 basic_objects = ( 38 an_integer, a_float, a_string, 39 a_tuple, a_list, a_set, 40 a_dictionary, 41 ) 42 43 def test_assert_keyword(self):I use the new object attribute with a single starred expression in test_assertion_error_w_none
53 def test_assertion_error_w_none(self): 54 assert None is None 55 self.assertIs(None, None) 56 self.assertIsNone(None) 57 58 for an_object in ( 59 False, True, *self.basic_objects 60 ): 61 with self.subTest(object=an_object): 62 assert_is_not_none(an_object) 63 self.assertIsNot(an_object, None) 64 self.assertIsNotNone(an_object) 65 66 def test_assertion_error_w_false(self):the test is still green.
I change
an_objectto None inassert_is_not_none(an_object)to make sure the test still works as expected61 with self.subTest(object=an_object): 62 assert_is_not_none(None) 63 self.assertIsNot(an_object, None) 64 self.assertIsNotNone(an_object)the terminal is my friend, and shows AssertionError
SUBFAILED(object=False) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object=True) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object=0) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object=0.0) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object='') test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object=()) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object=[]) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object=set()) test_assertion_error_w_none - AssertionError: assert None is not None SUBFAILED(object={}) test_assertion_error_w_none - AssertionError: assert None is not Noneit works.
GREEN: make it pass
I undo the change
with self.subTest(object=an_object):
assert_is_not_none(an_object)
self.assertIsNot(an_object, None)
self.assertIsNotNone(an_object)
def test_assertion_error_w_false(self):
the test is green again.
REFACTOR: make it better
I use the new object attribute with a single starred expression in test_assertion_error_w_false
66 def test_assertion_error_w_false(self): 67 assert False is False 68 self.assertIs(False, False) 69 70 for an_object in ( 71 None, True, *self.basic_objects, 72 ): 73 with self.subTest(object=an_object): 74 assert_is_not_false(an_object) 75 self.assertIsNot(an_object, False) 76 77 def test_assertion_error_w_true(self):the test is still green.
I use the new object attribute with a single starred expression in test_assertion_error_w_true
77 def test_assertion_error_w_true(self): 78 assert True is True 79 self.assertIs(True, True) 80 81 for an_object in ( 82 None, False, *self.basic_objects, 83 ): 84 with self.subTest(object=an_object): 85 assert_is_not_true(an_object) 86 self.assertIsNot(an_object, True) 87 88 def test_assertion_error_w_equality(self):still green.
I remove the other object attributes because they are only used by the
basic_objectsobject attribute28class TestAssertionError(unittest.TestCase): 29 30 basic_objects = (0, 0.0, '', (), [], set(), {},) 31 32 def test_assert_keyword(self):green.
I add a git commit message
git commit -am \ 'extract basic_objects object attribute'
test_assertion_error_w_equality with a for loop
RED: make it fail
I add two for loops to test_assertion_error_w_equality
77 def test_assertion_error_w_equality(self): 78 objects = (None, False, True) 79 for first in objects: 80 for second in objects: 81 with self.subTest(pair=(first, second)): 82 self.assertEqual(first, second) 83 84 assert_equal(None, None)the terminal is my friend, and shows AssertionError
SUBFAILED(pair=(None, False)) ...test_assertion_error_w_equality - AssertionError: None != False SUBFAILED(pair=(None, True)) ...test_assertion_error_w_equality - AssertionError: None != True SUBFAILED(pair=(False, None)) ...test_assertion_error_w_equality - AssertionError: False != None SUBFAILED(pair=(False, True)) ...test_assertion_error_w_equality - AssertionError: False != True SUBFAILED(pair=(True, None)) ...test_assertion_error_w_equality - AssertionError: True != None SUBFAILED(pair=(True, False)) ...test_assertion_error_w_equality - AssertionError: True != False
GREEN: make it pass
I add a try statement to handle the cases where the pairings are not equal
77 def test_assertion_error_w_equality(self):
78 objects = (None, False, True)
79 for first in objects:
80 for second in objects:
81 with self.subTest(pair=(first, second)):
82 try:
83 self.assertEqual(first, second)
84 except AssertionError:
85 self.assertNotEqual(first, second)
86
87 assert_equal(None, None)
the test passes.
REFACTOR: make it better
I remove the other statements from test_assertion_error_w_equality because they are now covered by the for loops
77 def test_assertion_error_w_equality(self): 78 objects = (None, False, True) 79 for first in objects: 80 for second in objects: 81 with self.subTest(pair=(first, second)): 82 try: 83 self.assertEqual(first, second) 84 except AssertionError: 85 self.assertNotEqual(first, second) 86 87 def test_assertion_error_w_is_vs_equal(self):I add a git commit message
git commit -am \ 'move test_assertion_error_w_equality to TestAssertionError'
test_assertion_error_w_is_vs_equal with a for loop
RED: make it fail
I move test_assertion_error_w_is_vs_equal to make it a method of TestAssertionError
176 assert_not_equal(True, None) 177 self.assertNotEqual(True, None) 178 179 def test_assertion_error_w_is_vs_equal(): 180 assert_is_not(0, 0.0) 181 assert_equal(0, 0.0) 182 183 184def will_not_run():the terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_assertion_error_w_is_vs_equal() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the definition of test_assertion_error_w_is_vs_equal
176 assert_not_equal(True, None)
177 self.assertNotEqual(True, None)
178
179 # def test_assertion_error_w_is_vs_equal():
180 def test_assertion_error_w_is_vs_equal(self):
181 assert_is_not(0, 0.0)
182 assert_equal(0, 0.0)
the test is green again.
REFACTOR: make it better
I add a call to assertIs for
assert 0 is not 0.0179 # def test_assertion_error_w_is_vs_equal(): 180 def test_assertion_error_w_is_vs_equal(self): 181 assert_is_not(0, 0.0) 182 self.assertIs(0, 0.0) 183 184 assert_equal(0, 0.0)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not 0.0compare the assertions:
assertIs(0, 0.0)vsassert 0 is 0.0.I change assertIs to assertIsNot
179 # def test_assertion_error_w_is_vs_equal(): 180 def test_assertion_error_w_is_vs_equal(self): 181 assert_is_not(0, 0.0) 182 # self.assertIs(0, 0.0) 183 self.assertIsNot(0, 0.0) 184 185 assert_equal(0, 0.0)the test passes. Compare the assertions:
assertIsNot(0, 0.0)vsassert 0 is not 0.0.I add a call to assertNotEqual for
assert 0 == 0.0185 assert_equal(0, 0.0) 186 self.assertNotEqual(0, 0.0) 187 188 189def will_not_run():the terminal is my friend, and shows AssertionError
AssertionError: 0 == 0.0compare the assertions:
assertNotEqual(0, 0.0)vsassert 0 != 0.0.I change assertNotEqual to assertEqual
185 assert_equal(0, 0.0) 186 # self.assertNotEqual(0, 0.0) 187 self.assertEqual(0, 0.0) 188 189 190def will_not_run():the test passes. Compare the assertions:
assertIsNot(0, 0.0)vsassert 0 is not 0.0.I remove the commented lines from test_assertion_error_w_is_vs_equal
176 assert_not_equal(True, None) 177 self.assertNotEqual(True, None) 178 179 def test_assertion_error_w_is_vs_equal(self): 180 assert_is_not(0, 0.0) 181 self.assertIsNot(0, 0.0) 182 183 assert_equal(0, 0.0) 184 self.assertEqual(0, 0.0) 185 186 187def will_not_run():I add a git commit message
git commit -am \ 'move test_assertion_error_w_is_vs_equal to TestAssertionError'
will_not_run with a for loop
RED: make it fail
I move will not run to the TestAssertionError class
183 assert_equal(0, 0.0) 184 self.assertEqual(0, 0.0) 185 186 def will_not_run(): 187 # will not run because 188 # the name does not start with test 189 assert False == True 190 191 192def test_failure():the tests are still green.
I add a call to test_assertEqual
186 def will_not_run(): 187 # will not run because 188 # the name does not start with test 189 assert False == True 190 self.assertEqual(False, True) 191 192 193def test_failure():still green, it does not raise NameError because this method never gets called by pytest.
I add
selfto the parentheses of will not run183 assert_equal(0, 0.0) 184 self.assertEqual(0, 0.0) 185 186 # def will_not_run(): 187 def will_not_run(self): 188 # will not run because 189 # the name does not start with test 190 assert False == True 191 self.assertEqual(False, True)green, it does not raise AssertionError.
I change the name from will not run to
test_will_not_run186 # def will_not_run(): 187 # def will_not_run(self): 188 def test_will_not_run(self): 189 # will not run because 190 # the name does not start with test 191 assert False == True 192 self.assertEqual(False, True)the terminal is my friend, and shows AssertionError
E assert False == Truea reminder that the test only runs if the method/function name starts with test.
GREEN: make it pass
I undo the change
183 assert_equal(0, 0.0)
184 self.assertEqual(0, 0.0)
185
186 def will_not_run():
187 # def will_not_run(self):
188 # def test_will_not_run(self):
189 # will not run because
190 # the name does not start with test
191 assert False == True
192 self.assertEqual(False, True)
193
194
195def test_failure():
the test is green again.
REFACTOR: make it better
I remove the new commented lines from will not run
186 def will_not_run(): 187 # will not run because 188 # the name does not start with test 189 assert False == True 190 self.assertEqual(False, True) 191 192 193def test_failure():I add a git commit message
git commit -am \ 'move will_not_run to TestAssertionError'
test_failure with a for loop
RED: make it fail
I move test_failure to TestAssertionError
186 def will_not_run(): 187 # will not run because 188 # the name does not start with test 189 assert False == True 190 self.assertEqual(False, True) 191 192 def test_failure(): 193 # assert False == True 194 assert_not_equal(False, True) 195 196 197# NOTESthe terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_failure() takes 0 positional arguments but 1 was givenbecause …
GREEN: make it pass
I add self to the parentheses of test_failure in TestAssertionError
186 def will_not_run():
187 # will not run because
188 # the name does not start with test
189 assert False == True
190 self.assertEqual(False, True)
191
192 # def test_failure():
193 def test_failure(self):
194 # assert False == True
195 assert_not_equal(False, True)
the test is green again.
REFACTOR: make it better
I add a call to the assertNotEqual method
192 # def test_failure(): 193 def test_failure(self): 194 # assert False == True 195 assert_not_equal(False, True) 196 self.assertNotEqual(False, False) 197 198 199# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: False == FalseI change assertNotEqual to assertEqual
192 # def test_failure(): 193 def test_failure(self): 194 # assert False == True 195 assert_not_equal(False, True) 196 # self.assertNotEqual(False, False) 197 self.assertEqual(False, False) 198 199 200# NOTESthe test passes.
I remove the commented lines from test_failure
186 def will_not_run(): 187 # will not run because 188 # the name does not start with test 189 assert False == True 190 self.assertEqual(False, True) 191 192 def test_failure(self): 193 assert_not_equal(False, True) 194 self.assertEqual(False, False) 195 196 197# NOTESI add a git commit message
git commit -am \ 'move test_failure to TestAssertionError'
remove repetition with object attributes
Three of the tests have the exact same variables. I can make them object attributes to remove repetition of making the same variables in the tests.
I add an object attribute for
an_integer = 0to TestAssertionError28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 32 def test_assert_keyword(self):I use the object attribute to remove repetition of
an_integer = 0from test_assertion_error_w_none48 def test_assertion_error_w_none(self): 49 assert None is None 50 self.assertIs(None, None) 51 52 assert_is_not_none(False) 53 self.assertIsNot(False, None) 54 55 assert_is_not_none(True) 56 self.assertIsNot(True, None) 57 58 # an_integer = 0 59 # assert_is_not_none(an_integer) 60 # self.assertIsNot(an_integer, None) 61 assert_is_not_none(self.an_integer) 62 self.assertIsNot(self.an_integer, None) 63 64 a_float = 0.0the test is still green.
I use the object attribute to remove repetition of
an_integer = 0from test_assertion_error_w_false84 a_dictionary = {} 85 assert_is_not_none(a_dictionary) 86 self.assertIsNot(a_dictionary, None) 87 88 def test_assertion_error_w_false(self): 89 assert False is False 90 self.assertIs(False, False) 91 92 assert_is_not_false(None) 93 self.assertIsNot(None, False) 94 95 assert_is_not_false(True) 96 self.assertIsNot(True, False) 97 98 # an_integer = 0 99 # assert_is_not_false(an_integer) 100 # self.assertIsNot(an_integer, False) 101 assert_is_not_false(self.an_integer) 102 self.assertIsNot(self.an_integer, False) 103 104 a_float = 0.0still green.
I use the object attribute to remove repetition of
an_integer = 0from test_assertion_error_w_true124 a_dictionary = {} 125 assert_is_not_false(a_dictionary) 126 self.assertIsNot(a_dictionary, False) 127 128 def test_assertion_error_w_true(self): 129 assert True is True 130 self.assertIs(True, True) 131 132 assert_is_not_true(None) 133 self.assertIsNot(None, True) 134 135 assert_is_not_true(False) 136 self.assertIsNot(False, True) 137 138 # an_integer = 0 139 # assert_is_not_true(an_integer) 140 # self.assertIsNot(an_integer, True) 141 assert_is_not_true(self.an_integer) 142 self.assertIsNot(self.an_integer, True) 143 144 a_float = 0.0green.
I add an object attribute for
a_float = 0.028class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 33 def test_assert_keyword(self):I use the object attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_none59 self.assertIsNot(self.an_integer, None) 60 61 # a_float = 0.0 62 # assert_is_not_none(a_float) 63 # self.assertIsNot(a_float, None) 64 assert_is_not_none(self.a_float) 65 self.assertIsNot(self.a_float, None) 66 67 a_string = ''still green.
I use the object attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_false105 self.assertIsNot(self.an_integer, False) 106 107 # a_float = 0.0 108 # assert_is_not_false(a_float) 109 # self.assertIsNot(a_float, False) 110 assert_is_not_false(self.a_float) 111 self.assertIsNot(self.a_float, False) 112 113 a_string = ''the test is still green.
I use the object attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_true147 self.assertIsNot(self.an_integer, True) 148 149 # a_float = 0.0 150 # assert_is_not_true(a_float) 151 # self.assertIsNot(a_float, True) 152 assert_is_not_true(self.a_float) 153 self.assertIsNot(self.a_float, True) 154 155 a_string = ''still green.
I add an object attribute for
a_string = ''28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 a_string = '' 33 34 def test_assert_keyword(self):I use the object attribute to remove repetition of
a_string = ''from test_assertion_error_w_none70 self.assertIsNot(self.a_float, None) 71 72 # a_string = '' 73 # assert_is_not_none(a_string) 74 # self.assertIsNot(a_string, None) 75 assert_is_not_none(self.a_string) 76 self.assertIsNot(self.a_string, None) 77 78 a_tuple = ()green.
I use the object attribute to remove repetition of
a_string = ''from test_assertion_error_w_false114 self.assertIsNot(self.a_float, False) 115 116 # a_string = '' 117 # assert_is_not_false(a_string) 118 # self.assertIsNot(a_string, False) 119 assert_is_not_false(self.a_string) 120 self.assertIsNot(self.a_string, False) 121 122 a_tuple = ()still green.
I use the object attribute to remove repetition of
a_string = ''from test_assertion_error_w_true158 self.assertIsNot(self.a_float, True) 159 160 # a_string = '' 161 # assert_is_not_true(a_string) 162 # self.assertIsNot(a_string, True) 163 assert_is_not_true(self.a_string) 164 self.assertIsNot(self.a_string, True) 165 166 a_tuple = ()the test is still green.
I add an object attribute for
a_tuple = ()28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 a_string = '' 33 a_tuple = () 34 35 def test_assert_keyword(self):I use the object attribute to remove repetition of
a_tuple = ()from test_assertion_error_w_none77 self.assertIsNot(self.a_string, None) 78 79 # a_tuple = () 80 # assert_is_not_none(a_tuple) 81 # self.assertIsNot(a_tuple, None) 82 assert_is_not_none(self.a_tuple) 83 self.assertIsNot(self.a_tuple, None) 84 85 a_list = []still green.
I use the object attribute to remove repetition of
a_tuple = ()from test_assertion_error_w_false123 self.assertIsNot(self.a_string, False) 124 125 # a_tuple = () 126 # assert_is_not_false(a_tuple) 127 # self.assertIsNot(a_tuple, False) 128 assert_is_not_false(self.a_tuple) 129 self.assertIsNot(self.a_tuple, False) 130 131 a_list = []green.
I use the object attribute to remove repetition of
a_tuple = ()from test_assertion_error_w_true169 self.assertIsNot(self.a_string, True) 170 171 # a_tuple = () 172 # assert_is_not_true(a_tuple) 173 # self.assertIsNot(a_tuple, True) 174 assert_is_not_true(self.a_tuple) 175 self.assertIsNot(self.a_tuple, True) 176 177 a_list = []still green.
I add an object attribute for
a_list = []28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 a_string = '' 33 a_tuple = () 34 a_list = [] 35 36 def test_assert_keyword(self):I use the object attribute to remove repetition of
a_list = []from test_assertion_error_w_none84 self.assertIsNot(self.a_tuple, None) 85 86 # a_list = [] 87 # assert_is_not_none(a_list) 88 # self.assertIsNot(a_list, None) 89 assert_is_not_none(self.a_list) 90 self.assertIsNot(self.a_list, None) 91 92 a_set = set()the test is still green.
I use the object attribute to remove repetition of
a_list = []from test_assertion_error_w_false132 self.assertIsNot(self.a_tuple, False) 133 134 # a_list = [] 135 # assert_is_not_false(a_list) 136 # self.assertIsNot(a_list, False) 137 assert_is_not_false(self.a_list) 138 self.assertIsNot(self.a_list, False) 139 140 a_set = set()still green.
I use the object attribute to remove repetition of
a_list = []from test_assertion_error_w_true180 self.assertIsNot(self.a_tuple, True) 181 182 # a_list = [] 183 # assert_is_not_true(a_list) 184 # self.assertIsNot(a_list, True) 185 assert_is_not_true(self.a_list) 186 self.assertIsNot(self.a_list, True) 187 188 a_set = set()green.
I add an object attribute for
a_set = set()28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 a_string = '' 33 a_tuple = () 34 a_list = [] 35 a_set = set() 36 37 def test_assert_keyword(self):I use the object attribute to remove repetition of
a_set = set()from test_assertion_error_w_none91 self.assertIsNot(self.a_list, None) 92 93 # a_set = set() 94 # assert_is_not_none(a_set) 95 # self.assertIsNot(a_set, None) 96 assert_is_not_none(self.a_set) 97 self.assertIsNot(self.a_set, None) 98 99 a_dictionary = {}still green.
I use the object attribute to remove repetition of
a_set = set()from test_assertion_error_w_false141 self.assertIsNot(self.a_list, False) 142 143 # a_set = set() 144 # assert_is_not_false(a_set) 145 # self.assertIsNot(a_set, False) 146 assert_is_not_false(self.a_set) 147 self.assertIsNot(self.a_set, False) 148 149 a_dictionary = {}the test is still green.
I use the object attribute to remove repetition of
a_set = set()from test_assertion_error_w_true191 self.assertIsNot(self.a_list, True) 192 193 # a_set = set() 194 # assert_is_not_true(a_set) 195 # self.assertIsNot(a_set, True) 196 assert_is_not_true(self.a_set) 197 self.assertIsNot(self.a_set, True) 198 199 a_dictionary = {}still green.
I add an object attribute for
a_dictionary = {}28class TestAssertionError(unittest.TestCase): 29 30 an_integer = 0 31 a_float = 0.0 32 a_string = '' 33 a_tuple = () 34 a_list = [] 35 a_set = set() 36 a_dictionary = {} 37 38 def test_assert_keyword(self):I use the object attribute to remove repetition of
a_dictionary = {}from test_assertion_error_w_none98 self.assertIsNot(self.a_set, None) 99 100 # a_dictionary = {} 101 # assert_is_not_none(a_dictionary) 102 # self.assertIsNot(a_dictionary, None) 103 assert_is_not_none(self.a_dictionary) 104 self.assertIsNot(self.a_dictionary, None) 105 106 def test_assertion_error_w_false(self):the test is still green.
I use the object attribute to remove repetition of
a_dictionary = {}from test_assertion_error_w_false150 self.assertIsNot(self.a_set, False) 151 152 # a_dictionary = {} 153 # assert_is_not_false(a_dictionary) 154 # self.assertIsNot(a_dictionary, False) 155 assert_is_not_false(self.a_dictionary) 156 self.assertIsNot(self.a_dictionary, False) 157 158 def test_assertion_error_w_true(self):still green.
I use the object attribute to remove repetition of
a_dictionary = {}from test_assertion_error_w_true202 self.assertIsNot(self.a_set, True) 203 204 # a_dictionary = {} 205 # assert_is_not_true(a_dictionary) 206 # self.assertIsNot(a_dictionary, True) 207 assert_is_not_true(self.a_dictionary) 208 self.assertIsNot(self.a_dictionary, True) 209 210 def test_assertion_error_w_equality(self):green.
I remove the commented lines from test_assertion_error_w_true
158 def test_assertion_error_w_true(self): 159 assert True is True 160 self.assertIs(True, True) 161 162 assert_is_not_true(None) 163 self.assertIsNot(None, True) 164 165 assert_is_not_true(False) 166 self.assertIsNot(False, True)168 assert_is_not_true(self.an_integer) 169 self.assertIsNot(self.an_integer, True) 170 171 assert_is_not_true(self.a_float) 172 self.assertIsNot(self.a_float, True) 173 174 assert_is_not_true(self.a_string) 175 self.assertIsNot(self.a_string, True)177 assert_is_not_true(self.a_tuple) 178 self.assertIsNot(self.a_tuple, True) 179 180 assert_is_not_true(self.a_list) 181 self.assertIsNot(self.a_list, True) 182 183 assert_is_not_true(self.a_set) 184 self.assertIsNot(self.a_set, True) 185 186 assert_is_not_true(self.a_dictionary) 187 self.assertIsNot(self.a_dictionary, True) 188 189 def test_assertion_error_w_equality(self):I remove the commented lines from test_assertion_error_w_false
106 def test_assertion_error_w_false(self): 107 assert False is False 108 self.assertIs(False, False) 109 110 assert_is_not_false(None) 111 self.assertIsNot(None, False) 112 113 assert_is_not_false(True) 114 self.assertIsNot(True, False)116 assert_is_not_false(self.an_integer) 117 self.assertIsNot(self.an_integer, False) 118 119 assert_is_not_false(self.a_float) 120 self.assertIsNot(self.a_float, False) 121 122 assert_is_not_false(self.a_string) 123 self.assertIsNot(self.a_string, False)125 assert_is_not_false(self.a_tuple) 126 self.assertIsNot(self.a_tuple, False) 127 128 assert_is_not_false(self.a_list) 129 self.assertIsNot(self.a_list, False) 130 131 assert_is_not_false(self.a_set) 132 self.assertIsNot(self.a_set, False) 133 134 assert_is_not_false(self.a_dictionary) 135 self.assertIsNot(self.a_dictionary, False) 136 137 def test_assertion_error_w_true(self):I remove the commented lines from test_assertion_error_w_none
54 def test_assertion_error_w_none(self): 55 assert None is None 56 self.assertIs(None, None) 57 58 assert_is_not_none(False) 59 self.assertIsNot(False, None) 60 61 assert_is_not_none(True) 62 self.assertIsNot(True, None)64 assert_is_not_none(self.an_integer) 65 self.assertIsNot(self.an_integer, None) 66 67 assert_is_not_none(self.a_float) 68 self.assertIsNot(self.a_float, None) 69 70 assert_is_not_none(self.a_string) 71 self.assertIsNot(self.a_string, None)73 assert_is_not_none(self.a_tuple) 74 self.assertIsNot(self.a_tuple, None) 75 76 assert_is_not_none(self.a_list) 77 self.assertIsNot(self.a_list, None) 78 79 assert_is_not_none(self.a_set) 80 self.assertIsNot(self.a_set, None) 81 82 assert_is_not_none(self.a_dictionary) 83 self.assertIsNot(self.a_dictionary, None) 84 85 def test_assertion_error_w_false(self):I add a git commit message
git commit -am 'extract object attributes'
I can use object attributes to remove repetition. I make them once and other things in the object can reference them.
close the project
I close
test_assertion_error.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
assertion_errorcd ..the terminal is my friend, and shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I can use the unittest library to write tests with the methods of the unittest.TestCase class or I can write them with bare assert statements.
I can use object attributes for things that repeat, which allows methods of the same object to use them.
code from the chapter
Do you want to see all the CODE I typed in this chapter?
what is next?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.