AssertionError: use unittest
I used the setUp method in classes to remove repetition of variables by making them class attributes that the test methods can all use.
I want to do the same thing with the assertion_project.
preview
I have these tests by the end of the chapter
1import unittest
2
3
4class TestAssertionError(unittest.TestCase):
5
6 an_integer = 0
7 a_float = 0.0
8 a_string = 'a string'
9 a_tuple = (0, 1, 2, 'n')
10 a_list = [0, 1, 2, 'n']
11 a_set = {0, 1, 2, 'n'}
12 a_dictionary = {'key': 'value'}
13
14 def test_assert_keyword(self):
15 reality = 1 + 1
16 my_expectation = 2
17 assert reality == my_expectation
18 self.assertEqual(reality, my_expectation)
19
20 reality = '1' + '1'
21 my_expectation = '11'
22 assert reality == my_expectation
23 self.assertEqual(reality, my_expectation)
24
25 reality = 'I am' + ' alive'
26 my_expectation = 'I am alive'
27 assert reality == my_expectation
28 self.assertEqual(reality, my_expectation)
29
30 def test_assertion_error_w_none(self):
31 assert None is None
32 self.assertIs(None, None)
33
34 assert False is not None
35 self.assertIsNot(False, None)
36
37 assert True is not None
38 self.assertIsNot(True, None)
39
40 assert self.an_integer is not None
41 self.assertIsNot(self.an_integer, None)
42
43 assert self.a_float is not None
44 self.assertIsNot(self.a_float, None)
45
46 assert self.a_string is not None
47 self.assertIsNot(self.a_string, None)
48
49 assert self.a_tuple is not None
50 self.assertIsNot(self.a_tuple, None)
51
52 assert self.a_list is not None
53 self.assertIsNot(self.a_list, None)
54
55 assert self.a_set is not None
56 self.assertIsNot(self.a_set, None)
57
58 assert self.a_dictionary is not None
59 self.assertIsNot(self.a_dictionary, None)
60
61 def test_assertion_error_w_false(self):
62 assert None is not False
63 self.assertIsNot(None, False)
64
65 assert False is False
66 self.assertIs(False, False)
67
68 assert True is not False
69 self.assertIsNot(True, False)
70
71 assert self.an_integer is not False
72 self.assertIsNot(self.an_integer, False)
73
74 assert self.a_float is not False
75 self.assertIsNot(self.a_float, False)
76
77 assert self.a_string is not False
78 self.assertIsNot(self.a_string, False)
79
80 assert self.a_tuple is not False
81 self.assertIsNot(self.a_tuple, False)
82
83 assert self.a_list is not False
84 self.assertIsNot(self.a_list, False)
85
86 assert self.a_set is not False
87 self.assertIsNot(self.a_set, False)
88
89 assert self.a_dictionary is not False
90 self.assertIsNot(self.a_dictionary, False)
91
92 def test_assertion_error_w_true(self):
93 assert None is not True
94 self.assertIsNot(None, True)
95
96 assert False is not True
97 self.assertIsNot(False, True)
98
99 assert True is True
100 self.assertIs(True, True)
101
102 assert self.an_integer is not True
103 self.assertIsNot(self.an_integer, True)
104
105 assert self.a_float is not True
106 self.assertIsNot(self.a_float, True)
107
108 assert self.a_string is not True
109 self.assertIsNot(self.a_string, True)
110
111 assert self.a_tuple is not True
112 self.assertIsNot(self.a_tuple, True)
113
114 assert self.a_list is not True
115 self.assertIsNot(self.a_list, True)
116
117 assert self.a_set is not True
118 self.assertIsNot(self.a_set, True)
119
120 assert self.a_dictionary is not True
121 self.assertIsNot(self.a_dictionary, True)
122
123 def test_assertion_error_w_equality(self):
124 assert None == None
125 self.assertEqual(None, None)
126
127 assert False != None
128 self.assertNotEqual(False, None)
129
130 assert False != True
131 self.assertNotEqual(False, True)
132
133 assert False == False
134 self.assertEqual(False, False)
135
136 assert True != None
137 self.assertNotEqual(True, None)
138
139 assert True == True
140 self.assertEqual(True, True)
141
142 def test_assertion_error_w_is_vs_equal(self):
143 assert 0 is not 0.0
144 self.assertIsNot(0, 0.0)
145
146 assert 0 == 0.0
147 self.assertEqual(0, 0.0)
148
149
150# NOTES
151# a dictionary is not True
152# a dictionary is not False
153# a dictionary is not None
154# a set is not True
155# a set is not False
156# a set is not None
157# a list is not True
158# a list is not False
159# a list is not None
160# a tuple is not True
161# a tuple is not False
162# a tuple is not None
163# a string is not True
164# a string is not False
165# a string is not None
166# a float is not True
167# a float is not False
168# a float is not None
169# an integer is not True
170# an integer is not False
171# an integer is not None
172# True is True and equal to True
173# True is not False and NOT equal to False
174# True is not None and NOT equal to None
175# False is not True and NOT equal to True
176# False is False and equal to False
177# False is not None and NOT equal to None
178# None is not True and NOT equal to True
179# None is not False and NOT equal to False
180# None is None and equal to None
181
182
183# Exceptions seen
184# AssertionError
185# NameError
continue the project
Make sure you are in the
pumping_pythonfolder with pwd in the terminalpwdif the terminal does not show
.../pumping_pythonchange directory to the
pumping_pythonfolderOnce in
pumping_python, change directory to the projectcd assertion_errorthe terminal shows
.../pumping_python/assertion_errorI run the tests with pytest-watcher
uv run pytest-watcher . --nowthe terminal is my friend, and shows
rootdir: .../pumping_python/assertion_error configfile: pyproject.toml collected 6 items tests/test_assertion_error.py ...... [100%] ================== 6 passed in A.BCs ===================I hold ctrl (Windows) or option (MacOS) on the keyboard, then click on
tests/test_assertion_error.pywith the mouse to open it
remove repetition with class attributes
I add the setUp method to the
TestAssertionErrorclass with a class attribute4class TestAssertionError(unittest.TestCase): 5 6 def setUp(self): 7 self.an_integer = 0 8 9 def test_assert_keyword(self):I use the class attribute to remove repetition of
an_integer = 0from test_assertion_error_w_none25 def test_assertion_error_w_none(self): 26 assert None is None 27 self.assertIs(None, None) 28 29 assert False is not None 30 self.assertIsNot(False, None) 31 32 assert True is not None 33 self.assertIsNot(True, None) 34 35 # an_integer = 0 36 # assert an_integer is not None 37 # self.assertIsNot(an_integer, None) 38 assert self.an_integer is not None 39 self.assertIsNot(self.an_integer, None) 40 41 a_float = 0.0 42 assert a_float is not None 43 self.assertIsNot(a_float, None) 44 45 a_string = 'a string' 46 assert a_string is not None 47 self.assertIsNot(a_string, None) 48 49 a_tuple = (0, 1, 2, 'n') 50 assert a_tuple is not None 51 self.assertIsNot(a_tuple, None) 52 53 a_list = [0, 1, 2, 'n'] 54 assert a_list is not None 55 self.assertIsNot(a_list, None) 56 57 a_set = {0, 1, 2, 'n'} 58 assert a_set is not None 59 self.assertIsNot(a_set, None) 60 61 a_dictionary = {'key': 'value'} 62 assert a_dictionary is not None 63 self.assertIsNot(a_dictionary, None) 64 65 def test_assertion_error_w_false(self):the test is still green.
I use the class attribute to remove repetition of
an_integer = 0from test_assertion_error_w_false65 def test_assertion_error_w_false(self): 66 assert None is not False 67 self.assertIsNot(None, False) 68 69 assert False is False 70 self.assertIs(False, False) 71 72 assert True is not False 73 self.assertIsNot(True, False) 74 75 # an_integer = 0 76 # assert an_integer is not False 77 # self.assertIsNot(an_integer, False) 78 assert self.an_integer is not False 79 self.assertIsNot(self.an_integer, False) 80 81 a_float = 0.0 82 assert a_float is not False 83 self.assertIsNot(a_float, False) 84 85 a_string = 'a string' 86 assert a_string is not False 87 self.assertIsNot(a_string, False) 88 89 a_tuple = (0, 1, 2, 'n') 90 assert a_tuple is not False 91 self.assertIsNot(a_tuple, False) 92 93 a_list = [0, 1, 2, 'n'] 94 assert a_list is not False 95 self.assertIsNot(a_list, False) 96 97 a_set = {0, 1, 2, 'n'} 98 assert a_set is not False 99 self.assertIsNot(a_set, False) 100 101 a_dictionary = {'key': 'value'} 102 assert a_dictionary is not False 103 self.assertIsNot(a_dictionary, False) 104 105 def test_assertion_error_w_true(self):still green.
I use the class attribute to remove repetition of
an_integer = 0from test_assertion_error_w_true105 def test_assertion_error_w_true(self): 106 assert None is not True 107 self.assertIsNot(None, True) 108 109 assert False is not True 110 self.assertIsNot(False, True) 111 112 assert True is True 113 self.assertIs(True, True) 114 115 # an_integer = 0 116 # assert an_integer is not True 117 # self.assertIsNot(an_integer, True) 118 assert self.an_integer is not True 119 self.assertIsNot(self.an_integer, True) 120 121 a_float = 0.0 122 assert a_float is not True 123 self.assertIsNot(a_float, True) 124 125 a_string = 'a string' 126 assert a_string is not True 127 self.assertIsNot(a_string, True) 128 129 a_tuple = (0, 1, 2, 'n') 130 assert a_tuple is not True 131 self.assertIsNot(a_tuple, True) 132 133 a_list = [0, 1, 2, 'n'] 134 assert a_list is not True 135 self.assertIsNot(a_list, True) 136 137 a_set = {0, 1, 2, 'n'} 138 assert a_set is not True 139 self.assertIsNot(a_set, True) 140 141 a_dictionary = {'key': 'value'} 142 assert a_dictionary is not True 143 self.assertIsNot(a_dictionary, True) 144 145 def test_assertion_error_w_equality(self):green.
I add a class attribute to the setUp method
6 def setUp(self): 7 self.an_integer = 0 8 self.a_float = 0.0 9 10 def test_assert_keyword(self):I use the class attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_none26 def test_assertion_error_w_none(self): 27 assert None is None 28 self.assertIs(None, None) 29 30 assert False is not None 31 self.assertIsNot(False, None) 32 33 assert True is not None 34 self.assertIsNot(True, None) 35 36 # an_integer = 0 37 # assert an_integer is not None 38 # self.assertIsNot(an_integer, None) 39 assert self.an_integer is not None 40 self.assertIsNot(self.an_integer, None) 41 42 # a_float = 0.0 43 # assert a_float is not None 44 # self.assertIsNot(a_float, None) 45 assert self.a_float is not None 46 self.assertIsNot(self.a_float, None) 47 48 a_string = 'a string' 49 assert a_string is not None 50 self.assertIsNot(a_string, None) 51 52 a_tuple = (0, 1, 2, 'n') 53 assert a_tuple is not None 54 self.assertIsNot(a_tuple, None) 55 56 a_list = [0, 1, 2, 'n'] 57 assert a_list is not None 58 self.assertIsNot(a_list, None) 59 60 a_set = {0, 1, 2, 'n'} 61 assert a_set is not None 62 self.assertIsNot(a_set, None) 63 64 a_dictionary = {'key': 'value'} 65 assert a_dictionary is not None 66 self.assertIsNot(a_dictionary, None) 67 68 def test_assertion_error_w_false(self):still green.
I use the class attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_false68 def test_assertion_error_w_false(self): 69 assert None is not False 70 self.assertIsNot(None, False) 71 72 assert False is False 73 self.assertIs(False, False) 74 75 assert True is not False 76 self.assertIsNot(True, False) 77 78 # an_integer = 0 79 # assert an_integer is not False 80 # self.assertIsNot(an_integer, False) 81 assert self.an_integer is not False 82 self.assertIsNot(self.an_integer, False) 83 84 # a_float = 0.0 85 # assert a_float is not False 86 # self.assertIsNot(a_float, False) 87 assert self.a_float is not False 88 self.assertIsNot(self.a_float, False) 89 90 a_string = 'a string' 91 assert a_string is not False 92 self.assertIsNot(a_string, False) 93 94 a_tuple = (0, 1, 2, 'n') 95 assert a_tuple is not False 96 self.assertIsNot(a_tuple, False) 97 98 a_list = [0, 1, 2, 'n'] 99 assert a_list is not False 100 self.assertIsNot(a_list, False) 101 102 a_set = {0, 1, 2, 'n'} 103 assert a_set is not False 104 self.assertIsNot(a_set, False) 105 106 a_dictionary = {'key': 'value'} 107 assert a_dictionary is not False 108 self.assertIsNot(a_dictionary, False) 109 110 def test_assertion_error_w_true(self):the test is still green.
I use the class attribute to remove repetition of
a_float = 0.0from test_assertion_error_w_true110 def test_assertion_error_w_true(self): 111 assert None is not True 112 self.assertIsNot(None, True) 113 114 assert False is not True 115 self.assertIsNot(False, True) 116 117 assert True is True 118 self.assertIs(True, True) 119 120 # an_integer = 0 121 # assert an_integer is not True 122 # self.assertIsNot(an_integer, True) 123 assert self.an_integer is not True 124 self.assertIsNot(self.an_integer, True) 125 126 # a_float = 0.0 127 # assert a_float is not True 128 # self.assertIsNot(a_float, True) 129 assert self.a_float is not True 130 self.assertIsNot(self.a_float, True) 131 132 a_string = 'a string' 133 assert a_string is not True 134 self.assertIsNot(a_string, True) 135 136 a_tuple = (0, 1, 2, 'n') 137 assert a_tuple is not True 138 self.assertIsNot(a_tuple, True) 139 140 a_list = [0, 1, 2, 'n'] 141 assert a_list is not True 142 self.assertIsNot(a_list, True) 143 144 a_set = {0, 1, 2, 'n'} 145 assert a_set is not True 146 self.assertIsNot(a_set, True) 147 148 a_dictionary = {'key': 'value'} 149 assert a_dictionary is not True 150 self.assertIsNot(a_dictionary, True) 151 152 def test_assertion_error_w_equality(self):still green.
I add a class attribute to the setUp method
6 def setUp(self): 7 self.an_integer = 0 8 self.a_float = 0.0 9 self.a_string = 'a string' 10 11 def test_assert_keyword(self):I use the class attribute to remove repetition of
a_string = 'a string'from test_assertion_error_w_none27 def test_assertion_error_w_none(self): 28 assert None is None 29 self.assertIs(None, None) 30 31 assert False is not None 32 self.assertIsNot(False, None) 33 34 assert True is not None 35 self.assertIsNot(True, None) 36 37 # an_integer = 0 38 # assert an_integer is not None 39 # self.assertIsNot(an_integer, None) 40 assert self.an_integer is not None 41 self.assertIsNot(self.an_integer, None) 42 43 # a_float = 0.0 44 # assert a_float is not None 45 # self.assertIsNot(a_float, None) 46 assert self.a_float is not None 47 self.assertIsNot(self.a_float, None) 48 49 # a_string = 'a string' 50 # assert a_string is not None 51 # self.assertIsNot(a_string, None) 52 assert self.a_string is not None 53 self.assertIsNot(self.a_string, None) 54 55 a_tuple = (0, 1, 2, 'n') 56 assert a_tuple is not None 57 self.assertIsNot(a_tuple, None) 58 59 a_list = [0, 1, 2, 'n'] 60 assert a_list is not None 61 self.assertIsNot(a_list, None) 62 63 a_set = {0, 1, 2, 'n'} 64 assert a_set is not None 65 self.assertIsNot(a_set, None) 66 67 a_dictionary = {'key': 'value'} 68 assert a_dictionary is not None 69 self.assertIsNot(a_dictionary, None) 70 71 def test_assertion_error_w_false(self):green.
I use the class attribute to remove repetition of
a_string = 'a string'from test_assertion_error_w_false71 def test_assertion_error_w_false(self): 72 assert None is not False 73 self.assertIsNot(None, False) 74 75 assert False is False 76 self.assertIs(False, False) 77 78 assert True is not False 79 self.assertIsNot(True, False) 80 81 # an_integer = 0 82 # assert an_integer is not False 83 # self.assertIsNot(an_integer, False) 84 assert self.an_integer is not False 85 self.assertIsNot(self.an_integer, False) 86 87 # a_float = 0.0 88 # assert a_float is not False 89 # self.assertIsNot(a_float, False) 90 assert self.a_float is not False 91 self.assertIsNot(self.a_float, False) 92 93 # a_string = 'a string' 94 # assert a_string is not False 95 # self.assertIsNot(a_string, False) 96 assert self.a_string is not False 97 self.assertIsNot(self.a_string, False) 98 99 a_tuple = (0, 1, 2, 'n') 100 assert a_tuple is not False 101 self.assertIsNot(a_tuple, False) 102 103 a_list = [0, 1, 2, 'n'] 104 assert a_list is not False 105 self.assertIsNot(a_list, False) 106 107 a_set = {0, 1, 2, 'n'} 108 assert a_set is not False 109 self.assertIsNot(a_set, False) 110 111 a_dictionary = {'key': 'value'} 112 assert a_dictionary is not False 113 self.assertIsNot(a_dictionary, False) 114 115 def test_assertion_error_w_true(self):still green.
I use the class attribute to remove repetition of
a_string = 'a string'from test_assertion_error_w_true115 def test_assertion_error_w_true(self): 116 assert None is not True 117 self.assertIsNot(None, True) 118 119 assert False is not True 120 self.assertIsNot(False, True) 121 122 assert True is True 123 self.assertIs(True, True) 124 125 # an_integer = 0 126 # assert an_integer is not True 127 # self.assertIsNot(an_integer, True) 128 assert self.an_integer is not True 129 self.assertIsNot(self.an_integer, True) 130 131 # a_float = 0.0 132 # assert a_float is not True 133 # self.assertIsNot(a_float, True) 134 assert self.a_float is not True 135 self.assertIsNot(self.a_float, True) 136 137 # a_string = 'a string' 138 # assert a_string is not True 139 # self.assertIsNot(a_string, True) 140 assert self.a_string is not True 141 self.assertIsNot(self.a_string, True) 142 143 a_tuple = (0, 1, 2, 'n') 144 assert a_tuple is not True 145 self.assertIsNot(a_tuple, True) 146 147 a_list = [0, 1, 2, 'n'] 148 assert a_list is not True 149 self.assertIsNot(a_list, True) 150 151 a_set = {0, 1, 2, 'n'} 152 assert a_set is not True 153 self.assertIsNot(a_set, True) 154 155 a_dictionary = {'key': 'value'} 156 assert a_dictionary is not True 157 self.assertIsNot(a_dictionary, True) 158 159 def test_assertion_error_w_equality(self):the test is still green.
I add a class attribute to the setUp method
6 def setUp(self): 7 self.an_integer = 0 8 self.a_float = 0.0 9 self.a_string = 'a string' 10 self.a_tuple = (0, 1, 2, 'n') 11 12 def test_assert_keyword(self):I use the class attribute to remove repetition of
a_tuple = (0, 1, 2, 'n')from test_assertion_error_w_none28 def test_assertion_error_w_none(self): 29 assert None is None 30 self.assertIs(None, None) 31 32 assert False is not None 33 self.assertIsNot(False, None) 34 35 assert True is not None 36 self.assertIsNot(True, None) 37 38 # an_integer = 0 39 # assert an_integer is not None 40 # self.assertIsNot(an_integer, None) 41 assert self.an_integer is not None 42 self.assertIsNot(self.an_integer, None) 43 44 # a_float = 0.0 45 # assert a_float is not None 46 # self.assertIsNot(a_float, None) 47 assert self.a_float is not None 48 self.assertIsNot(self.a_float, None) 49 50 # a_string = 'a string' 51 # assert a_string is not None 52 # self.assertIsNot(a_string, None) 53 assert self.a_string is not None 54 self.assertIsNot(self.a_string, None) 55 56 # a_tuple = (0, 1, 2, 'n') 57 # assert a_tuple is not None 58 # self.assertIsNot(a_tuple, None) 59 assert self.a_tuple is not None 60 self.assertIsNot(self.a_tuple, None) 61 62 a_list = [0, 1, 2, 'n'] 63 assert a_list is not None 64 self.assertIsNot(a_list, None) 65 66 a_set = {0, 1, 2, 'n'} 67 assert a_set is not None 68 self.assertIsNot(a_set, None) 69 70 a_dictionary = {'key': 'value'} 71 assert a_dictionary is not None 72 self.assertIsNot(a_dictionary, None) 73 74 def test_assertion_error_w_false(self):still green.
I use the class attribute to remove repetition of
a_tuple = (0, 1, 2, 'n')from test_assertion_error_w_false74 def test_assertion_error_w_false(self): 75 assert None is not False 76 self.assertIsNot(None, False) 77 78 assert False is False 79 self.assertIs(False, False) 80 81 assert True is not False 82 self.assertIsNot(True, False) 83 84 # an_integer = 0 85 # assert an_integer is not False 86 # self.assertIsNot(an_integer, False) 87 assert self.an_integer is not False 88 self.assertIsNot(self.an_integer, False) 89 90 # a_float = 0.0 91 # assert a_float is not False 92 # self.assertIsNot(a_float, False) 93 assert self.a_float is not False 94 self.assertIsNot(self.a_float, False) 95 96 # a_string = 'a string' 97 # assert a_string is not False 98 # self.assertIsNot(a_string, False) 99 assert self.a_string is not False 100 self.assertIsNot(self.a_string, False) 101 102 # a_tuple = (0, 1, 2, 'n') 103 # assert a_tuple is not False 104 # self.assertIsNot(a_tuple, False) 105 assert self.a_tuple is not False 106 self.assertIsNot(self.a_tuple, False) 107 108 a_list = [0, 1, 2, 'n'] 109 assert a_list is not False 110 self.assertIsNot(a_list, False) 111 112 a_set = {0, 1, 2, 'n'} 113 assert a_set is not False 114 self.assertIsNot(a_set, False) 115 116 a_dictionary = {'key': 'value'} 117 assert a_dictionary is not False 118 self.assertIsNot(a_dictionary, False) 119 120 def test_assertion_error_w_true(self):green.
I use the class attribute to remove repetition of
a_tuple = (0, 1, 2, 'n')from test_assertion_error_w_true120 def test_assertion_error_w_true(self): 121 assert None is not True 122 self.assertIsNot(None, True) 123 124 assert False is not True 125 self.assertIsNot(False, True) 126 127 assert True is True 128 self.assertIs(True, True) 129 130 # an_integer = 0 131 # assert an_integer is not True 132 # self.assertIsNot(an_integer, True) 133 assert self.an_integer is not True 134 self.assertIsNot(self.an_integer, True) 135 136 # a_float = 0.0 137 # assert a_float is not True 138 # self.assertIsNot(a_float, True) 139 assert self.a_float is not True 140 self.assertIsNot(self.a_float, True) 141 142 # a_string = 'a string' 143 # assert a_string is not True 144 # self.assertIsNot(a_string, True) 145 assert self.a_string is not True 146 self.assertIsNot(self.a_string, True) 147 148 # a_tuple = (0, 1, 2, 'n') 149 # assert a_tuple is not True 150 # self.assertIsNot(a_tuple, True) 151 assert self.a_tuple is not True 152 self.assertIsNot(self.a_tuple, True) 153 154 a_list = [0, 1, 2, 'n'] 155 assert a_list is not True 156 self.assertIsNot(a_list, True) 157 158 a_set = {0, 1, 2, 'n'} 159 assert a_set is not True 160 self.assertIsNot(a_set, True) 161 162 a_dictionary = {'key': 'value'} 163 assert a_dictionary is not True 164 self.assertIsNot(a_dictionary, True) 165 166 def test_assertion_error_w_equality(self):still green.
I add a class attribute to the setUp method
6 def setUp(self): 7 self.an_integer = 0 8 self.a_float = 0.0 9 self.a_string = 'a string' 10 self.a_tuple = (0, 1, 2, 'n') 11 self.a_list = [0, 1, 2, 'n'] 12 13 def test_assert_keyword(self):I use the class attribute to remove repetition of
a_list = [0, 1, 2, 'n']from test_assertion_error_w_none29 def test_assertion_error_w_none(self): 30 assert None is None 31 self.assertIs(None, None) 32 33 assert False is not None 34 self.assertIsNot(False, None) 35 36 assert True is not None 37 self.assertIsNot(True, None) 38 39 # an_integer = 0 40 # assert an_integer is not None 41 # self.assertIsNot(an_integer, None) 42 assert self.an_integer is not None 43 self.assertIsNot(self.an_integer, None) 44 45 # a_float = 0.0 46 # assert a_float is not None 47 # self.assertIsNot(a_float, None) 48 assert self.a_float is not None 49 self.assertIsNot(self.a_float, None) 50 51 # a_string = 'a string' 52 # assert a_string is not None 53 # self.assertIsNot(a_string, None) 54 assert self.a_string is not None 55 self.assertIsNot(self.a_string, None) 56 57 # a_tuple = (0, 1, 2, 'n') 58 # assert a_tuple is not None 59 # self.assertIsNot(a_tuple, None) 60 assert self.a_tuple is not None 61 self.assertIsNot(self.a_tuple, None) 62 63 # a_list = [0, 1, 2, 'n'] 64 # assert a_list is not None 65 # self.assertIsNot(a_list, None) 66 assert self.a_list is not None 67 self.assertIsNot(self.a_list, None) 68 69 a_set = {0, 1, 2, 'n'} 70 assert a_set is not None 71 self.assertIsNot(a_set, None) 72 73 a_dictionary = {'key': 'value'} 74 assert a_dictionary is not None 75 self.assertIsNot(a_dictionary, None) 76 77 def test_assertion_error_w_false(self):the test is still green.
I use the class attribute to remove repetition of
a_list = [0, 1, 2, 'n']from test_assertion_error_w_false77 def test_assertion_error_w_false(self): 78 assert None is not False 79 self.assertIsNot(None, False) 80 81 assert False is False 82 self.assertIs(False, False) 83 84 assert True is not False 85 self.assertIsNot(True, False) 86 87 # an_integer = 0 88 # assert an_integer is not False 89 # self.assertIsNot(an_integer, False) 90 assert self.an_integer is not False 91 self.assertIsNot(self.an_integer, False) 92 93 # a_float = 0.0 94 # assert a_float is not False 95 # self.assertIsNot(a_float, False) 96 assert self.a_float is not False 97 self.assertIsNot(self.a_float, False) 98 99 # a_string = 'a string' 100 # assert a_string is not False 101 # self.assertIsNot(a_string, False) 102 assert self.a_string is not False 103 self.assertIsNot(self.a_string, False) 104 105 # a_tuple = (0, 1, 2, 'n') 106 # assert a_tuple is not False 107 # self.assertIsNot(a_tuple, False) 108 assert self.a_tuple is not False 109 self.assertIsNot(self.a_tuple, False) 110 111 # a_list = [0, 1, 2, 'n'] 112 # assert a_list is not False 113 # self.assertIsNot(a_list, False) 114 assert self.a_list is not False 115 self.assertIsNot(self.a_list, False) 116 117 a_set = {0, 1, 2, 'n'} 118 assert a_set is not False 119 self.assertIsNot(a_set, False) 120 121 a_dictionary = {'key': 'value'} 122 assert a_dictionary is not False 123 self.assertIsNot(a_dictionary, False) 124 125 def test_assertion_error_w_true(self):still green.
I use the class attribute to remove repetition of
a_list = [0, 1, 2, 'n']from test_assertion_error_w_true125 def test_assertion_error_w_true(self): 126 assert None is not True 127 self.assertIsNot(None, True) 128 129 assert False is not True 130 self.assertIsNot(False, True) 131 132 assert True is True 133 self.assertIs(True, True) 134 135 # an_integer = 0 136 # assert an_integer is not True 137 # self.assertIsNot(an_integer, True) 138 assert self.an_integer is not True 139 self.assertIsNot(self.an_integer, True) 140 141 # a_float = 0.0 142 # assert a_float is not True 143 # self.assertIsNot(a_float, True) 144 assert self.a_float is not True 145 self.assertIsNot(self.a_float, True) 146 147 # a_string = 'a string' 148 # assert a_string is not True 149 # self.assertIsNot(a_string, True) 150 assert self.a_string is not True 151 self.assertIsNot(self.a_string, True) 152 153 # a_tuple = (0, 1, 2, 'n') 154 # assert a_tuple is not True 155 # self.assertIsNot(a_tuple, True) 156 assert self.a_tuple is not True 157 self.assertIsNot(self.a_tuple, True) 158 159 # a_list = [0, 1, 2, 'n'] 160 # assert a_list is not True 161 # self.assertIsNot(a_list, True) 162 assert self.a_list is not True 163 self.assertIsNot(self.a_list, True) 164 165 a_set = {0, 1, 2, 'n'} 166 assert a_set is not True 167 self.assertIsNot(a_set, True) 168 169 a_dictionary = {'key': 'value'} 170 assert a_dictionary is not True 171 self.assertIsNot(a_dictionary, True) 172 173 def test_assertion_error_w_equality(self):green.
I add a class attribute to the setUp method
6 def setUp(self): 7 self.an_integer = 0 8 self.a_float = 0.0 9 self.a_string = 'a string' 10 self.a_tuple = (0, 1, 2, 'n') 11 self.a_list = [0, 1, 2, 'n'] 12 self.a_set = {0, 1, 2, 'n'} 13 14 def test_assert_keyword(self):I use the class attribute to remove repetition of
a_set = {0, 1, 2, 'n'}from test_assertion_error_w_none30 def test_assertion_error_w_none(self): 31 assert None is None 32 self.assertIs(None, None) 33 34 assert False is not None 35 self.assertIsNot(False, None) 36 37 assert True is not None 38 self.assertIsNot(True, None) 39 40 # an_integer = 0 41 # assert an_integer is not None 42 # self.assertIsNot(an_integer, None) 43 assert self.an_integer is not None 44 self.assertIsNot(self.an_integer, None) 45 46 # a_float = 0.0 47 # assert a_float is not None 48 # self.assertIsNot(a_float, None) 49 assert self.a_float is not None 50 self.assertIsNot(self.a_float, None) 51 52 # a_string = 'a string' 53 # assert a_string is not None 54 # self.assertIsNot(a_string, None) 55 assert self.a_string is not None 56 self.assertIsNot(self.a_string, None) 57 58 # a_tuple = (0, 1, 2, 'n') 59 # assert a_tuple is not None 60 # self.assertIsNot(a_tuple, None) 61 assert self.a_tuple is not None 62 self.assertIsNot(self.a_tuple, None) 63 64 # a_list = [0, 1, 2, 'n'] 65 # assert a_list is not None 66 # self.assertIsNot(a_list, None) 67 assert self.a_list is not None 68 self.assertIsNot(self.a_list, None) 69 70 # a_set = {0, 1, 2, 'n'} 71 # assert a_set is not None 72 # self.assertIsNot(a_set, None) 73 assert self.a_set is not None 74 self.assertIsNot(self.a_set, None) 75 76 a_dictionary = {'key': 'value'} 77 assert a_dictionary is not None 78 self.assertIsNot(a_dictionary, None) 79 80 def test_assertion_error_w_false(self):still green.
I use the class attribute to remove repetition of
a_set = {0, 1, 2, 'n'}from test_assertion_error_w_false80 def test_assertion_error_w_false(self): 81 assert None is not False 82 self.assertIsNot(None, False) 83 84 assert False is False 85 self.assertIs(False, False) 86 87 assert True is not False 88 self.assertIsNot(True, False) 89 90 # an_integer = 0 91 # assert an_integer is not False 92 # self.assertIsNot(an_integer, False) 93 assert self.an_integer is not False 94 self.assertIsNot(self.an_integer, False) 95 96 # a_float = 0.0 97 # assert a_float is not False 98 # self.assertIsNot(a_float, False) 99 assert self.a_float is not False 100 self.assertIsNot(self.a_float, False) 101 102 # a_string = 'a string' 103 # assert a_string is not False 104 # self.assertIsNot(a_string, False) 105 assert self.a_string is not False 106 self.assertIsNot(self.a_string, False) 107 108 # a_tuple = (0, 1, 2, 'n') 109 # assert a_tuple is not False 110 # self.assertIsNot(a_tuple, False) 111 assert self.a_tuple is not False 112 self.assertIsNot(self.a_tuple, False) 113 114 # a_list = [0, 1, 2, 'n'] 115 # assert a_list is not False 116 # self.assertIsNot(a_list, False) 117 assert self.a_list is not False 118 self.assertIsNot(self.a_list, False) 119 120 # a_set = {0, 1, 2, 'n'} 121 # assert a_set is not False 122 # self.assertIsNot(a_set, False) 123 assert self.a_set is not False 124 self.assertIsNot(self.a_set, False) 125 126 a_dictionary = {'key': 'value'} 127 assert a_dictionary is not False 128 self.assertIsNot(a_dictionary, False) 129 130 def test_assertion_error_w_true(self):the test is still green.
I use the class attribute to remove repetition of
a_set = {0, 1, 2, 'n'}from test_assertion_error_w_true130 def test_assertion_error_w_true(self): 131 assert None is not True 132 self.assertIsNot(None, True) 133 134 assert False is not True 135 self.assertIsNot(False, True) 136 137 assert True is True 138 self.assertIs(True, True) 139 140 # an_integer = 0 141 # assert an_integer is not True 142 # self.assertIsNot(an_integer, True) 143 assert self.an_integer is not True 144 self.assertIsNot(self.an_integer, True) 145 146 # a_float = 0.0 147 # assert a_float is not True 148 # self.assertIsNot(a_float, True) 149 assert self.a_float is not True 150 self.assertIsNot(self.a_float, True) 151 152 # a_string = 'a string' 153 # assert a_string is not True 154 # self.assertIsNot(a_string, True) 155 assert self.a_string is not True 156 self.assertIsNot(self.a_string, True) 157 158 # a_tuple = (0, 1, 2, 'n') 159 # assert a_tuple is not True 160 # self.assertIsNot(a_tuple, True) 161 assert self.a_tuple is not True 162 self.assertIsNot(self.a_tuple, True) 163 164 # a_list = [0, 1, 2, 'n'] 165 # assert a_list is not True 166 # self.assertIsNot(a_list, True) 167 assert self.a_list is not True 168 self.assertIsNot(self.a_list, True) 169 170 # a_set = {0, 1, 2, 'n'} 171 # assert a_set is not True 172 # self.assertIsNot(a_set, True) 173 assert self.a_set is not True 174 self.assertIsNot(self.a_set, True) 175 176 a_dictionary = {'key': 'value'} 177 assert a_dictionary is not True 178 self.assertIsNot(a_dictionary, True) 179 180 def test_assertion_error_w_equality(self):still green.
I add a class attribute to the setUp method
6 def setUp(self): 7 self.an_integer = 0 8 self.a_float = 0.0 9 self.a_string = 'a string' 10 self.a_tuple = (0, 1, 2, 'n') 11 self.a_list = [0, 1, 2, 'n'] 12 self.a_set = {0, 1, 2, 'n'} 13 self.a_dictionary = {'key': 'value'} 14 15 def test_assert_keyword(self):I use the class attribute to remove repetition of
a_dictionary = {'key': 'value'}from test_assertion_error_w_none31 def test_assertion_error_w_none(self): 32 assert None is None 33 self.assertIs(None, None) 34 35 assert False is not None 36 self.assertIsNot(False, None) 37 38 assert True is not None 39 self.assertIsNot(True, None) 40 41 # an_integer = 0 42 # assert an_integer is not None 43 # self.assertIsNot(an_integer, None) 44 assert self.an_integer is not None 45 self.assertIsNot(self.an_integer, None) 46 47 # a_float = 0.0 48 # assert a_float is not None 49 # self.assertIsNot(a_float, None) 50 assert self.a_float is not None 51 self.assertIsNot(self.a_float, None) 52 53 # a_string = 'a string' 54 # assert a_string is not None 55 # self.assertIsNot(a_string, None) 56 assert self.a_string is not None 57 self.assertIsNot(self.a_string, None) 58 59 # a_tuple = (0, 1, 2, 'n') 60 # assert a_tuple is not None 61 # self.assertIsNot(a_tuple, None) 62 assert self.a_tuple is not None 63 self.assertIsNot(self.a_tuple, None) 64 65 # a_list = [0, 1, 2, 'n'] 66 # assert a_list is not None 67 # self.assertIsNot(a_list, None) 68 assert self.a_list is not None 69 self.assertIsNot(self.a_list, None) 70 71 # a_set = {0, 1, 2, 'n'} 72 # assert a_set is not None 73 # self.assertIsNot(a_set, None) 74 assert self.a_set is not None 75 self.assertIsNot(self.a_set, None) 76 77 # a_dictionary = {'key': 'value'} 78 # assert a_dictionary is not None 79 # self.assertIsNot(a_dictionary, None) 80 assert self.a_dictionary is not None 81 self.assertIsNot(self.a_dictionary, None) 82 83 def test_assertion_error_w_false(self):the test is still green.
I use the class attribute to remove repetition of
a_dictionary = {'key': 'value'}from test_assertion_error_w_false83 def test_assertion_error_w_false(self): 84 assert None is not False 85 self.assertIsNot(None, False) 86 87 assert False is False 88 self.assertIs(False, False) 89 90 assert True is not False 91 self.assertIsNot(True, False) 92 93 # an_integer = 0 94 # assert an_integer is not False 95 # self.assertIsNot(an_integer, False) 96 assert self.an_integer is not False 97 self.assertIsNot(self.an_integer, False) 98 99 # a_float = 0.0 100 # assert a_float is not False 101 # self.assertIsNot(a_float, False) 102 assert self.a_float is not False 103 self.assertIsNot(self.a_float, False) 104 105 # a_string = 'a string' 106 # assert a_string is not False 107 # self.assertIsNot(a_string, False) 108 assert self.a_string is not False 109 self.assertIsNot(self.a_string, False) 110 111 # a_tuple = (0, 1, 2, 'n') 112 # assert a_tuple is not False 113 # self.assertIsNot(a_tuple, False) 114 assert self.a_tuple is not False 115 self.assertIsNot(self.a_tuple, False) 116 117 # a_list = [0, 1, 2, 'n'] 118 # assert a_list is not False 119 # self.assertIsNot(a_list, False) 120 assert self.a_list is not False 121 self.assertIsNot(self.a_list, False) 122 123 # a_set = {0, 1, 2, 'n'} 124 # assert a_set is not False 125 # self.assertIsNot(a_set, False) 126 assert self.a_set is not False 127 self.assertIsNot(self.a_set, False) 128 129 # a_dictionary = {'key': 'value'} 130 # assert a_dictionary is not False 131 # self.assertIsNot(a_dictionary, False) 132 assert self.a_dictionary is not False 133 self.assertIsNot(self.a_dictionary, False) 134 135 def test_assertion_error_w_true(self):still green.
I use the class attribute to remove repetition of
a_dictionary = {'key': 'value'}from test_assertion_error_w_true135 def test_assertion_error_w_true(self): 136 assert None is not True 137 self.assertIsNot(None, True) 138 139 assert False is not True 140 self.assertIsNot(False, True) 141 142 assert True is True 143 self.assertIs(True, True) 144 145 # an_integer = 0 146 # assert an_integer is not True 147 # self.assertIsNot(an_integer, True) 148 assert self.an_integer is not True 149 self.assertIsNot(self.an_integer, True) 150 151 # a_float = 0.0 152 # assert a_float is not True 153 # self.assertIsNot(a_float, True) 154 assert self.a_float is not True 155 self.assertIsNot(self.a_float, True) 156 157 # a_string = 'a string' 158 # assert a_string is not True 159 # self.assertIsNot(a_string, True) 160 assert self.a_string is not True 161 self.assertIsNot(self.a_string, True) 162 163 # a_tuple = (0, 1, 2, 'n') 164 # assert a_tuple is not True 165 # self.assertIsNot(a_tuple, True) 166 assert self.a_tuple is not True 167 self.assertIsNot(self.a_tuple, True) 168 169 # a_list = [0, 1, 2, 'n'] 170 # assert a_list is not True 171 # self.assertIsNot(a_list, True) 172 assert self.a_list is not True 173 self.assertIsNot(self.a_list, True) 174 175 # a_set = {0, 1, 2, 'n'} 176 # assert a_set is not True 177 # self.assertIsNot(a_set, True) 178 assert self.a_set is not True 179 self.assertIsNot(self.a_set, True) 180 181 # a_dictionary = {'key': 'value'} 182 # assert a_dictionary is not True 183 # self.assertIsNot(a_dictionary, True) 184 assert self.a_dictionary is not True 185 self.assertIsNot(self.a_dictionary, True) 186 187 def test_assertion_error_w_equality(self):green.
I remove the commented lines from test_assertion_error_w_true
135 def test_assertion_error_w_true(self): 136 assert None is not True 137 self.assertIsNot(None, True) 138 139 assert False is not True 140 self.assertIsNot(False, True) 141 142 assert True is True 143 self.assertIs(True, True) 144 145 assert self.an_integer is not True 146 self.assertIsNot(self.an_integer, True) 147 148 assert self.a_float is not True 149 self.assertIsNot(self.a_float, True) 150 151 assert self.a_string is not True 152 self.assertIsNot(self.a_string, True) 153 154 assert self.a_tuple is not True 155 self.assertIsNot(self.a_tuple, True) 156 157 assert self.a_list is not True 158 self.assertIsNot(self.a_list, True) 159 160 assert self.a_set is not True 161 self.assertIsNot(self.a_set, True) 162 163 assert self.a_dictionary is not True 164 self.assertIsNot(self.a_dictionary, True) 165 166 def test_assertion_error_w_equality(self):I remove the commented lines from test_assertion_error_w_false
83 def test_assertion_error_w_false(self): 84 assert None is not False 85 self.assertIsNot(None, False) 86 87 assert False is False 88 self.assertIs(False, False) 89 90 assert True is not False 91 self.assertIsNot(True, False) 92 93 assert self.an_integer is not False 94 self.assertIsNot(self.an_integer, False) 95 96 assert self.a_float is not False 97 self.assertIsNot(self.a_float, False) 98 99 assert self.a_string is not False 100 self.assertIsNot(self.a_string, False) 101 102 assert self.a_tuple is not False 103 self.assertIsNot(self.a_tuple, False) 104 105 assert self.a_list is not False 106 self.assertIsNot(self.a_list, False) 107 108 assert self.a_set is not False 109 self.assertIsNot(self.a_set, False) 110 111 assert self.a_dictionary is not False 112 self.assertIsNot(self.a_dictionary, False) 113 114 def test_assertion_error_w_true(self):I remove the commented lines from test_assertion_error_w_none
31 def test_assertion_error_w_none(self): 32 assert None is None 33 self.assertIs(None, None) 34 35 assert False is not None 36 self.assertIsNot(False, None) 37 38 assert True is not None 39 self.assertIsNot(True, None) 40 41 assert self.an_integer is not None 42 self.assertIsNot(self.an_integer, None) 43 44 assert self.a_float is not None 45 self.assertIsNot(self.a_float, None) 46 47 assert self.a_string is not None 48 self.assertIsNot(self.a_string, None) 49 50 assert self.a_tuple is not None 51 self.assertIsNot(self.a_tuple, None) 52 53 assert self.a_list is not None 54 self.assertIsNot(self.a_list, None) 55 56 assert self.a_set is not None 57 self.assertIsNot(self.a_set, None) 58 59 assert self.a_dictionary is not None 60 self.assertIsNot(self.a_dictionary, None) 61 62 def test_assertion_error_w_false(self):
In this case, I do not need the setUp method because the class attributes are the same for every test and I do not need anything to run before each test. I move them out
4class TestAssertionError(unittest.TestCase): 5 6 self.an_integer = 0 7 self.a_float = 0.0 8 self.a_string = 'a string' 9 self.a_tuple = (0, 1, 2, 'n') 10 self.a_list = [0, 1, 2, 'n'] 11 self.a_set = {0, 1, 2, 'n'} 12 self.a_dictionary = {'key': 'value'} 13 14 # def setUp(self): 15 # self.an_integer = 0 16 # self.a_float = 0.0 17 # self.a_string = 'a string' 18 # self.a_tuple = (0, 1, 2, 'n') 19 # self.a_list = [0, 1, 2, 'n'] 20 # self.a_set = {0, 1, 2, 'n'} 21 # self.a_dictionary = {'key': 'value'} 22 23 def test_assert_keyword(self):the terminal is my friend, and shows NameError
NameError: name 'self' is not definedbecause
selfis not defined outside the methods I can declare the class attributes the same way I do variables as long as it is indented under the class definitionI add NameError to the list of Exceptions
200# Exceptions seen 201# AssertionError 202# NameErrorI remove
self.4class TestAssertionError(unittest.TestCase): 5 6 # self.an_integer = 0 7 # self.a_float = 0.0 8 # self.a_string = 'a string' 9 # self.a_tuple = (0, 1, 2, 'n') 10 # self.a_list = [0, 1, 2, 'n'] 11 # self.a_set = {0, 1, 2, 'n'} 12 # self.a_dictionary = {'key': 'value'} 13 14 an_integer = 0 15 a_float = 0.0 16 a_string = 'a string' 17 a_tuple = (0, 1, 2, 'n') 18 a_list = [0, 1, 2, 'n'] 19 a_set = {0, 1, 2, 'n'} 20 a_dictionary = {'key': 'value'} 21 22 # def setUp(self): 23 # self.an_integer = 0 24 # self.a_float = 0.0 25 # self.a_string = 'a string' 26 # self.a_tuple = (0, 1, 2, 'n') 27 # self.a_list = [0, 1, 2, 'n'] 28 # self.a_set = {0, 1, 2, 'n'} 29 # self.a_dictionary = {'key': 'value'} 30 31 def test_assert_keyword(self):the test is green again.
I remove the commented lines
4class TestAssertionError(unittest.TestCase): 5 6 an_integer = 0 7 a_float = 0.0 8 a_string = 'a string' 9 a_tuple = (0, 1, 2, 'n') 10 a_list = [0, 1, 2, 'n'] 11 a_set = {0, 1, 2, 'n'} 12 a_dictionary = {'key': 'value'} 13 14 def test_assert_keyword(self):I add a git commit message in the other terminal
git commit -am 'extract class attributes'the terminal shows a summary of the changes then goes back to the command line.
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 class attributes for things that repeat, which allows methods of the same class to use them.
code from the chapter
what is next?
As a reminder, you know
Would you like to test what happens when classes have one or more parents?
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.