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_pythonfolderI open
test_assertion_error.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
the terminal is my friend, and shows
tests/test_assertion_error.py ....... [100%] ================== 7 passed in A.BCs ===================
add TestAssertionError object
RED: make it fail
I add an object named
AssertionErrorwith a method for the first failing test totest_assertion_error.pythe test is still green.
I change the name of the object to
TestAssertionErrorthe terminal is my friend, and shows AttributeError
AttributeError: 'TestAssertionError' object has no attribute 'assertEqual'I add AttributeError to the list of Exceptions seen
GREEN: make it pass
I add unittest.TestCase as the parent object of
TestAssertionErrorthe 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
I 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
the test passes.
REFACTOR: make it better
I remove the commented lines from TestAssertionError
I open a new terminal then make sure I am in the
assertion_errorfolder
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
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
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
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)the terminal is my friend, and shows AssertionError
AssertionError: '11' == '11'I change assertNotEqual to assertEqual for
('1'+'1', '11')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')the test passes.
I add variables for
'I am' + ' alive'and'I am alive'I use the variables to remove repetition of
'I am' + ' alive'and'I am alive'the test is still green.
I add variables for
'1' + '1'and'11'I use the variables to remove repetition of
'1' + '1'and'11'still green.
I add variables for
1 + 1and2I use the variables to remove repetition of
1 + 1and2green.
I remove the commented lines from test_assert_keyword
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)
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
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)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)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)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not NoneI change assertIs to assertIsNot for
(0, None)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not NoneI change assertIs to assertIsNot for
(0.0, None)the terminal is my friend, and shows AssertionError
AssertionError: '' is not NoneI change assertIs to assertIsNot for
('', None)the terminal is my friend, and shows AssertionError
AssertionError: () is not NoneI change assertIs to assertIsNot for
((), None)the terminal is my friend, and shows AssertionError
AssertionError: [] is not NoneI change assertIs to assertIsNot for
([], None)the terminal is my friend, and shows AssertionError
AssertionError: set() is not NoneI change assertIs to assertIsNot for
(set(), None)the terminal is my friend, and shows AssertionError
AssertionError: {} is not NoneI change assertIs to assertIsNot for
({}, None)the test passes.
I add a variable for
{}I use the variable to remove repetition of
{}the test is still green.
I add a variable for
set()I use the variable to remove repetition of
set()still green.
I add a variable for
[]I use the variable to remove repetition of
[]green.
I add a variable for
()I use the variable to remove repetition of
()still green.
I add a variable for
''I use the variable to remove repetition of
''the test is still green.
I add a variable for
0.0I use the variable to remove repetition of
0.0still green.
I add a variable for
0I use the variable to remove repetition of
0green.
I remove the commented lines from test_assertion_error_w_none
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
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
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)the terminal is my friend, and shows AssertionError
AssertionError: None is not FalseI change assertIs to assertIsNot for
(None, False)the terminal is my friend, and shows AssertionError
AssertionError: True is not FalseI change assertIs to assertIsNot for
(True, False)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not FalseI change assertIs to assertIsNot for
(0, False)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not FalseI change assertIs to assertIsNot for
(0.0, False)the terminal is my friend, and shows AssertionError
AssertionError: '' is not FalseI change assertIs to assertIsNot for
('', False)the terminal is my friend, and shows AssertionError
AssertionError: () is not FalseI change assertIs to assertIsNot for
((), False)the terminal is my friend, and shows AssertionError
AssertionError: [] is not FalseI change assertIs to assertIsNot for
([], False)the terminal is my friend, and shows AssertionError
AssertionError: set() is not FalseI change assertIs to assertIsNot for
(set(), False)the terminal is my friend, and shows AssertionError
AssertionError: {} is not FalseI change assertIs to assertIsNot for
({}, False)the test passes.
I add a variable for
{}I use the variable to remove repetition of
{}the test is still green.
I add a variable for
set()I use the variable to remove repetition of
set()still green.
I add a variable for
[]I use the variable to remove repetition of
[]green.
I add a variable for
()I use the variable to remove repetition of
()still green.
I add a variable for
''I use the variable to remove repetition of
''the test is still green.
I add a variable for
0.0I use the variable to remove repetition of
0.0still green.
I add a variable for
0I use the variable to remove repetition of
0green.
I remove the commented lines from test_assertion_error_w_false
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
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
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)the terminal is my friend, and shows AssertionError
AssertionError: None is not TrueI change assertIs to assertIsNot for
(None, True)the terminal is my friend, and shows AssertionError
AssertionError: False is not TrueI change assertIsNot to assertIs for
(False, True)the terminal is my friend, and shows AssertionError
AssertionError: 0 is not TrueI change assertIs to assertIsNot for
(0, True)the terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not TrueI change assertIs to assertIsNot for
(0.0, True)the terminal is my friend, and shows AssertionError
AssertionError: '' is not TrueI change assertIs to assertIsNot for
('', True)the terminal is my friend, and shows AssertionError
AssertionError: () is not TrueI change assertIs to assertIsNot for
((), True)the terminal is my friend, and shows AssertionError
AssertionError: [] is not TrueI change assertIs to assertIsNot for
([], True)the terminal is my friend, and shows AssertionError
AssertionError: set() is not TrueI change assertIs to assertIsNot for
(set(), True)the terminal is my friend, and shows AssertionError
AssertionError: {} is not TrueI change assertIs to assertIsNot for
({}, True)the test passes.
I add a variable for
{}I use the variable to remove repetition of
{}the test is still green.
I add a variable for
set()I use the variable to remove repetition of
set()still green.
I add a variable for
[]I use the variable to remove repetition of
[]green.
I add a variable for
()I use the variable to remove repetition of
()still green.
I add a variable for
''I use the variable to remove repetition of
''the test is still green.
I add a variable for
0.0I use the variable to remove repetition of
0.0still green.
I add a variable for
0I use the variable to remove repetition of
0green.
I remove the commented lines from test_assertion_error_w_true
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
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
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)the terminal is my friend, and shows AssertionError
AssertionError: False == FalseI change assertNotEqual to assertEqual for
(False, False)the terminal is my friend, and shows AssertionError
AssertionError: True == TrueI change assertNotEqual to assertEqual for
(True, True)the terminal is my friend, and shows AssertionError
AssertionError: False != NoneI change assertEqual to assertNotEqual for
(False, None)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)the terminal is my friend, and shows AssertionError
AssertionError: True != NoneI change assertEqual to assertNotEqual for
(True, None)the test passes.
I remove the commented lines from test_assertion_error_w_equality
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
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.0the 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
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.0the 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
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
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
the tests are still green.
I add a call to test_assertEqual
still green, it does not raise NameError because this method never gets called by pytest.
I add
selfto the parentheses of will not rungreen, it does not raise AssertionError.
I change the name from will not run to
test_will_not_runthe 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
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
the 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
the terminal is my friend, and shows AssertionError
AssertionError: False == FalseI change assertNotEqual to assertEqual
the test passes.
I remove the commented lines from test_failure
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 TestAssertionErrorI use the object attribute to remove repetition of
an_integer = 0from test_assertion_error_w_nonethe test is still green.
I use the object attribute to remove repetition of
an_integer = 0from test_assertion_error_w_falsestill green.
I use the object attribute to remove repetition of
an_integer = 0from test_assertion_error_w_truegreen.
I add an object attribute for
a_float = 0.0I use the object attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_nonestill green.
I use the object attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_falsethe test is still green.
I use the object attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_truestill green.
I add an object attribute for
a_string = ''I use the object attribute to remove repetition of
a_string = ''from test_assertion_error_w_nonegreen.
I use the object attribute to remove repetition of
a_string = ''from test_assertion_error_w_falsestill green.
I use the object attribute to remove repetition of
a_string = ''from test_assertion_error_w_truethe test is still green.
I add an object attribute for
a_tuple = ()I use the object attribute to remove repetition of
a_tuple = ()from test_assertion_error_w_nonestill green.
I use the object attribute to remove repetition of
a_tuple = ()from test_assertion_error_w_falsegreen.
I use the object attribute to remove repetition of
a_tuple = ()from test_assertion_error_w_truestill green.
I add an object attribute for
a_list = []I use the object attribute to remove repetition of
a_list = []from test_assertion_error_w_nonethe test is still green.
I use the object attribute to remove repetition of
a_list = []from test_assertion_error_w_falsestill green.
I use the object attribute to remove repetition of
a_list = []from test_assertion_error_w_truegreen.
I add an object attribute for
a_set = set()I use the object attribute to remove repetition of
a_set = set()from test_assertion_error_w_nonestill green.
I use the object attribute to remove repetition of
a_set = set()from test_assertion_error_w_falsethe test is still green.
I use the object attribute to remove repetition of
a_set = set()from test_assertion_error_w_truestill green.
I add an object attribute for
a_dictionary = {}I use the object attribute to remove repetition of
a_dictionary = {}from test_assertion_error_w_nonethe test is still green.
I use the object attribute to remove repetition of
a_dictionary = {}from test_assertion_error_w_falsestill green.
I use the object attribute to remove repetition of
a_dictionary = {}from test_assertion_error_w_truegreen.
I remove the commented lines from test_assertion_error_w_true
I remove the commented lines from test_assertion_error_w_false
I remove the commented lines from test_assertion_error_w_none
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_errorthe 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.