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 unittest
    
  • I open test_unittest.py from the tests folder

  • I 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

unittest/tests/test_unittest.py
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

terminal
AssertionError: Lists differ: [
    '__call__', '__class__', '__delattr__', '_[1726 chars]ass'
] != [
    'BaseTestSuite', 'FunctionTestCase', 'Isol[578 chars]til'
]

GREEN: make it pass


  • I set maxDiff to None

    unittest/tests/test_unittest.py
    28    def test_dir_unittest_testcase(self):
    29        self.maxDiff = None
    30        dir_unittest_testcase = dir(unittest)
    
  • I use extend to append the additional elements

    unittest/tests/test_unittest.py
    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

    unittest/tests/test_unittest.py
    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

    unittest/tests/test_unittest.py
    174        dir_unittest.sort()
    175        self.assertEqual(
    176            dir(src.unittest.Person),
    177            dir_unittest
    178        )
    179
    180    def test_dir_unittest_instance(self):
    
  • I add a git commit message

    terminal
    git commit -am \
    'test_dir_unittest with extend and sort'
    

All objects inherit attributes and methods from ‘object’.


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

unittest/tests/test_unittest.py
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

terminal
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

terminal
Diff is 718 characters long. Set self.maxDiff to None to see it.

GREEN: make it pass


  • I add self.maxDiff = None

    unittest/tests/test_unittest.py
    180    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

    unittest/tests/test_unittest.py
    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

    unittest/tests/test_unittest.py
    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


  • I remove the other assertion from test_dir_unittest_instance

    unittest/tests/test_unittest.py
    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
    202# Exceptions seen
    
  • I add a git commit message

    terminal
    git commit -am \
    'test_dir_unittest_instance with extend and sort'
    

All objects inherit attributes and methods from ‘object’.


close the project

  • I close test_unittest.py

  • I 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 lists

    cd ..
    

review

All objects inherit attributes and methods from ‘object’.


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to test list comprehensions?


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.