test AssertionError with unittest
I want to use the unittest library 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 . --nowthe terminal is my friend, and shows
tests/test_assertion_error.py ....... [100%] ================== 7 passed in A.BCs ===================
add TestAssertionError class
RED: make it fail
I add an object named
AssertionErrorwith a method for the first failing test totest_assertion_error.py21def assert_equal(x, y): 22 assert x == y 23 24 25class AssertionError(object): 26 27 def test_failure(self): 28 self.assertEqual(True, False) 29 30 31def test_assert_keyword():the test is still green.
I change the name of the object to
TestAssertionError21def assert_equal(x, y): 22 assert x == y 23 24 25# class AssertionError(object): 26class TestAssertionError(object): 27 28 def test_failure(self):the terminal is my friend, and shows AttributeError
AttributeError: 'TestAssertionError' object has no attribute 'assertEqual'I add AttributeError to the list of Exceptions seen
142# None is None and equal to None 143 144 145# Exceptions seen 146# AssertionError 147# IndentationError 148# AttributeError
GREEN: make it pass
I add unittest.TestCase as the parent object of
TestAssertionError21def assert_equal(x, y): 22 assert x == y 23 24 25# class AssertionError(object): 26# class TestAssertionError(object): 27class TestAssertionError(unittest.TestCase): 28 29 def test_failure(self):the terminal is my friend, and shows NameError
NameError: name 'unittest' is not defined. Did you forget to import 'unittest'?I add NameError to the list of Exceptions seen
146# Exceptions seen 147# AssertionError 148# IndentationError 149# AttributeError 150# NameErrorI add an import statement at the top of the file
1import unittest 2 3 4def assert_is_not(x, y):the terminal is my friend, and shows AssertionError
AssertionError: True != FalseI change False to True in the assertion in test_failure in the TestAssertionError class
28# class AssertionError(object): 29# class TestAssertionError(object): 30class TestAssertionError(unittest.TestCase): 31 32 def test_failure(self): 33 # self.assertEqual(True, False) 34 self.assertEqual(True, True) 35 36 37def test_assert_keyword():the test passes.
REFACTOR: make it better
I remove the commented lines from TestAssertionError
24def assert_equal(x, y): 25 assert x == y 26 27 28class TestAssertionError(unittest.TestCase): 29 30 def test_failure(self): 31 self.assertEqual(True, True) 32 33 34def test_assert_keyword():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 \ 'add TestAssertionError class'
test_assert_keyword with unittest
RED: make it fail
I go back to the terminal where the tests are running
I remove test_failure
I move test_assert_keyword to make it a method of the TestAssertionError class
28class TestAssertionError(unittest.TestCase): 29 30 def test_assert_keyword(): 31 assert_equal(1+1, 2) 32 assert_equal('1'+'1', '11') 33 assert_equal('I am'+' alive', 'I am alive') 34 35 36def test_assertion_error_w_none():the terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_assert_keyword() 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.I add TypeError to the list of Exceptions seen
143# Exceptions seen 144# AssertionError 145# IndentationError 146# AttributeError 147# NameError 148# TypeError
GREEN: make it pass
I add self to the parentheses of test_assert_keyword
28class TestAssertionError(unittest.TestCase):
29
30 # def test_assert_keyword():
31 def test_assert_keyword(self):
32 assert_equal(1+1, 2)
the test is green again.
REFACTOR: make it better
I add calls to the assertNotEqual method for the three assertions
28 # def test_assert_keyword(): 29 def test_assert_keyword(self): 30 assert_equal(1+1, 2) 31 self.assertNotEqual(1+1, 2) 32 33 assert_equal('1'+'1', '11') 34 self.assertNotEqual('1'+'1', '11') 35 36 assert_equal('I am'+' alive', 'I am alive') 37 self.assertNotEqual('I am'+' alive', 'I am alive') 38 39 40def test_assertion_error_w_none():the terminal is my friend, and shows AssertionError
AssertionError: 2 == 2compare this with the error message for
assert 1 + 1 == 11E assert (1 + 1) == 11I change assertNotEqual to assertEqual for
(1+1, 2)30 # def test_assert_keyword(): 31 def test_assert_keyword(self): 32 assert_equal(1+1, 2) 33 # self.assertNotEqual(1+1, 2) 34 self.assertEqual(1+1, 2)the terminal is my friend, and shows AssertionError
AssertionError: '11' == '11'I change assertNotEqual to assertEqual for
('1'+'1', '11')30 # def test_assert_keyword(): 31 def test_assert_keyword(self): 32 assert_equal(1+1, 2) 33 # self.assertNotEqual(1+1, 2) 34 self.assertEqual(1+1, 2) 35 36 assert_equal('1'+'1', '11') 37 # self.assertNotEqual('1'+'1', '11') 38 self.assertEqual('1'+'1', '11') 39 40 assert_equal('I am'+' alive', 'I am alive')the terminal is my friend, and shows AssertionError
AssertionError: 'I am alive' == 'I am alive'I change assertNotEqual to assertEqual for
('I am'+' alive', 'I am alive')40 assert_equal('I am'+' alive', 'I am alive') 41 # self.assertNotEqual('I am'+' alive', 'I am alive') 42 self.assertEqual('I am'+' alive', 'I am alive') 43 44 45def test_assertion_error_w_none():the test passes.
I add variables for
'I am' + ' alive'and'I am alive'38 self.assertEqual('1'+'1', '11') 39 40 reality = 'I am' + ' alive' 41 my_expectation = 'I am alive' 42 assert_equal('I am'+' alive', 'I am alive') 43 # self.assertNotEqual('I am'+' alive', 'I am alive') 44 self.assertEqual('I am'+' alive', 'I am alive') 45 46 47def test_assertion_error_w_none():I use the variables to remove repetition of
'I am' + ' alive'and'I am alive'40 reality = 'I am' + ' alive' 41 my_expectation = 'I am alive' 42 # assert_equal('I am'+' alive', 'I am alive') 43 assert_equal(reality, my_expectation) 44 # self.assertNotEqual('I am'+' alive', 'I am alive') 45 # self.assertEqual('I am'+' alive', 'I am alive') 46 self.assertEqual(reality, my_expectation) 47 48 49def test_assertion_error_w_none():the test is still green.
I add variables for
'1' + '1'and'11'30 # def test_assert_keyword(): 31 def test_assert_keyword(self): 32 assert_equal(1+1, 2) 33 # self.assertNotEqual(1+1, 2) 34 self.assertEqual(1+1, 2) 35 36 reality = '1' + '1' 37 my_expectation = '11' 38 assert_equal('1'+'1', '11') 39 # self.assertNotEqual('1'+'1', '11') 40 self.assertEqual('1'+'1', '11')I use the variables to remove repetition of
'1' + '1'and'11'36 reality = '1' + '1' 37 my_expectation = '11' 38 # assert_equal('1'+'1', '11') 39 assert_equal(reality, my_expectation) 40 # self.assertNotEqual('1'+'1', '11') 41 # self.assertEqual('1'+'1', '11') 42 self.assertEqual(reality, my_expectation) 43 44 reality = 'I am' + ' alive'still green.
I add variables for
1 + 1and230 # def test_assert_keyword(): 31 def test_assert_keyword(self): 32 reality = 1 + 1 33 my_expectation = 2 34 assert_equal(1+1, 2) 35 # self.assertNotEqual(1+1, 2) 36 self.assertEqual(1+1, 2)I use the variables to remove repetition of
1 + 1and230 # def test_assert_keyword(): 31 def test_assert_keyword(self): 32 reality = 1 + 1 33 my_expectation = 2 34 # assert_equal(1+1, 2) 35 assert_equal(reality, my_expectation) 36 # self.assertNotEqual(1+1, 2) 37 # self.assertEqual(1+1, 2) 38 self.assertEqual(reality, my_expectation) 39 40 reality = '1' + '1'green.
I remove the commented lines from test_assert_keyword
28class TestAssertionError(unittest.TestCase): 29 30 def test_assert_keyword(self): 31 reality = 1 + 1 32 my_expectation = 2 33 assert_equal(reality, my_expectation) 34 self.assertEqual(reality, my_expectation) 35 36 reality = '1' + '1' 37 my_expectation = '11' 38 assert_equal(reality, my_expectation) 39 self.assertEqual(reality, my_expectation) 40 41 reality = 'I am' + ' alive' 42 my_expectation = 'I am alive' 43 assert_equal(reality, my_expectation) 44 self.assertEqual(reality, my_expectation) 45 46 47def test_assertion_error_w_none():I add a git commit message in the other terminal
git commit -am \ 'move test_assert_keyword to TestAssertionError'
test_assertion_error_w_none with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_assertion_error_w_none to make it a method of TestAssertionError
Caution
Indentation matters in Python. It is how it knows what blocks belong to what function/method, object or module (Use 4 spaces)
41 reality = 'I am' + ' alive' 42 my_expectation = 'I am alive' 43 assert_equal(reality, my_expectation) 44 self.assertEqual(reality, my_expectation) 45 46 def test_assertion_error_w_none(): 47 assert None is None 48 49 assert_is_not_none(False) 50 assert_is_not_none(True) 51 assert_is_not_none(0) 52 assert_is_not_none(0.0) 53 assert_is_not_none('') 54 assert_is_not_none(()) 55 assert_is_not_none([]) 56 assert_is_not_none(set()) 57 assert_is_not_none({}) 58 59 60def test_assertion_error_w_false():the terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_assertion_error_w_none() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_assertion_error_w_none
41 reality = 'I am' + ' alive'
42 my_expectation = 'I am alive'
43 assert_equal(reality, my_expectation)
44 self.assertEqual(reality, my_expectation)
45
46 # def test_assertion_error_w_none():
47 def test_assertion_error_w_none(self):
48 assert None is None
the test is green again.
REFACTOR: make it better
I add calls to the assertIsNot and assertIs methods for the assertions in test_assertion_error_w_none
46 # def test_assertion_error_w_none(): 47 def test_assertion_error_w_none(self): 48 assert None is None 49 self.assertIsNot(None, None) 50 51 assert_is_not_none(False) 52 self.assertIs(False, None) 53 54 assert_is_not_none(True) 55 self.assertIs(True, None)57 assert_is_not_none(0) 58 self.assertIs(0, None) 59 60 assert_is_not_none(0.0) 61 self.assertIs(0.0, None) 62 63 assert_is_not_none('') 64 self.assertIs('', None) 65 66 assert_is_not_none(()) 67 self.assertIs((), None)69 assert_is_not_none([]) 70 self.assertIs([], None) 71 72 assert_is_not_none(set()) 73 self.assertIs(set(), None) 74 75 assert_is_not_none({}) 76 self.assertIs({}, None) 77 78 79def test_assertion_error_w_false():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: Nonecompare this with the error message for
assert None is not NoneE assert None is not NoneI change assertIsNot to assertIs for
(None, None)46 # def test_assertion_error_w_none(): 47 def test_assertion_error_w_none(self): 48 assert None is None 49 # self.assertIsNot(None, None) 50 self.assertIs(None, None) 51 52 assert_is_not_none(False)the terminal is my friend, and shows AssertionError
AssertionError: False is not Nonecompare this with the error message for
assert False is NoneE assert False is NoneI change assertIs to assertIsNot for
(False, None)52 assert_is_not_none(False) 53 # self.assertIs(False, None) 54 self.assertIsNot(False, None) 55 56 assert_is_not_none(True)the terminal is my friend, and shows AssertionError
AssertionError: True is not Nonecompare this with the error message for
assert True is NoneE assert True is NoneI change assertIs to assertIsNot for
(True, None)56 assert_is_not_none(True) 57 # self.assertIs(True, None) 58 self.assertIsNot(True, None) 59 60 assert_is_not_none(0)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not NoneI change assertIs to assertIsNot for
(0, None)60 assert_is_not_none(0) 61 # self.assertIs(0, None) 62 self.assertIsNot(0, None) 63 64 assert_is_not_none(0.0)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not NoneI change assertIs to assertIsNot for
(0.0, None)64 assert_is_not_none(0.0) 65 # self.assertIs(0.0, None) 66 self.assertIsNot(0.0, None) 67 68 assert_is_not_none('')the terminal is my friend, and shows AssertionError
AssertionError: '' is not NoneI change assertIs to assertIsNot for
('', None)68 assert_is_not_none('') 69 # self.assertIs('', None) 70 self.assertIsNot('', None) 71 72 assert_is_not_none(())the terminal is my friend, and shows AssertionError
AssertionError: () is not NoneI change assertIs to assertIsNot for
((), None)72 assert_is_not_none(()) 73 # self.assertIs((), None) 74 self.assertIsNot((), None) 75 76 assert_is_not_none([])the terminal is my friend, and shows AssertionError
AssertionError: [] is not NoneI change assertIs to assertIsNot for
([], None)76 assert_is_not_none([]) 77 # self.assertIs([], None) 78 self.assertIsNot([], None) 79 80 assert_is_not_none(set())the terminal is my friend, and shows AssertionError
AssertionError: set() is not NoneI change assertIs to assertIsNot for
(set(), None)80 assert_is_not_none(set()) 81 # self.assertIs(set(), None) 82 self.assertIsNot(set(), None) 83 84 assert_is_not_none({})the terminal is my friend, and shows AssertionError
AssertionError: {} is not NoneI change assertIs to assertIsNot for
({}, None)84 assert_is_not_none({}) 85 # self.assertIs({}, None) 86 self.assertIsNot({}, None) 87 88 89def test_assertion_error_w_false():the test passes.
I add a variable for
{}80 assert_is_not_none(set()) 81 # self.assertIs(set(), None) 82 self.assertIsNot(set(), None) 83 84 a_dictionary = {} 85 assert_is_not_none({}) 86 # self.assertIs({}, None) 87 self.assertIsNot({}, None) 88 89 90def test_assertion_error_w_false():I use the variable to remove repetition of
{}84 a_dictionary = {} 85 # assert_is_not_none({}) 86 assert_is_not_none(a_dictionary) 87 # self.assertIs({}, None) 88 # self.assertIsNot({}, None) 89 self.assertIsNot(a_dictionary, None) 90 91 92def test_assertion_error_w_false():the test is still green.
I add a variable for
set()76 assert_is_not_none([]) 77 # self.assertIs([], None) 78 self.assertIsNot([], None) 79 80 a_set = set() 81 assert_is_not_none(set()) 82 # self.assertIs(set(), None) 83 self.assertIsNot(set(), None) 84 85 a_dictionary = {}I use the variable to remove repetition of
set()80 a_set = set() 81 # assert_is_not_none(set()) 82 assert_is_not_none(a_set) 83 # self.assertIs(set(), None) 84 # self.assertIsNot(set(), None) 85 self.assertIsNot(a_set, None) 86 87 a_dictionary = {}still green.
I add a variable for
[]72 assert_is_not_none(()) 73 # self.assertIs((), None) 74 self.assertIsNot((), None) 75 76 a_list = [] 77 assert_is_not_none([]) 78 # self.assertIs([], None) 79 self.assertIsNot([], None)I use the variable to remove repetition of
[]76 a_list = [] 77 # assert_is_not_none([]) 78 assert_is_not_none(a_list) 79 # self.assertIs([], None) 80 # self.assertIsNot([], None) 81 self.assertIsNot(a_list, None) 82 83 a_set = set()green.
I add a variable for
()68 assert_is_not_none('') 69 # self.assertIs('', None) 70 self.assertIsNot('', None) 71 72 a_tuple = () 73 assert_is_not_none(()) 74 # self.assertIs((), None) 75 self.assertIsNot((), None)I use the variable to remove repetition of
()72 a_tuple = () 73 # assert_is_not_none(()) 74 assert_is_not_none(a_tuple) 75 # self.assertIs((), None) 76 # self.assertIsNot((), None) 77 self.assertIsNot(a_tuple, None) 78 79 a_list = []still green.
I add a variable for
''64 assert_is_not_none(0.0) 65 # self.assertIs(0.0, None) 66 self.assertIsNot(0.0, None) 67 68 a_string = '' 69 assert_is_not_none('') 70 # self.assertIs('', None) 71 self.assertIsNot('', None)I use the variable to remove repetition of
''68 a_string = '' 69 # assert_is_not_none('') 70 assert_is_not_none(a_string) 71 # self.assertIs('', None) 72 # self.assertIsNot('', None) 73 self.assertIsNot(a_string, None) 74 75 a_tuple = ()the test is still green.
I add a variable for
0.060 assert_is_not_none(0) 61 # self.assertIs(0, None) 62 self.assertIsNot(0, None) 63 64 a_float = 0.0 65 assert_is_not_none(0.0) 66 # self.assertIs(0.0, None) 67 self.assertIsNot(0.0, None)I use the variable to remove repetition of
0.064 a_float = 0.0 65 # assert_is_not_none(0.0) 66 assert_is_not_none(a_float) 67 # self.assertIs(0.0, None) 68 # self.assertIsNot(0.0, None) 69 self.assertIsNot(a_float, None) 70 71 a_string = ''still green.
I add a variable for
056 assert_is_not_none(True) 57 # self.assertIs(True, None) 58 self.assertIsNot(True, None) 59 60 an_integer = 0 61 assert_is_not_none(0) 62 # self.assertIs(0, None) 63 self.assertIsNot(0, None)I use the variable to remove repetition of
060 an_integer = 0 61 # assert_is_not_none(0) 62 assert_is_not_none(an_integer) 63 # self.assertIs(0, None) 64 # self.assertIsNot(0, None) 65 self.assertIsNot(an_integer, None) 66 67 a_float = 0.0green.
I remove the commented lines from test_assertion_error_w_none
41 reality = 'I am' + ' alive' 42 my_expectation = 'I am alive' 43 assert_equal(reality, my_expectation) 44 self.assertEqual(reality, my_expectation) 45 46 def test_assertion_error_w_none(self): 47 assert None is None 48 self.assertIs(None, None) 49 50 assert_is_not_none(False) 51 self.assertIsNot(False, None) 52 53 assert_is_not_none(True) 54 self.assertIsNot(True, None)56 an_integer = 0 57 assert_is_not_none(an_integer) 58 self.assertIsNot(an_integer, None) 59 60 a_float = 0.0 61 assert_is_not_none(a_float) 62 self.assertIsNot(a_float, None) 63 64 a_string = '' 65 assert_is_not_none(a_string) 66 self.assertIsNot(a_string, None)68 a_tuple = () 69 assert_is_not_none(a_tuple) 70 self.assertIsNot(a_tuple, None) 71 72 a_list = [] 73 assert_is_not_none(a_list) 74 self.assertIsNot(a_list, None) 75 76 a_set = set() 77 assert_is_not_none(a_set) 78 self.assertIsNot(a_set, None) 79 80 a_dictionary = {} 81 assert_is_not_none(a_dictionary) 82 self.assertIsNot(a_dictionary, None) 83 84 85def test_assertion_error_w_false():I add a git commit message in the other terminal
git commit -am \ 'move test_assertion_error_w_none to TestAssertionError'
test_assertion_error_w_false with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_assertion_error_w_false to make it a method of TestAssertionError
80 a_dictionary = {} 81 assert_is_not_none(a_dictionary) 82 self.assertIsNot(a_dictionary, None) 83 84 def test_assertion_error_w_false(): 85 assert False is False 86 87 assert_is_not_false(None) 88 assert_is_not_false(True) 89 assert_is_not_false(0) 90 assert_is_not_false(0.0) 91 assert_is_not_false('') 92 assert_is_not_false(()) 93 assert_is_not_false([]) 94 assert_is_not_false(set()) 95 assert_is_not_false({}) 96 97 98def test_assertion_error_w_true():the terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_assertion_error_w_false() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_assertion_error_w_false
80 a_dictionary = {}
81 assert_is_not_none(a_dictionary)
82 self.assertIsNot(a_dictionary, None)
83
84 # def test_assertion_error_w_false():
85 def test_assertion_error_w_false(self):
86 assert False is False
the test is green again.
REFACTOR: make it better
I add calls to assertIsNot and assertIs to test_assertion_error_w_false
84 # def test_assertion_error_w_false(): 85 def test_assertion_error_w_false(self): 86 assert False is False 87 self.assertIsNot(False, False) 88 89 assert_is_not_false(None) 90 self.assertIs(None, False) 91 92 assert_is_not_false(True) 93 self.assertIs(True, False)95 assert_is_not_false(0) 96 self.assertIs(0, False) 97 98 assert_is_not_false(0.0) 99 self.assertIs(0.0, False) 100 101 assert_is_not_false('') 102 self.assertIs('', False) 103 104 assert_is_not_false(()) 105 self.assertIs((), False)104 assert_is_not_false([]) 105 self.assertIs([], False) 106 107 assert_is_not_false(set()) 108 self.assertIs(set(), False) 109 110 assert_is_not_false({}) 111 self.assertIs({}, False) 112 113 114def test_assertion_error_w_true():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: Falsecompare this with the error message for
assert False is not FalseE assert False is not FalseI change assertIsNot to assertIs for
(False, False)84 # def test_assertion_error_w_false(): 85 def test_assertion_error_w_false(self): 86 assert False is False 87 # self.assertIsNot(False, False) 88 self.assertIs(False, False) 89 90 assert_is_not_false(None)the terminal is my friend, and shows AssertionError
AssertionError: None is not FalseI change assertIs to assertIsNot for
(None, False)90 assert_is_not_false(None) 91 # self.assertIs(None, False) 92 self.assertIsNot(None, False) 93 94 assert_is_not_false(True)the terminal is my friend, and shows AssertionError
AssertionError: True is not FalseI change assertIs to assertIsNot for
(True, False)94 assert_is_not_false(True) 95 # self.assertIs(True, False) 96 self.assertIsNot(True, False) 97 98 assert_is_not_false(0)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not FalseI change assertIs to assertIsNot for
(0, False)98 assert_is_not_false(0) 99 # self.assertIs(0, False) 100 self.assertIsNot(0, False) 101 102 assert_is_not_false(0.0)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not FalseI change assertIs to assertIsNot for
(0.0, False)102 assert_is_not_false(0.0) 103 # self.assertIs(0.0, False) 104 self.assertIsNot(0.0, False) 105 106 assert_is_not_false('')the terminal is my friend, and shows AssertionError
AssertionError: '' is not FalseI change assertIs to assertIsNot for
('', False)106 assert_is_not_false('') 107 # self.assertIs('', False) 108 self.assertIsNot('', False) 109 110 assert_is_not_false(())the terminal is my friend, and shows AssertionError
AssertionError: () is not FalseI change assertIs to assertIsNot for
((), False)110 assert_is_not_false(()) 111 # self.assertIs((), False) 112 self.assertIsNot((), False) 113 114 assert_is_not_false([])the terminal is my friend, and shows AssertionError
AssertionError: [] is not FalseI change assertIs to assertIsNot for
([], False)114 assert_is_not_false([]) 115 # self.assertIs([], False) 116 self.assertIsNot([], False) 117 118 assert_is_not_false(set())the terminal is my friend, and shows AssertionError
AssertionError: set() is not FalseI change assertIs to assertIsNot for
(set(), False)118 assert set() is not False 119 # self.assertIs(set(), False) 120 self.assertIsNot(set(), False) 121 122 assert {} is not Falsethe terminal is my friend, and shows AssertionError
AssertionError: {} is not FalseI change assertIs to assertIsNot for
({}, False)122 assert_is_not_false({}) 123 # self.assertIs({}, False) 124 self.assertIsNot({}, False) 125 126 127def test_assertion_error_w_true():the test passes.
I add a variable for
{}118 assert_is_not_false(set()) 119 # self.assertIs(set(), False) 120 self.assertIsNot(set(), False) 121 122 a_dictionary = {} 123 assert_is_not_false({}) 124 # self.assertIs({}, False) 125 self.assertIsNot({}, False)I use the variable to remove repetition of
{}122 a_dictionary = {} 123 # assert_is_not_false({}) 124 assert_is_not_false(a_dictionary) 125 # self.assertIs({}, False) 126 # self.assertIsNot({}, False) 127 self.assertIsNot(a_dictionary, False) 128 129 130def test_assertion_error_w_true():the test is still green.
I add a variable for
set()114 assert_is_not_false([]) 115 # self.assertIs([], False) 116 self.assertIsNot([], False) 117 118 a_set = set() 119 assert_is_not_false(set()) 120 # self.assertIs(set(), False) 121 self.assertIsNot(set(), False)I use the variable to remove repetition of
set()118 a_set = set() 119 # assert_is_not_false(set()) 120 assert_is_not_false(a_set) 121 # self.assertIs(set(), False) 122 # self.assertIsNot(set(), False) 123 self.assertIsNot(a_set, False) 124 125 a_dictionary = {}still green.
I add a variable for
[]110 assert_is_not_false(()) 111 # self.assertIs((), False) 112 self.assertIsNot((), False) 113 114 a_list = [] 115 assert_is_not_false([]) 116 # self.assertIs([], False) 117 self.assertIsNot([], False)I use the variable to remove repetition of
[]114 a_list = [] 115 # assert_is_not_false([]) 116 assert_is_not_false(a_list) 117 # self.assertIs([], False) 118 # self.assertIsNot([], False) 119 self.assertIsNot(a_list, False) 120 121 a_set = set()green.
I add a variable for
()106 assert_is_not_false('') 107 # self.assertIs('', False) 108 self.assertIsNot('', False) 109 110 a_tuple = () 111 assert_is_not_false(()) 112 # self.assertIs((), False) 113 self.assertIsNot((), False)I use the variable to remove repetition of
()110 a_tuple = () 111 # assert_is_not_false(()) 112 assert_is_not_false(a_tuple) 113 # self.assertIs((), False) 114 # self.assertIsNot((), False) 115 self.assertIsNot(a_tuple, False) 116 117 a_list = []still green.
I add a variable for
''102 assert_is_not_false(0.0) 103 # self.assertIs(0.0, False) 104 self.assertIsNot(0.0, False) 105 106 a_string = '' 107 assert_is_not_false('') 108 # self.assertIs('', False) 109 self.assertIsNot('', False)I use the variable to remove repetition of
''106 a_string = '' 107 # assert_is_not_false('') 108 assert_is_not_false(a_string) 109 # self.assertIs('', False) 110 # self.assertIsNot('', False) 111 self.assertIsNot(a_string, False) 112 113 a_tuple = ()the test is still green.
I add a variable for
0.098 assert_is_not_false(0) 99 # self.assertIs(0, False) 100 self.assertIsNot(0, False) 101 102 a_float = 0.0 103 assert_is_not_false(0.0) 104 # self.assertIs(0.0, False) 105 self.assertIsNot(0.0, False)I use the variable to remove repetition of
0.0102 a_float = 0.0 103 # assert_is_not_false(0.0) 104 assert_is_not_false(a_float) 105 # self.assertIs(0.0, False) 106 # self.assertIsNot(0.0, False) 107 self.assertIsNot(a_float, False) 108 109 a_string = ''still green.
I add a variable for
094 assert_is_not_false(True) 95 # self.assertIs(True, False) 96 self.assertIsNot(True, False) 97 98 an_integer = 0 99 assert_is_not_false(0) 100 # self.assertIs(0, False) 101 self.assertIsNot(0, False)I use the variable to remove repetition of
098 an_integer = 0 99 # assert_is_not_false(0) 100 assert_is_not_false(an_integer) 101 # self.assertIs(0, False) 102 # self.assertIsNot(0, False) 103 self.assertIsNot(an_integer, False) 104 105 a_float = 0.0green.
I remove the commented lines from test_assertion_error_w_false
80 a_dictionary = {} 81 assert_is_not_none(a_dictionary) 82 self.assertIsNot(a_dictionary, None) 83 84 def test_assertion_error_w_false(self): 85 assert False is False 86 self.assertIs(False, False) 87 88 assert_is_not_false(None) 89 self.assertIsNot(None, False) 90 91 assert_is_not_false(True) 92 self.assertIsNot(True, False)94 an_integer = 0 95 assert_is_not_false(an_integer) 96 self.assertIsNot(an_integer, False) 97 98 a_float = 0.0 99 assert_is_not_false(a_float) 100 self.assertIsNot(a_float, False) 101 102 a_string = '' 103 assert_is_not_false(a_string) 104 self.assertIsNot(a_string, False)106 a_tuple = () 107 assert_is_not_false(a_tuple) 108 self.assertIsNot(a_tuple, False) 109 110 a_list = [] 111 assert_is_not_false(a_list) 112 self.assertIsNot(a_list, False) 113 114 a_set = set() 115 assert_is_not_false(a_set) 116 self.assertIsNot(a_set, False) 117 118 a_dictionary = {} 119 assert_is_not_false(a_dictionary) 120 self.assertIsNot(a_dictionary, False) 121 122 123def test_assertion_error_w_true():I add a git commit message in the other terminal
git commit -am \ 'move test_assertion_error_w_false to TestAssertionError'
test_assertion_error_w_true with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_assertion_error_w_true to make it a method of the TestAssertionError class
118 a_dictionary = {} 119 assert_is_not_false(a_dictionary) 120 self.assertIsNot(a_dictionary, False) 121 122 def test_assertion_error_w_true(): 123 assert True is True 124 125 assert_is_not_true(None) 126 assert_is_not_true(False) 127 assert_is_not_true(0) 128 assert_is_not_true(0.0) 129 assert_is_not_true('') 130 assert_is_not_true(()) 131 assert_is_not_true([]) 132 assert_is_not_true(set()) 133 assert_is_not_true({}) 134 135 136def test_assertion_error_w_equality():the terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_assertion_error_w_true() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) it belongs to as the first argument.
GREEN: make it pass
I add self to the parentheses of test_assertion_error_w_true
118 a_dictionary = {}
119 assert_is_not_false(a_dictionary)
120 self.assertIsNot(a_dictionary, False)
121
122 # def test_assertion_error_w_true():
123 def test_assertion_error_w_true(self):
124 assert True is True
the test is green again.
REFACTOR: make it better
I add calls to assertIsNot and assertIs to test_assertion_error_w_true
122 # def test_assertion_error_w_true(): 123 def test_assertion_error_w_true(self): 124 assert True is True 125 self.assertIsNot(True, True) 126 127 assert_is_not_true(None) 128 self.assertIs(None, True) 129 130 assert_is_not_true(False) 131 self.assertIs(False, True) 132 133 assert_is_not_true(0) 134 self.assertIs(0, True) 135 136 assert_is_not_true(0.0) 137 self.assertIs(0.0, True) 138 139 assert_is_not_true('') 140 self.assertIs('', True) 141 142 assert_is_not_true(()) 143 self.assertIs((), True) 144 145 assert_is_not_true([]) 146 self.assertIs([], True) 147 148 assert_is_not_true(set()) 149 self.assertIs(set(), True) 150 151 assert_is_not_true({}) 152 self.assertIs({}, True) 153 154 155def test_assertion_error_w_equality():the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: Truecompare this with the error message for
assert True is not TrueE assert True is not TrueI change assertIsNot to assertIs for
(True, True)122 # def test_assertion_error_w_true(): 123 def test_assertion_error_w_true(self): 124 assert True is True 125 # self.assertIsNot(True, True) 126 self.assertIs(True, True) 127 128 assert_is_not_true(None)the terminal is my friend, and shows AssertionError
AssertionError: None is not TrueI change assertIs to assertIsNot for
(None, True)128 assert_is_not_true(None) 129 # self.assertIs(None, True) 130 self.assertIsNot(None, True) 131 132 assert_is_not_true(False)the terminal is my friend, and shows AssertionError
AssertionError: False is not TrueI change assertIsNot to assertIs for
(False, True)132 assert_is_not_true(False) 133 # self.assertIs(False, True) 134 self.assertIsNot(False, True) 135 136 assert_is_not_true(0)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not TrueI change assertIs to assertIsNot for
(0, True)136 assert_is_not_true(0) 137 # self.assertIs(0, True) 138 self.assertIsNot(0, True) 139 140 assert_is_not_true(0.0)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not TrueI change assertIs to assertIsNot for
(0.0, True)140 assert_is_not_true(0.0) 141 # self.assertIs(0.0, True) 142 self.assertIsNot(0.0, True) 143 144 assert_is_not_true('')the terminal is my friend, and shows AssertionError
AssertionError: '' is not TrueI change assertIs to assertIsNot for
('', True)144 assert_is_not_true('') 145 # self.assertIs('', True) 146 self.assertIsNot('', True) 147 148 assert_is_not_true(())the terminal is my friend, and shows AssertionError
AssertionError: () is not TrueI change assertIs to assertIsNot for
((), True)148 assert_is_not_true(()) 149 # self.assertIs((), True) 150 self.assertIsNot((), True) 151 152 assert_is_not_true([])the terminal is my friend, and shows AssertionError
AssertionError: [] is not TrueI change assertIs to assertIsNot for
([], True)152 assert_is_not_true([]) 153 # self.assertIs([], True) 154 self.assertIsNot([], True) 155 156 assert_is_not_true(set())the terminal is my friend, and shows AssertionError
AssertionError: set() is not TrueI change assertIs to assertIsNot for
(set(), True)156 assert_is_not_true(set()) 157 # self.assertIs(set(), True) 158 self.assertIsNot(set(), True) 159 160 assert_is_not_true({})the terminal is my friend, and shows AssertionError
AssertionError: {} is not TrueI change assertIs to assertIsNot for
({}, True)160 assert_is_not_true({}) 161 # self.assertIs({}, True) 162 self.assertIsNot({}, True) 163 164 165def test_assertion_error_w_equality():the test passes.
I add a variable for
{}156 assert_is_not_true(set()) 157 # self.assertIs(set(), True) 158 self.assertIsNot(set(), True) 159 160 a_dictionary = {} 161 assert_is_not_true({}) 162 # self.assertIs({}, True) 163 self.assertIsNot({}, True)I use the variable to remove repetition of
{}160 a_dictionary = {} 161 # assert_is_not_true({}) 162 assert_is_not_true(a_dictionary) 163 # self.assertIs({}, True) 164 # self.assertIsNot({}, True) 165 self.assertIsNot(a_dictionary, True) 166 167 168def test_assertion_error_w_equality():the test is still green.
I add a variable for
set()152 assert_is_not_true([]) 153 # self.assertIs([], True) 154 self.assertIsNot([], True) 155 156 a_set = set() 157 assert_is_not_true(set()) 158 # self.assertIs(set(), True) 159 self.assertIsNot(set(), True)I use the variable to remove repetition of
set()156 a_set = set() 157 # assert_is_not_true(set()) 158 assert_is_not_true(a_set) 159 # self.assertIs(set(), True) 160 # self.assertIsNot(set(), True) 161 self.assertIsNot(a_set, True) 162 163 a_dictionary = {}still green.
I add a variable for
[]148 assert_is_not_true(()) 149 # self.assertIs((), True) 150 self.assertIsNot((), True) 151 152 a_list = [] 153 assert_is_not_true([]) 154 # self.assertIs([], True) 155 self.assertIsNot([], True)I use the variable to remove repetition of
[]152 a_list = [] 153 # assert_is_not_true([]) 154 assert_is_not_true(a_list) 155 # self.assertIs([], True) 156 # self.assertIsNot([], True) 157 self.assertIsNot(a_list, True) 158 159 a_set = set()green.
I add a variable for
()144 assert_is_not_true('') 145 # self.assertIs('', True) 146 self.assertIsNot('', True) 147 148 a_tuple = () 149 assert_is_not_true(()) 150 # self.assertIs((), True) 151 self.assertIsNot((), True)I use the variable to remove repetition of
()148 a_tuple = () 149 # assert_is_not_true(()) 150 assert_is_not_true(a_tuple) 151 # self.assertIs((), True) 152 # self.assertIsNot((), True) 153 self.assertIsNot(a_tuple, True) 154 155 a_list = []still green.
I add a variable for
''140 assert_is_not_true(0.0) 141 # self.assertIs(0.0, True) 142 self.assertIsNot(0.0, True) 143 144 a_string = '' 145 assert_is_not_true('') 146 # self.assertIs('', True) 147 self.assertIsNot('', True)I use the variable to remove repetition of
''144 a_string = '' 145 # assert_is_not_true('') 146 assert_is_not_true(a_string) 147 # self.assertIs('', True) 148 # self.assertIsNot('', True) 149 self.assertIsNot(a_string, True) 150 151 a_tuple = ()the test is still green.
I add a variable for
0.0136 assert_is_not_true(0) 137 # self.assertIs(0, True) 138 self.assertIsNot(0, True) 139 140 a_float = 0.0 141 assert_is_not_true(0.0) 142 # self.assertIs(0.0, True) 143 self.assertIsNot(0.0, True)I use the variable to remove repetition of
0.0140 a_float = 0.0 141 # assert_is_not_true(0.0) 142 assert_is_not_true(a_float) 143 # self.assertIs(0.0, True) 144 # self.assertIsNot(0.0, True) 145 self.assertIsNot(a_float, True) 146 147 a_string = ''still green.
I add a variable for
0132 assert_is_not_true(False) 133 # self.assertIs(False, True) 134 self.assertIsNot(False, True) 135 136 an_integer = 0 137 assert_is_not_true(0) 138 # self.assertIs(0, True) 139 self.assertIsNot(0, True)I use the variable to remove repetition of
0136 an_integer = 0 137 # assert_is_not_true(0) 138 assert_is_not_true(an_integer) 139 # self.assertIs(0, True) 140 # self.assertIsNot(0, True) 141 self.assertIsNot(an_integer, True) 142 143 a_float = 0.0green.
I remove the commented lines from test_assertion_error_w_true
118 a_dictionary = {} 119 assert_is_not_false(a_dictionary) 120 self.assertIsNot(a_dictionary, False) 121 122 def test_assertion_error_w_true(self): 123 assert True is True 124 self.assertIs(True, True) 125 126 assert_is_not_true(None) 127 self.assertIsNot(None, True) 128 129 assert_is_not_true(False) 130 self.assertIsNot(False, True)132 an_integer = 0 133 assert_is_not_true(an_integer) 134 self.assertIsNot(an_integer, True) 135 136 a_float = 0.0 137 assert_is_not_true(a_float) 138 self.assertIsNot(a_float, True) 139 140 a_string = '' 141 assert_is_not_true(a_string) 142 self.assertIsNot(a_string, True)144 a_tuple = () 145 assert_is_not_true(a_tuple) 146 self.assertIsNot(a_tuple, True) 147 148 a_list = [] 149 assert_is_not_true(a_list) 150 self.assertIsNot(a_list, True) 151 152 a_set = set() 153 assert_is_not_true(a_set) 154 self.assertIsNot(a_set, True) 155 156 a_dictionary = {} 157 assert_is_not_true(a_dictionary) 158 self.assertIsNot(a_dictionary, True) 159 160 161def test_assertion_error_w_equality():I add a git commit message in the other terminal
git commit -am \ 'move test_assertion_error_w_true to TestAssertionError'
test_assertion_error_w_equality with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_assertion_error_w_equality to make it a method of the TestAssertionError
156 a_dictionary = {} 157 assert_is_not_true(a_dictionary) 158 self.assertIsNot(a_dictionary, True) 159 160 def test_assertion_error_w_equality(): 161 assert_equal(None, None) 162 assert_equal(False, False) 163 assert_equal(True, True) 164 165 assert_not_equal(False, None) 166 assert_not_equal(False, True) 167 assert_not_equal(True, None) 168 169 170def test_assertion_error_w_is_vs_equal():the terminal is my friend, and shows TypeError
TypeError: TestAssertionError.test_assertion_error_w_equality() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_assertion_error_w_equality
156 a_dictionary = {}
157 assert_is_not_true(a_dictionary)
158 self.assertIsNot(a_dictionary, True)
159
160 # def test_assertion_error_w_equality():
161 def test_assertion_error_w_equality(self):
162 assert_equal(None, None)
163 assert_equal(False, False)
164 assert_equal(True, True)
the test is green again.
REFACTOR: make it better
I add calls to assertNotEqual and assertEqual to test_assertion_error_w_equality
160 # def test_assertion_error_w_equality(): 161 def test_assertion_error_w_equality(self): 162 assert_equal(None, None) 163 self.assertNotEqual(None, None) 164 165 assert_equal(False, False) 166 self.assertNotEqual(False, False) 167 168 assert_equal(True, True) 169 self.assertNotEqual(True, True) 170 171 assert_not_equal(False, None) 172 self.assertEqual(False, None) 173 174 assert_not_equal(False, True) 175 self.assertEqual(False, True) 176 177 assert_not_equal(True, None) 178 self.assertEqual(True, None) 179 180 181def test_assertion_error_w_is_vs_equal():the terminal is my friend, and shows AssertionError
AssertionError: None == Nonecompare this with the error for
assert None != NoneE assert None != NoneI change assertNotEqual to assertEqual for
(None, None)160 # def test_assertion_error_w_equality(): 161 def test_assertion_error_w_equality(self): 162 assert_equal(None, None) 163 # self.assertNotEqual(None, None) 164 self.assertEqual(None, None) 165 166 assert_equal(False, False)the terminal is my friend, and shows AssertionError
AssertionError: False == FalseI change assertNotEqual to assertEqual for
(False, False)166 assert_equal(False, False) 167 # self.assertNotEqual(False, False) 168 self.assertEqual(False, False) 169 170 assert_equal(True, True)the terminal is my friend, and shows AssertionError
AssertionError: True == TrueI change assertNotEqual to assertEqual for
(True, True)170 assert_equal(True, True) 171 # self.assertNotEqual(True, True) 172 self.assertEqual(True, True) 173 174 assert_not_equal(False, None)the terminal is my friend, and shows AssertionError
AssertionError: False != NoneI change assertEqual to assertNotEqual for
(False, None)174 assert_not_equal(False, None) 175 # self.assertEqual(False, None) 176 self.assertNotEqual(False, None) 177 178 assert_not_equal(False, True)the terminal is my friend, and shows AssertionError
AssertionError: False != Truecompare this with the error for
assert False == TrueE assert False == TrueI change assertEqual to assertNotEqual for
(False, True)178 assert_not_equal(False, True) 179 # self.assertEqual(False, True) 180 self.assertNotEqual(False, True) 181 182 assert_not_equal(True, None)the terminal is my friend, and shows AssertionError
AssertionError: True != NoneI change assertEqual to assertNotEqual for
(True, None)182 assert_not_equal(True, None) 183 # self.assertEqual(True, None) 184 self.assertNotEqual(True, None) 185 186 187def test_assertion_error_w_is_vs_equal():the test passes.
I remove the commented lines from test_assertion_error_w_equality
156 a_dictionary = {} 157 assert_is_not_true(a_dictionary) 158 self.assertIsNot(a_dictionary, True) 159 160 def test_assertion_error_w_equality(self): 161 assert_equal(None, None) 162 self.assertEqual(None, None) 163 164 assert_equal(False, False) 165 self.assertEqual(False, False) 166 167 assert_equal(True, True) 168 self.assertEqual(True, True)170 assert_not_equal(False, None) 171 self.assertNotEqual(False, None) 172 173 assert_not_equal(False, True) 174 self.assertNotEqual(False, True) 175 176 assert_not_equal(True, None) 177 self.assertNotEqual(True, None) 178 179 180def test_assertion_error_w_is_vs_equal():I add a git commit message in the other terminal
git commit -am \ 'move test_assertion_error_w_equality to TestAssertionError'
test_assertion_error_w_is_vs_equal with unittest
RED: make it fail
I go back to the terminal where the tests are running
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 in the other terminal
git commit -am \ 'move test_assertion_error_w_is_vs_equal to TestAssertionError'
will_not_run with unittest
RED: make it fail
I go back to the terminal where the tests are running
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 in the other terminal
git commit -am \ 'move will_not_run to TestAssertionError'
test_failure with unittest
RED: make it fail
I go back to the terminal where the tests are running
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 in the other terminal
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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 in the other terminal
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
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.