test unittest with lists
I want to use a list like I did in test_dir_list with extend and sort to make test_dir_unittest and test_dir_unittest_testcase simpler.
preview
I have these tests by the end of the chapter
open the project
I open a terminal
I change directory to the project
cd unittestI open
test_unittest.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --now --patterns "**/*.py"
test_dir_unittest_testcase with extend and sort
The attributes and methods of the unittest.TestCase object are the attributes and methods of the unittest object with extra things. I can make the attributes and methods of the unittest.TestCase object by adding the “extra” to the attributes and methods of the unittest object.
RED: make it fail
I add a variable for the attributes and methods of the unittest.TestCase object, then add an assertion for it, in test_dir_unittest
28 def test_dir_unittest_testcase(self):
29 dir_unittest_testcase = dir(unittest)
30 self.assertEqual(
31 dir(unittest.TestCase),
32 dir_unittest_testcase
33 )
34
35 reality = dir(unittest.TestCase)
the terminal is my friend, and shows AssertionError
GREEN: make it pass
I use extend to append the additional elements
165 def test_dir_unittest(self): 166 dir_unittest = dir(object) 167 dir_unittest.extend(( 168 '__dict__', '__firstlineno__', 169 '__module__', '__static_attributes__', 170 '__weakref__', 171 'can_get_license', 'can_vote', 172 'check_age', 'say_hello', 173 )) 174 self.assertEqual( 175 dir(src.unittest.Person), 176 dir_unittest 177 )the terminal still shows AssertionError.
I add a call to the sort method
167 dir_unittest.extend(( 168 '__dict__', '__firstlineno__', 169 '__module__', '__static_attributes__', 170 '__weakref__', 171 'can_get_license', 'can_vote', 172 'check_age', 'say_hello', 173 )) 174 dir_unittest.sort() 175 self.assertEqual( 176 dir(src.unittest.Person), 177 dir_unittest 178 ) 179 180 self.assertEqual( 181 dir(src.unittest.Person), 182 [ 183 '__class__', '__delattr__', '__dict__', 184 '__dir__', '__doc__', '__eq__',the test passes because all objects inherit from ‘object’.
REFACTOR: make it better
I remove the other assertion from test_dir_unittest
I add a git commit message
test_dir_unittest_instance with extend and sort
The attributes and methods of an instance of the Person object are the attributes and methods of the unittest object with extra. Since they are both lists I can make the attributes and methods of an instance of the Person object by adding the “extra” to the attributes and methods of the unittest object.
RED: make it fail
I add a variable for the attributes and methods of an instance of the Person object, then add an assertion for it
180 def test_dir_unittest_instance(self):
181 dir_unittest_instance = dir(src.unittest.Person)
182 self.assertEqual(
183 dir(
184 src.unittest.Person(
185 first_name='first_name',
186 last_name='last_name',
187 sex='M',
188 year_of_birth=2026,
189 )
190 ),
191 dir_unittest_instance
192 )
193
194 self.assertEqual(
195 dir(
196 src.unittest.Person(
197 first_name='first_name',
the terminal is my friend, and shows AssertionError
AssertionError: Lists differ: [
'__c[396 chars]_', 'age', 'can_get_license',
'can_vote', 'che[96 chars]rth'
] != [
'__c[396 chars]_', 'can_get_license', 'can_vote',
'check_age', 'say_hello'
]
with a note about the difference
GREEN: make it pass
I add
self.maxDiff = None180 def test_dir_unittest_instance(self): 181 self.maxDiff = None 182 dir_unittest_instance = dir(src.unittest.Person)the terminal is my friend, and shows the difference.
I use extend to append the seven additional elements
180 def test_dir_unittest_instance(self): 181 self.maxDiff = None 182 dir_unittest_instance = dir(src.unittest.Person) 183 dir_unittest_instance.extend(( 184 'age', 'first_name', 'is_citizen', 185 'last_name', 'passed_test', 186 'sex', 'year_of_birth', 187 )) 188 self.assertEqual( 189 dir( 190 src.unittest.Person( 191 first_name='first_name', 192 last_name='last_name', 193 sex='M', 194 year_of_birth=2026, 195 ) 196 ), 197 dir_unittest_instance 198 )the terminal still shows AssertionError.
I add a call to the sort method
183 dir_unittest_instance.extend(( 184 'age', 'first_name', 'is_citizen', 185 'last_name', 'passed_test', 186 'sex', 'year_of_birth', 187 )) 188 dir_unittest_instance.sort() 189 self.assertEqual( 190 dir( 191 src.unittest.Person( 192 first_name='first_name', 193 last_name='last_name', 194 sex='M', 195 year_of_birth=2026, 196 ) 197 ), 198 dir_unittest_instance 199 ) 200 201 self.assertEqual( 202 dir( 203 src.unittest.Person( 204 first_name='first_name',the test passes because all objects inherit from ‘object’.
REFACTOR: make it better
close the project
I close
test_unittest.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests
I change directory to the parent of
listscd ..
review
code from the chapter
Do you want to see all the CODE I typed in this chapter?
what is next?
I know how to make a Python Test Driven Development environment manually.
I know how to make a unittest with strings.
I know how to make a unittest say hello with f-strings.
I know how to make a unittest with an object.
I know how to use the datetime library.
I know how to make a unittest with conditions.
I know how to make a Python Test Driven Development environment automatically.
I know how to make a Python Test Driven Development environment automatically with variables.
I know how to make a unittest with Exceptions.
I know how to make a unittest with loops.
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.