test objects with unittest

The object class is the mother of all things in Python. I think of classes_ as attributes (variables) and methods (functions) that belong together.


questions about classes

Questions to think about as I go through the chapter


preview

I have these tests by the end of the chapter

  1import src.classes
  2import unittest
  3
  4
  5class TestClasses(unittest.TestCase):
  6
  7    def test_making_a_class_w_pass(self):
  8        an_instance = src.classes.WPass()
  9        assert isinstance(an_instance, object)
 10        self.assertIsInstance(an_instance, object)
 11
 12        a_class = src.classes.WPass
 13        assert issubclass(a_class, object)
 14        self.assertIsSubclass(a_class, object)
 15
 16    def test_making_a_class_w_parentheses(self):
 17        an_instance = src.classes.WParentheses()
 18        assert isinstance(an_instance, object)
 19        self.assertIsInstance(an_instance, object)
 20
 21        a_class = src.classes.WParentheses
 22        assert issubclass(a_class, object)
 23        self.assertIsSubclass(a_class, object)
 24
 25    def test_making_a_class_w_object(self):
 26        an_instance = src.classes.WObject()
 27        assert isinstance(an_instance, object)
 28        self.assertIsInstance(an_instance, object)
 29
 30        a_class = src.classes.WObject
 31        assert issubclass(a_class, object)
 32        self.assertIsSubclass(a_class, object)
 33
 34    def test_is_none_an_object(self):
 35        assert isinstance(None, object)
 36        self.assertIsInstance(None, object)
 37
 38    def test_is_a_boolean_an_object(self):
 39        assert issubclass(bool, object)
 40        self.assertIsSubclass(bool, object)
 41
 42    def test_is_an_integer_an_object(self):
 43        assert issubclass(int, object)
 44        self.assertIsSubclass(int, object)
 45
 46    def test_is_a_float_an_object(self):
 47        assert issubclass(float, object)
 48        self.assertIsSubclass(float, object)
 49
 50    def test_is_a_string_an_object(self):
 51        assert issubclass(str, object)
 52        self.assertIsSubclass(str, object)
 53
 54    def test_is_a_tuple_an_object(self):
 55        assert issubclass(tuple, object)
 56        self.assertIsSubclass(tuple, object)
 57
 58    def test_is_a_list_an_object(self):
 59        assert issubclass(list, object)
 60        self.assertIsSubclass(list, object)
 61
 62    def test_is_a_set_an_object(self):
 63        assert issubclass(set, object)
 64        self.assertIsSubclass(set, object)
 65
 66    def test_is_a_dictionary_an_object(self):
 67        assert issubclass(dict, object)
 68        self.assertIsSubclass(dict, object)
 69
 70    def test_dir_object(self):
 71        reality = dir(object)
 72        my_expectation = [
 73            '__class__',
 74            '__delattr__',
 75            '__dir__',
 76            '__doc__',
 77            '__eq__',
 78            '__format__',
 79            '__ge__',
 80            '__getattribute__',
 81            '__getstate__',
 82            '__gt__',
 83            '__hash__',
 84            '__init__',
 85            '__init_subclass__',
 86            '__le__',
 87            '__lt__',
 88            '__ne__',
 89            '__new__',
 90            '__reduce__',
 91            '__reduce_ex__',
 92            '__repr__',
 93            '__setattr__',
 94            '__sizeof__',
 95            '__str__',
 96            '__subclasshook__'
 97        ]
 98        self.assertEqual(reality, my_expectation)
 99
100
101# Exceptions seen
102# AssertionError
103# NameError
104# AttributeError
105# TypeError

start the project

  • I name this project classes

  • I open a terminal

  • I change directory to the classes folder in the pumping_python folder

    cd classes
    

    the terminal shows

    cd: no such file or directory: classes
    
  • I use uv to make a directory for the project and initialize it

    uv init classes
    

    the terminal shows

    Initialized project `classes`
    at `.../pumping_python/classes`
    
  • I change directory to classes

    cd classes
    

    the terminal shows I am in the classes folder

    .../pumping_python/classes
    
  • I make a directory for the tests

    mkdir tests
    
  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    
    New-Item tests/__init__.py
    
  • I use the mv program to change the name of main.py to test_classes.py and move it to the tests folder

    mv main.py tests/test_classes.py
    
    Move-Item main.py tests/test_classes.py
    
  • I open test_classes.py

  • I delete the text in the file then add the first failing test to test_classes.py

    1def test_failure():
    2    assert False is True
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    
  • I add the new files and folder to git for tracking

    git add .
    
  • I add a git commit message

    git commit -am 'setup project'
    
  • I use pytest-watcher to run the tests

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES ========================
    ______________________ test_failure ______________________
    
        def test_failure():
    >       assert False is True
    E       assert False is True
    
    test_classes.py:2: AssertionError
    ================ short test summary info =================
    FAILED test_classes.py::test_failure - assert False is True
    =================== 1 failed in X.YZs ====================
    

    because True is NOT False.

    if the terminal does not show the same error, then check

    • if your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • if you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now again

  • I add AssertionError to the list of Exceptions seen in test_classes.py

    1def test_failure():
    2    assert False is True
    3
    4
    5# Exceptions seen
    6# AssertionError
    
  • I change False to True in the assertion

    1def test_failure():
    2    # assert False is True
    3    assert True is True
    4
    5
    6# Exceptions seen
    7# AssertionError
    

    the test passes.


test_making_a_class_w_pass

To review, I can make a class with the class keyword, use CapWords format for the name and use a name that tells what the group of attributes and methods do.

class NameOfClass(ParentClass):

    attribute = SOMETHING

    def method():
        the body of the method
        ...

RED: make it fail


how to test if something is NOT an instance


I can test if an object is an instance (a copy) of another object or NOT with the `isinstance built-in function`_ from The Python Standard Library.

isinstance_ checks if the thing in the parentheses on the left is an instance (a copy) of the class on the right in the parentheses.


GREEN: make it pass


  • I add an import statement for the classes module

    1import src.classes
    2import unittest
    3
    4
    5class TestClasses(unittest.TestCase):
    
    • import src.classes brings in an object for the classes.py module from the src folder so I can use it in test_classes.py

    • the terminal is my friend, and shows AttributeError

      AttributeError: module 'src.classes'
                      has no attribute 'WPass'
      

      because there is no definition for WPass in classes.py

  • I add AttributeError to the list of Exceptions seen

    13# Exceptions seen
    14# AssertionError
    15# NameError
    16# AttributeError
    
  • I open classes.py from the src folder

  • then I add a class definition for WPass to classes.py

    1class WPass: pass
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert not True
    

    because the statement not isinstance(src.classes.WPass(), object) is False.


how to test if something is an instance



REFACTOR: make it better


The `unittest.TestCase class`_ has two methods I can also use to test if an object is an instance (a copy) of a class or NOT - assertIsInstance_ and assertNotIsInstance_


another way to test if something is NOT an instance


I add the `assertNotIsInstance method`_ to the test

 7    def test_making_a_class_w_pass(self):
 8        # assert not isinstance(
 9        assert isinstance(
10            src.classes.WPass(), object
11        )
12        self.assertNotIsInstance(
13            src.classes.WPass(), object
14        )
15
16
17# Exceptions seen

the terminal is my friend, and shows AssertionError

AssertionError:
    <src.classes.WPass() object at 0xffff8a7b6543>
    is an instance of <class 'object'>

another way to test if something is an instance


  • I change assertNotIsInstance_ to assertIsInstance_

     7    def test_making_a_class_w_pass(self):
     8        # assert not isinstance(
     9        assert isinstance(
    10            src.classes.WPass(), object
    11        )
    12        # self.assertNotIsInstance(
    13        self.assertIsInstance(
    14            src.classes.WPass(), object
    15        )
    16
    17
    18# Exceptions seen
    

    the test passes.

  • I add a variable

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        # assert not isinstance(
    10        assert isinstance(
    11            src.classes.WPass(), object
    12        )
    13        # self.assertNotIsInstance(
    14        self.assertIsInstance(
    15            src.classes.WPass(), object
    16        )
    17
    18
    19# Exceptions seen
    
  • I use the variable to remove repetition of src.classes.WPass()

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11        # assert not isinstance(
    12        # assert isinstance(
    13        #     src.classes.WPass(), object
    14        # )
    15        # self.assertNotIsInstance(
    16        # self.assertIsInstance(
    17        #     src.classes.WPass(), object
    18        # )
    19
    20
    21# Exceptions seen
    

    the test is still green.

  • I remove the commented lines

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11
    12
    13# Exceptions seen
    
  • I open a new terminal, then add a git commit message

    git commit -am \
    'add test_making_a_class_w_pass'
    

I can make a class with pass


test_making_a_class_w_parentheses

I can also make a class with parentheses/brackets ( ).


RED: make it red


  • I go back to the terminal where the tests are running

  • I add another test

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11
    12    def test_making_a_class_w_parentheses(self):
    13        assert not isinstance(
    14            src.classes.WParentheses(), object
    15        )
    16
    17
    18# Exceptions seen
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.classes'
                    has no attribute 'WParentheses'
    

GREEN: make it pass


  • I add a class definition like WPass to classes.py

    1class WPass: pass
    2
    3
    4class WParentheses: pass
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert not True
    

    because the statement not isinstance(src.classes.WParentheses(), object) is False.

  • I change the assertion in test_making_a_class_w_parentheses to make the statement True, in test_classes.py

    12    def test_making_a_class_w_parentheses(self):
    13        # assert not isinstance(
    14        assert isinstance(
    15            src.classes.WParentheses(), object
    16        )
    17
    18
    19# Exceptions seen
    

    the test passes.


REFACTOR: make it better


  • I add parentheses to the definition of WParentheses in classes.py

    4# class WParentheses: pass
    5class WParentheses(): pass
    
  • I remove the commented line

    1class WPass: pass
    2
    3
    4class WParentheses(): pass
    
  • I add the `assertNotIsInstance method`_ to test_making_a_class_w_parentheses in test_classes.py

    12    def test_making_a_class_w_parentheses(self):
    13        # assert not isinstance(
    14        assert isinstance(
    15            src.classes.WParentheses(), object
    16        )
    17        self.assertNotIsInstance(
    18            src.classes.WParentheses(), object
    19        )
    20
    21
    22# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <src.classes.WParentheses() object at 0xffffab123456>
        is an instance of <class 'object'>
    
  • I change assertNotIsInstance_ to assertIsInstance_

    12    def test_making_a_class_w_parentheses(self):
    13        # assert not isinstance(
    14        assert isinstance(
    15            src.classes.WParentheses(), object
    16        )
    17        # self.assertNotIsInstance(
    18        self.assertIsInstance(
    19            src.classes.WParentheses(), object
    20        )
    21
    22
    23# Exceptions seen
    

    the test passes.

  • I add a variable

    12    def test_making_a_class_w_parentheses(self):
    13        an_instance = src.classes.WParentheses()
    14        # assert not isinstance(
    15        assert isinstance(
    16            src.classes.WParentheses(), object
    17        )
    18        # self.assertNotIsInstance(
    19        self.assertIsInstance(
    20            src.classes.WParentheses(), object
    21        )
    22
    23
    24# Exceptions seen
    
  • I use the variable to remove repetition of src.classes.WParentheses()

    12    def test_making_a_class_w_parentheses(self):
    13        an_instance = src.classes.WParentheses()
    14        assert isinstance(an_instance, object)
    15        self.assertIsInstance(an_instance, object)
    16        # assert not isinstance(
    17        # assert isinstance(
    18        #     src.classes.WParentheses(), object
    19        # )
    20        # self.assertNotIsInstance(
    21        # self.assertIsInstance(
    22        #     src.classes.WParentheses(), object
    23        # )
    24
    25
    26# Exceptions seen
    

    the test is still green.

  • I remove the commented lines

    12    def test_making_a_class_w_parentheses(self):
    13        an_instance = src.classes.WParentheses()
    14        assert isinstance(an_instance, object)
    15        self.assertIsInstance(an_instance, object)
    16
    17
    18# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_making_a_class_w_parentheses'
    

I can make a class with parentheses

I have two classes with different statements, and the tests show that they are both instances of the object class

class WPass: pass
class WParentheses(): pass

because all classes inherit from ‘object’, which leads me to the next test.


test_making_a_class_w_object

I can make a class with object (the mother of all classes).


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with an assertion for a new class

    12    def test_making_a_class_w_parentheses(self):
    13        an_instance = src.classes.WParentheses()
    14        assert isinstance(an_instance, object)
    15        self.assertIsInstance(an_instance, object)
    16
    17    def test_making_a_class_w_object(self):
    18        assert not isinstance(
    19            src.classes.WObject(), object
    20        )
    21
    22
    23# Exceptions seen
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.classes' has no attribute 'WObject'
    

GREEN: make it pass


  • I add the class definition to classes.py

    4class WParentheses(): pass
    5
    6
    7class WObject(): pass
    

    the terminal is my friend, and shows AssertionError

    AssertionError: assert not True
    
  • I change the statement of the assertion in test_making_a_class_w_object to make it True

    17    def test_making_a_class_w_object(self):
    18        # assert not isinstance(
    19        assert isinstance(
    20            src.classes.WObject(), object
    21        )
    22
    23
    24# Exceptions seen
    

    the test passes.


REFACTOR: make it better


  • I add object to the parentheses of the class definition for WObject in classes.py

    7# class WObject(): pass
    8class WObject(object): pass
    

    the test is still green.

  • I remove the commented line

    4class WParentheses(): pass
    5
    6
    7class WObject(object): pass
    
  • I add an assertion with the `assertNotIsInstance method`_ to test_making_a_class_w_object in test_classes.py

    17    def test_making_a_class_w_object(self):
    18        # assert not isinstance(
    19        assert isinstance(
    20            src.classes.WObject(), object
    21        )
    22        self.assertNotIsInstance(
    23            src.classes.WObject(), object
    24        )
    25
    26
    27# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <src.classes.WObject() object at 0xffffcd781234>
        is an instance of <class 'object'>
    
  • I change assertNotIsInstance_ to assertIsInstance_

    17    def test_making_a_class_w_object(self):
    18        # assert not isinstance(
    19        assert isinstance(
    20            src.classes.WObject(), object
    21        )
    22        # self.assertNotIsInstance(
    23        self.assertIsInstance(
    24            src.classes.WObject(), object
    25        )
    26
    27
    28# Exceptions seen
    

    the test passes.

  • I add a variable to remove repetition of src.classes.WObject()

    17    def test_making_a_class_w_object(self):
    18        an_instance = src.classes.WObject()
    19        # assert not isinstance(
    20        assert isinstance(
    21            src.classes.WObject(), object
    22        )
    23        # self.assertNotIsInstance(
    24        self.assertIsInstance(
    25            src.classes.WObject(), object
    26        )
    27
    28
    29# Exceptions seen
    
  • I use the variable to remove repetition of src.classes.WObject()

    17    def test_making_a_class_w_object(self):
    18        an_instance = src.classes.WObject()
    19        assert isinstance(an_instance, object)
    20        self.assertIsInstance(an_instance, object)
    21        # assert not isinstance(
    22        # assert isinstance(
    23        #     src.classes.WObject(), object
    24        # )
    25        # self.assertNotIsInstance(
    26        # self.assertIsInstance(
    27        #     src.classes.WObject(), object
    28        # )
    29
    30
    31# Exceptions seen
    

    the test is still green.

  • I remove the commented lines

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11
    12    def test_making_a_class_w_parentheses(self):
    13        an_instance = src.classes.WParentheses()
    14        assert isinstance(an_instance, object)
    15        self.assertIsInstance(an_instance, object)
    16
    17    def test_making_a_class_w_object(self):
    18        an_instance = src.classes.WObject()
    19        assert isinstance(an_instance, object)
    20        self.assertIsInstance(an_instance, object)
    21
    22
    23# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_making_a_class_w_object'
    

I have three different classes, and the tests show that they are all instances of the object class

class WPass: pass
class WParentheses(): pass
class WObject(object): pass

their definitions are different, their results are the same because all classes inherit from ‘object’.

I like to write my classes with (object), so that anyone can see what the parent class is without thinking about it.

I can make a class with object.


test_is_none_an_object

I want to test if None is an object.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with an assertion

    17    def test_making_a_class_w_object(self):
    18        an_instance = src.classes.WObject()
    19        assert isinstance(an_instance, object)
    20        self.assertIsInstance(an_instance, object)
    21
    22    def test_is_none_an_object(self):
    23        assert not isinstance(None, object)
    24
    25
    26# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because None is an object.


GREEN: make it pass


I change the statement to make it True

22    def test_is_none_an_object(self):
23        # assert not isinstance(None, object)
24        assert isinstance(None, object)
25
26
27# Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsInstance_ to show that None is a child of object

    22    def test_is_none_an_object(self):
    23        # assert not isinstance(None, object)
    24        assert isinstance(None, object)
    25        self.assertNotIsInstance(None, object)
    26
    27
    28# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is an instance of <class 'object'>
    

    because None is an object.

  • I change assertNotIsInstance_ to assertIsInstance_

    22    def test_is_none_an_object(self):
    23        # assert not isinstance(None, object)
    24        assert isinstance(None, object)
    25        # self.assertNotIsInstance(None, object)
    26        self.assertIsInstance(None, object)
    27
    28
    29# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    22    def test_is_none_an_object(self):
    23        assert isinstance(None, object)
    24        self.assertIsInstance(None, object)
    25
    26
    27# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_an_object'
    

test_is_a_boolean_an_object

I want to test if a boolean is an object.


RED: make it fail


how to test if something is NOT a subclass


I can test if an object is a subclass (child) of another object or NOT with the `issubclass built-in function`_ from The Python Standard Library.

issubclass_ checks if the thing in the parentheses on the left is a subclass of the class on the right in the parentheses.


GREEN: make it pass


how to test if something is a subclass


I change the statement to make it True

26    def test_is_a_boolean_an_object(self):
27        # assert not issubclass(bool, object)
28        assert issubclass(bool, object)
29
30
31# Exceptions seen

the test passes.


REFACTOR: make it better


The `unittest.TestCase class`_ has 2 methods I can also use to test if an object is a subclass (child) of a class or NOT - assertIsSubclass_ and assertNotIsSubclass_.


another way to test if something is NOT a subclass


I use `unittest.TestCase.assertNotIsSubclass`_ to show that bool (the class for booleans) is a child of object

26    def test_is_a_boolean_an_object(self):
27        # assert not issubclass(bool, object)
28        assert issubclass(bool, object)
29        self.assertNotIsSubclass(bool, object)
30
31
32# Exceptions seen

the terminal is my friend, and shows AssertionError

AssertionError:
    <class 'bool'> is a subclass of <class 'object'>

because bool is a child of object.


another way to test if something is a subclass


  • I change assertNotIsSubclass_ to assertIsSubclass_

    26    def test_is_a_boolean_an_object(self):
    27        # assert not issubclass(bool, object)
    28        assert issubclass(bool, object)
    29        # self.assertNotIsSubclass(bool, object)
    30        self.assertIsSubclass(bool, object)
    31
    32
    33# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    26    def test_is_a_boolean_an_object(self):
    27        assert issubclass(bool, object)
    28        self.assertIsSubclass(bool, object)
    29
    30
    31# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_boolean_an_object'
    

A boolean is an object.


test_is_an_integer_an_object

I want to test if an integer (a whole number without decimals) is an object.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with an assertion for int (the class for whole numbers without decimals), to show that everything in Python is a child of object.

    26    def test_is_a_boolean_an_object(self):
    27        assert issubclass(bool, object)
    28        self.assertIsSubclass(bool, object)
    29
    30    def test_is_an_integer_an_object(self):
    31        assert not issubclass(int, object)
    32
    33
    34# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because int is a child of object.


GREEN: make it pass


I change the statement to make it True

30    def test_is_an_integer_an_object(self):
31        # assert not issubclass(int, object)
32        assert issubclass(int, object)
33
34
35# Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsSubclass_ to show that int is a child of object

    30    def test_is_an_integer_an_object(self):
    31        # assert not issubclass(int, object)
    32        assert issubclass(int, object)
    33        self.assertNotIsSubclass(int, object)
    34
    35
    36# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'int'> is a subclass of <class 'object'>
    

    because int is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    30    def test_is_an_integer_an_object(self):
    31        # assert not issubclass(int, object)
    32        assert issubclass(int, object)
    33        # self.assertNotIsSubclass(int, object)
    34        self.assertIsSubclass(int, object)
    35
    36
    37# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    30    def test_is_an_integer_an_object(self):
    31        assert issubclass(int, object)
    32        self.assertIsSubclass(int, object)
    33
    34
    35# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_an_integer_an_object'
    

An integer is an object.


test_is_a_float_an_object

I want to test if a float (a binary floating point decimal number) is an object


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with an assertion for float (the class for binary floating point decimal numbers), to show that everything in Python is a child of object.

    30    def test_is_an_integer_an_object(self):
    31        assert issubclass(int, object)
    32        self.assertIsSubclass(int, object)
    33
    34    def test_is_a_float_an_object(self):
    35        assert not issubclass(float, object)
    36
    37
    38# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because float is a child of object.


GREEN: make it pass


I change the statement to make it True

34    def test_is_a_float_an_object(self):
35        # assert not issubclass(float, object)
36        assert issubclass(float, object)
37
38
39# Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsSubclass_ to show that float is a child of object

    34    def test_is_a_float_an_object(self):
    35        # assert not issubclass(float, object)
    36        assert issubclass(float, object)
    37        self.assertNotIsSubclass(float, object)
    38
    39
    40# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'float'> is a subclass of <class 'object'>
    

    because float is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    34    def test_is_a_float_an_object(self):
    35        # assert not issubclass(float, object)
    36        assert issubclass(float, object)
    37        # self.assertNotIsSubclass(float, object)
    38        self.assertIsSubclass(float, object)
    39
    40
    41# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    34    def test_is_a_float_an_object(self):
    35        assert issubclass(float, object)
    36        self.assertIsSubclass(float, object)
    37
    38
    39# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_float_an_object'
    

A float is an object.


test_is_a_string_an_object

I want to test if a string (anything in quotes) is an object.


RED: make it fail



GREEN: make it pass


I change the statement to make it True

38    def test_is_a_string_an_object(self):
39        # assert not issubclass(str, object)
40        assert issubclass(str, object)
41
42
43# Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsSubclass_ to show that str is a child of object

    38    def test_is_a_string_an_object(self):
    39        # assert not issubclass(str, object)
    40        assert issubclass(str, object)
    41        self.assertNotIsSubclass(str, object)
    42
    43
    44# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'str'> is a subclass of <class 'object'>
    

    because str is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    38    def test_is_a_string_an_object(self):
    39        # assert not issubclass(str, object)
    40        assert issubclass(str, object)
    41        # self.assertNotIsSubclass(str, object)
    42        self.assertIsSubclass(str, object)
    43
    44
    45# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    38    def test_is_a_string_an_object(self):
    39        assert issubclass(str, object)
    40        self.assertIsSubclass(str, object)
    41
    42
    43# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_string_an_object'
    

A string is an object.


test_is_a_tuple_an_object

I want to test if a tuple (anything in parentheses ( ) separated by a comma) is an object.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with an assertion for tuple (the class for anything in parentheses ( ) separated by a comma), to show that in Python everything is an object

    38    def test_is_a_string_an_object(self):
    39        assert issubclass(str, object)
    40        self.assertIsSubclass(str, object)
    41
    42    def test_is_a_tuple_an_object(self):
    43        assert not issubclass(tuple, object)
    44
    45
    46# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because tuple is a child of object.


GREEN: make it pass


I change the statement to make it True

42    def test_is_a_tuple_an_object(self):
43        # assert not issubclass(tuple, object)
44        assert issubclass(tuple, object)
45
46
47# Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsSubclass_ to show that tuple is a child of object

    42    def test_is_a_tuple_an_object(self):
    43        # assert not issubclass(tuple, object)
    44        assert issubclass(tuple, object)
    45        self.assertNotIsSubclass(tuple, object)
    46
    47
    48# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'tuple'> is a subclass of <class 'object'>
    

    because tuple is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    42    def test_is_a_tuple_an_object(self):
    43        # assert not issubclass(tuple, object)
    44        assert issubclass(tuple, object)
    45        # self.assertNotIsSubclass(tuple, object)
    46        self.assertIsSubclass(tuple, object)
    47
    48
    49# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    42    def test_is_a_tuple_an_object(self):
    43        assert issubclass(tuple, object)
    44        self.assertIsSubclass(tuple, object)
    45
    46
    47# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_tuple_an_object'
    

A tuple is an object.


test_is_a_list_an_object

I want to test if a list (anything in square brackets [ ]) is an object.


RED: make it fail



GREEN: make it pass


I change the statement to make it True

46      def test_is_a_list_an_object(self):
47          # assert not issubclass(list, object)
48          assert issubclass(list, object)
49
50
51  # Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsSubclass_ to show that list is a child of object

    46    def test_is_a_list_an_object(self):
    47        # assert not issubclass(list, object)
    48        assert issubclass(list, object)
    49        self.assertNotIsSubclass(list, object)
    50
    51
    52# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'list'> is a subclass of <class 'object'>
    

    because list is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    46    def test_is_a_list_an_object(self):
    47        # assert not issubclass(list, object)
    48        assert issubclass(list, object)
    49        # self.assertNotIsSubclass(list, object)
    50        self.assertIsSubclass(list, object)
    51
    52
    53# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    46    def test_is_a_list_an_object(self):
    47        assert issubclass(list, object)
    48        self.assertIsSubclass(list, object)
    49
    50
    51# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_a_list_an_object'
    

A list is an object.


test_is_a_set_an_object

I want to test if a set (anything in curly braces { }, not key-value pairs) is an object.


RED: make it fail



GREEN: make it pass


I change the statement to make it True

50    def test_is_a_set_an_object(self):
51        # assert not issubclass(set, object)
52        assert issubclass(set, object)
53
54
55# Exceptions seen

the test passes.


REFACTOR: make it better


  • I use assertNotIsSubclass_ to show that set is a child of object

    50    def test_is_a_set_an_object(self):
    51        # assert not issubclass(set, object)
    52        assert issubclass(set, object)
    53        self.assertNotIsSubclass(set, object)
    54
    55
    56# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'set'> is a subclass of <class 'object'>
    

    because set is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    50    def test_is_a_set_an_object(self):
    51        # assert not issubclass(set, object)
    52        assert issubclass(set, object)
    53        # self.assertNotIsSubclass(set, object)
    54        self.assertIsSubclass(set, object)
    55
    56
    57# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    50    def test_is_a_set_an_object(self):
    51        assert issubclass(set, object)
    52        self.assertIsSubclass(set, object)
    53
    54
    55# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_a_set_an_object'
    

A set is an object.


test_is_a_dictionary_an_object

I want to test if a dictionary (any key-value pairs in curly braces { } separated by commas) is an object.



RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test with an assertion for dict (the class for key-value pairs in curly braces ‘{ }’ separated by commas), to show that in Python everything is an object

    50    def test_is_a_set_an_object(self):
    51        assert issubclass(set, object)
    52        self.assertIsSubclass(set, object)
    53
    54    def test_is_a_dictionary_an_object(self):
    55        assert not issubclass(dict, object)
    56
    57
    58# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    E       assert not True
    

    because dict is a child of object.

  • I change the statement to make it True

    54    def test_is_a_dictionary_an_object(self):
    55        # assert not issubclass(dict, object)
    56        assert issubclass(dict, object)
    57
    58
    59# Exceptions seen
    

    the test passes.

  • I use assertNotIsSubclass_ to show that dict is a child of object

    54    def test_is_a_dictionary_an_object(self):
    55        # assert not issubclass(dict, object)
    56        assert issubclass(dict, object)
    57        self.assertNotIsSubclass(dict, object)
    58
    59
    60# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'dict'> is a subclass of <class 'object'>
    

    because dict is a child of object.

  • I change assertNotIsSubclass_ to assertIsSubclass_

    54    def test_is_a_dictionary_an_object(self):
    55        # assert not issubclass(dict, object)
    56        assert issubclass(dict, object)
    57        # self.assertNotIsSubclass(dict, object)
    58        self.assertIsSubclass(dict, object)
    59
    60
    61# Exceptions seen
    

    the test passes.

  • I remove the commented lines

    54    def test_is_a_dictionary_an_object(self):
    55        assert issubclass(dict, object)
    56        self.assertIsSubclass(dict, object)
    57
    58
    59# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_a_dictionary_an_object'
    

A dictionary is an object.


instance vs subclass

RED: make it fail


  • I add an assertion to test_making_a_class_w_pass to show that an instance (a copy) is different from a subclass (child)

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11        assert issubclass(an_instance, object)
    12
    13    def test_making_a_class_w_parentheses(self):
    

    the terminal is my friend, and shows TypeError

    TypeError: issubclass() arg 1 must be a class
    

    because the argument I put on the left is an instance not a subclass.

  • I add TypeError to the list of Exceptions seen

    60# Exceptions seen
    61# AssertionError
    62# NameError
    63# AttributeError
    64# TypeError
    

GREEN: make it pass


I change the assertion to make the statement True

 7    def test_making_a_class_w_pass(self):
 8        an_instance = src.classes.WPass()
 9        assert isinstance(an_instance, object)
10        self.assertIsInstance(an_instance, object)
11        # assert issubclass(an_instance, object)
12        assert issubclass(src.classes.WPass, object)
13
14    def test_making_a_class_w_parentheses(self):

the test passes.


REFACTOR: make it better


  • I add a call to the `assertNotIsSubclass method`_

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11        # assert issubclass(an_instance, object)
    12        assert issubclass(src.classes.WPass, object)
    13        self.assertNotIsSubclass(
    14            src.classes.WPass, object
    15        )
    16
    17    def test_making_a_class_w_parentheses(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'src.classes.WPass'> is
        a subclass of <class 'object'>
    
  • I change assertNotIsSubclass_ to assertIsSubclass_

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11        # assert issubclass(an_instance, object)
    12        assert issubclass(src.classes.WPass, object)
    13        # self.assertNotIsSubclass(
    14        self.assertIsSubclass(
    15            src.classes.WPass, object
    16        )
    17
    18    def test_making_a_class_w_parentheses(self):
    

    the test passes.

  • I add a variable

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11
    12        a_class = src.classes.WPass
    13        # assert issubclass(an_instance, object)
    14        assert issubclass(src.classes.WPass, object)
    15        # self.assertNotIsSubclass(
    16        self.assertIsSubclass(
    17            src.classes.WPass, object
    18        )
    19
    20    def test_making_a_class_w_parentheses(self):
    
  • I use the variable to remove repetition of src.classes.WPass

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11
    12        a_class = src.classes.WPass
    13        assert issubclass(a_class, object)
    14        self.assertIsSubclass(a_class, object)
    15        # assert issubclass(an_instance, object)
    16        # assert issubclass(src.classes.WPass, object)
    17        # self.assertNotIsSubclass(
    18        # self.assertIsSubclass(
    19        #     src.classes.WPass, object
    20        # )
    21
    22    def test_making_a_class_w_parentheses(self):
    

    the test is still green.

  • I remove the commented lines

     7    def test_making_a_class_w_pass(self):
     8        an_instance = src.classes.WPass()
     9        assert isinstance(an_instance, object)
    10        self.assertIsInstance(an_instance, object)
    11
    12        a_class = src.classes.WPass
    13        assert issubclass(a_class, object)
    14        self.assertIsSubclass(a_class, object)
    15
    16    def test_making_a_class_w_parentheses(self):
    

  • I add an assertion to test_making_a_class_w_parentheses

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        assert issubclass(an_instance, object)
    22
    23    def test_making_a_class_w_object(self):
    

    the terminal is my friend, and shows TypeError

    TypeError: issubclass() arg 1 must be a class
    

    because src.classes.WParentheses() is an instance not a subclass.

  • I change the assertion to make the statement True

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        # assert issubclass(an_instance, object)
    22        assert issubclass(
    23            src.classes.WParentheses, object
    24        )
    25
    26    def test_making_a_class_w_object(self):
    

    the test passes.

  • I add a call to the `assertNotIsSubclass method`_

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        # assert issubclass(an_instance, object)
    22        assert issubclass(
    23            src.classes.WParentheses, object
    24        )
    25        self.assertNotIsSubclass(
    26            src.classes.WParentheses, object
    27        )
    28
    29    def test_making_a_class_w_object(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'src.classes.WParentheses'> is
        a subclass of <class 'object'>
    
  • I change assertNotIsSubclass_ to the `assertIsSubclass method`_

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        # assert issubclass(an_instance, object)
    22        assert issubclass(
    23            src.classes.WParentheses, object
    24        )
    25        # self.assertNotIsSubclass(
    26        self.assertIsSubclass(
    27            src.classes.WParentheses, object
    28        )
    29
    30    def test_making_a_class_w_object(self):
    

    the test passes.

  • I add a variable

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        a_class = src.classes.WParentheses
    22        # assert issubclass(an_instance, object)
    23        assert issubclass(
    24            src.classes.WParentheses, object
    25        )
    26        # self.assertNotIsSubclass(
    27        self.assertIsSubclass(
    28            src.classes.WParentheses, object
    29        )
    30
    31    def test_making_a_class_w_object(self):
    
  • I use the variable to remove repetition of src.classes.WParentheses

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        a_class = src.classes.WParentheses
    22        assert issubclass(a_class, object)
    23        self.assertIsSubclass(a_class, object)
    24        # assert issubclass(an_instance, object)
    25        # assert issubclass(
    26        #     src.classes.WParentheses, object
    27        # )
    28        # self.assertNotIsSubclass(
    29        # self.assertIsSubclass(
    30        #     src.classes.WParentheses, object
    31        # )
    32
    33    def test_making_a_class_w_object(self):
    

    the test is still green.

  • I remove the commented lines

    16    def test_making_a_class_w_parentheses(self):
    17        an_instance = src.classes.WParentheses()
    18        assert isinstance(an_instance, object)
    19        self.assertIsInstance(an_instance, object)
    20
    21        a_class = src.classes.WParentheses
    22        assert issubclass(a_class, object)
    23        self.assertIsSubclass(a_class, object)
    24
    25    def test_making_a_class_w_object(self):
    

  • I also add an assertion to test_making_a_class_w_object

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        assert issubclass(an_instance, object)
    31
    32    def test_is_none_an_object(self):
    

    the terminal is my friend, and shows TypeError

    TypeError: issubclass() arg 1 must be a class
    

    because src.classes.WObject() is an instance not a subclass.

  • I change the assertion to make the statement True

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        # assert issubclass(an_instance, object)
    31        assert issubclass(
    32            src.classes.WObject, object
    33        )
    34
    35    def test_is_none_an_object(self):
    

    the test passes.

  • I add a call to the `assertNotIsSubclass method`_

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        # assert issubclass(an_instance, object)
    31        assert issubclass(
    32            src.classes.WObject, object
    33        )
    34        self.assertNotIsSubclass(
    35            src.classes.WObject, object
    36        )
    37
    38    def test_is_none_an_object(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        <class 'src.classes.WObject'> is
        a subclass of <class 'object'>
    
  • I change assertNotIsSubclass_ to the `assertIsSubclass method`_

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        # assert issubclass(an_instance, object)
    31        assert issubclass(
    32            src.classes.WObject, object
    33        )
    34        # self.assertNotIsSubclass(
    35        self.assertIsSubclass(
    36            src.classes.WObject, object
    37        )
    38
    39    def test_is_none_an_object(self):
    

    the test passes.

  • I add a variable

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        a_class = src.classes.WObject
    31        # assert issubclass(an_instance, object)
    32        assert issubclass(
    33            src.classes.WObject, object
    34        )
    35        # self.assertNotIsSubclass(
    36        self.assertIsSubclass(
    37            src.classes.WObject, object
    38        )
    39
    40    def test_is_none_an_object(self):
    
  • I use the variable to remove repetition of src.classes.WObject

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        a_class = src.classes.WObject
    31        assert issubclass(a_class, object)
    32        self.assertIsSubclass(a_class, object)
    33        # assert issubclass(an_instance, object)
    34        # assert issubclass(
    35        #     src.classes.WObject, object
    36        # )
    37        # self.assertNotIsSubclass(
    38        # self.assertIsSubclass(
    39        #     src.classes.WObject, object
    40        # )
    41
    42    def test_is_none_an_object(self):
    

    the test is still green.

  • I remove the commented lines

    25    def test_making_a_class_w_object(self):
    26        an_instance = src.classes.WObject()
    27        assert isinstance(an_instance, object)
    28        self.assertIsInstance(an_instance, object)
    29
    30        a_class = src.classes.WObject
    31        assert issubclass(a_class, object)
    32        self.assertIsSubclass(a_class, object)
    33
    34    def test_is_none_an_object(self):
    
  • I add a git commit message in the other terminal

    git commit -am 'add instance vs subclass'
    

The difference between a subclass (child) and an an instance (a copy) is the () after the name

a_name = ClassName

points the a_name variable to ClassName

a_name = ClassName()

points the a_name variable to the result of calling ClassName()


test_dir_object

In test_dir_person_class I saw the methods I added to the Person class and also names that I did not add, which led to the question of where they came from.

I want to test the attributes and methods of the object class because it is the mother of all classes.


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test to test_classes.py

    66    def test_is_a_dictionary_an_object(self):
    67        assert issubclass(dict, object)
    68        self.assertIsSubclass(dict, object)
    69
    70    def test_dir_object(self):
    71        reality = dir(object)
    72        my_expectation = []
    73        self.assertEqual(reality, my_expectation)
    74
    75
    76# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: Lists differ:
        ['__class__', '__delattr__', '__dir__', '_[272 chars]k__']
     != []
    

GREEN: make it pass


  • I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as my_expectation

    70    def test_dir_object(self):
    71        reality = dir(object)
    72        # my_expectation = []
    73        my_expectation = [
    74            '__class__', '__delattr__', '__dir__',
    75            '_[272 chars]k__'
    76        ]
    77        self.assertEqual(reality, my_expectation)
    78
    79
    80# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: Lists differ:
        ['__c[32 chars]', '__doc__', '__eq__',
         '__format__', '__ge__'[231 chars]k__']
     != ['__c[32 chars]', '_[272 chars]k__']
    

    it shows me the entire list below the message

  • I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as my_expectation

     70    def test_dir_object(self):
     71        reality = dir(object)
     72        # my_expectation = []
     73        # my_expectation = [
     74        #     '__class__', '__delattr__', '__dir__',
     75        #     '_[272 chars]k__'
     76        # ]
     77        my_expectation = E       - ['__class__',
     78E       -  '__delattr__',
     79E       -  '__dir__',
     80E       -  '__doc__',
     81E       -  '__eq__',
     82E       -  '__format__',
     83E       -  '__ge__',
     84E       -  '__getattribute__',
     85E       -  '__getstate__',
     86E       -  '__gt__',
     87E       -  '__hash__',
     88E       -  '__init__',
     89E       -  '__init_subclass__',
     90E       -  '__le__',
     91E       -  '__lt__',
     92E       -  '__ne__',
     93E       -  '__new__',
     94E       -  '__reduce__',
     95E       -  '__reduce_ex__',
     96E       -  '__repr__',
     97E       -  '__setattr__',
     98E       -  '__sizeof__',
     99E       -  '__str__',
    100E       -  '__subclasshook__']
    101        self.assertEqual(reality, my_expectation)
    102
    103
    104# Exceptions seen
    

    the terminal is my friend, and shows NameError

    NameError: name 'E' is not defined
    
  • I use the find and replace feature of the Integrated Development Environment (IDE) to remove the extra characters, then remove the commented lines

     70    def test_dir_object(self):
     71        reality = dir(object)
     72        my_expectation = [
     73            '__class__',
     74            '__delattr__',
     75            '__dir__',
     76            '__doc__',
     77            '__eq__',
     78            '__format__',
     79            '__ge__',
     80            '__getattribute__',
     81            '__getstate__',
     82            '__gt__',
     83            '__hash__',
     84            '__init__',
     85            '__init_subclass__',
     86            '__le__',
     87            '__lt__',
     88            '__ne__',
     89            '__new__',
     90            '__reduce__',
     91            '__reduce_ex__',
     92            '__repr__',
     93            '__setattr__',
     94            '__sizeof__',
     95            '__str__',
     96            '__subclasshook__'
     97        ]
     98        self.assertEqual(reality, my_expectation)
     99
    100
    101# Exceptions seen
    

    the test passes. All classes automatically get these attributes, they inherit them because all classes inherit from ‘object’.

    The __init__ method is also inherited which means when I defined it in test_classy_person_says_hello I overwrote the inherited one.

  • I add a git commit message in the other terminal

    git commit -am \
    'add test_dir_object'
    

all classes inherit from ‘object’.


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.


close the project

  • I close test_classes.py and classes.py in the editor(s)

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

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


code from the chapter

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


what is next?

You know


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.