test telephone with unittest

I want to use the unittest library in the telephone project.


preview

I have these tests by the end of the chapter

telephone/tests/test_telephone.py
 1import src.telephone
 2import unittest
 3
 4
 5text = src.telephone.text
 6
 7
 8class TestTelephone(unittest.TestCase):
 9
10    def test_passing_none(self):
11        self.assertEqual(text(None), 'I got: None')
12
13    def test_passing_booleans(self):
14        self.assertEqual(text(False), 'I got: False')
15        self.assertEqual(text(True), 'I got: True')
telephone/tests/test_telephone.py
17    def test_passing_an_integer(self):
18        an_integer = 1234
19        self.assertEqual(
20            text(an_integer), f'I got: {an_integer}'
21        )
22
23    def test_passing_a_float(self):
24        a_float = 5.678
25        self.assertEqual(
26            text(a_float), f'I got: {a_float}'
27        )
telephone/tests/test_telephone.py
29    def test_passing_a_string(self):
30        a_string = 'hello'
31        self.assertEqual(
32            text('hello'), f'I got: {a_string}'
33        )
34
35    def test_passing_a_tuple(self):
36        a_tuple = (0, 1, 2, 'n')
37        self.assertEqual(
38            text(a_tuple), f"I got: {a_tuple}"
39        )
telephone/tests/test_telephone.py
41    def test_passing_a_list(self):
42        a_list = [0, 1, 2, 'n']
43        self.assertEqual(
44            text(a_list), f'I got: {a_list}'
45        )
46
47    def test_passing_a_set(self):
48        a_set = {0, 1, 2, 'n'}
49        self.assertEqual(text(a_set), f'I got: {a_set}')
telephone/tests/test_telephone.py
51    def test_passing_a_dictionary(self):
52        a_dictionary = {
53            'key0': 'value0',
54            'keyN': [0, 1, 2, 'n'],
55        }
56        reality = text(a_dictionary)
57        my_expectation = f'I got: {a_dictionary}'
58        self.assertEqual(reality, my_expectation)
telephone/tests/test_telephone.py
60    def test_passing_a_class(self):
61        self.assertEqual(
62            text(object), "I got: <class 'object'>"
63        )
64        self.assertEqual(
65            text(bool), "I got: <class 'bool'>"
66        )
67        self.assertEqual(text(int), "I got: <class 'int'>")
68        self.assertEqual(
69            text(float), "I got: <class 'float'>"
70        )
71        self.assertEqual(text(str), "I got: <class 'str'>")
72        self.assertEqual(
73            text(tuple), "I got: <class 'tuple'>"
74        )
75        self.assertEqual(
76            text(list), "I got: <class 'list'>"
77        )
78        self.assertEqual(text(set), "I got: <class 'set'>")
79        self.assertEqual(
80            text(dict), "I got: <class 'dict'>"
81        )
82
83
84# Exceptions seen
85# AssertionError
86# NameError
87# TypeError
88# AttributeError

open the project

  • I open a terminal

  • I change directory to the project

    cd telephone
    

    the terminal shows I am in the telephone folder

    .../pumping_python/telephone
    
  • I open test_telephone.py

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal shows

    test_telephone.py ..........                        [100%]
    
    =================== 10 passed in D.EFs ===================
    

add TestTelephone class

RED: make it fail


  • I add an object named Telephone to test_telephone.py

     1import src.telephone
     2
     3
     4text = src.telephone.text
     5
     6
     7def assert_equal(a, b):
     8    assert a == b
     9
    10
    11class Telephone(object):
    12
    13    def test_failure(self):
    14        self.assertEqual(True, False)
    15
    16
    17def test_passing_none():
    

    the test is still green.

  • I change the name of the object to TestTelephone

     7def assert_equal(a, b):
     8    assert a == b
     9
    10
    11# class Telephone(object):
    12class TestTelephone(object):
    13
    14    def test_failure(self):
    

    the terminal is my friend, and shows AttributeError

    AttributeError: 'TestTelephone' object
                    has no attribute 'assertEqual'
    

GREEN: make it pass


  • I add unittest.TestCase as the parent object of TestTelephone

    11# class Telephone(object):
    12# class TestTelephone(object):
    13class TestTelephone(unittest.TestCase):
    14
    15    def test_failure(self):
    

    the terminal is my friend, and shows NameError

    NameError: name 'unittest' is not defined.
               Did you forget to import 'unittest'?
    
  • I add an import statement at the top of the file

    1import src.telephone
    2import unittest
    3
    4
    5text = src.telephone.text
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True != False
    
  • I change False to True in test_failure

    12# class Telephone(object):
    13# class TestTelephone(object):
    14class TestTelephone(unittest.TestCase):
    15
    16    def test_failure(self):
    17        # self.assertEqual(True, False)
    18        self.assertEqual(True, True)
    19
    20
    21def test_passing_none():
    

    the test passes.


REFACTOR: make it better


  • I remove the commented lines

     1def assert_equal(a, b):
     2    assert a == b
     3
     4
     5class TestTelephone(unittest.TestCase):
     6
     7    def test_failure(self):
     8        self.assertEqual(True, True)
     9
    10
    11def test_passing_none():
    
  • I open a new terminal then make sure I am in the telephone folder

    cd telephone
    
  • I add a git commit message in the new terminal

    git commit -am \
    'add TestTelephone class'
    

test_passing_none with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_passing_none

12class TestTelephone(unittest.TestCase):
13
14    # def test_passing_none():
15    def test_passing_none(self):
16        assert_equal(text(None), 'I got: None')

the test is green again.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_none

    14    # def test_passing_none():
    15    def test_passing_none(self):
    16        # assert_equal(text(None), 'I got: None')
    17        self.assertNotEqual(text(None), 'I got: None')
    18
    19
    20def test_passing_booleans():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'I got: None' == 'I got: None'
    
  • I change assertNotEqual to assertEqual in test_passing_none

    14    # def test_passing_none():
    15    def test_passing_none(self):
    16        # assert_equal(text(None), 'I got: None')
    17        # self.assertNotEqual(text(None), 'I got: None')
    18        self.assertEqual(text(None), 'I got: None')
    19
    20
    21def test_passing_booleans():
    

    the test passes.

  • I remove the commented lines from test_passing_none

    12class TestTelephone(unittest.TestCase):
    13
    14    def test_passing_none(self):
    15        self.assertEqual(text(None), 'I got: None')
    16
    17
    18def test_passing_booleans():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_none to TestTelephone'
    

test_passing_booleans with unittest

RED: make it fail


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

  • I move test_passing_booleans to make it a method of the TestTelephone class

    14    def test_passing_none(self):
    15        self.assertEqual(text(None), 'I got: None')
    16
    17    def test_passing_booleans():
    18        assert_equal(text(False), 'I got: False')
    19        assert_equal(text(True), 'I got: True')
    20
    21
    22def test_passing_an_integer():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_booleans()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance …


GREEN: make it pass


I add self to the parentheses of test_passing_booleans

14    def test_passing_none(self):
15        self.assertEqual(text(None), 'I got: None')
16
17    # def test_passing_booleans():
18    def test_passing_booleans(self):
19        assert_equal(text(False), 'I got: False')
20        assert_equal(text(True), 'I got: True')

green again.


REFACTOR: make it better


  • I change the calls to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertions in test_passing_booleans

    17    # def test_passing_booleans():
    18    def test_passing_booleans(self):
    19        # assert_equal(text(False), 'I got: False')
    20        self.assertNotEqual(text(False), 'I got: False')
    21        # assert_equal(text(True), 'I got: True')
    22        self.assertNotEqual(text(True), 'I got: True')
    23
    24
    25def test_passing_an_integer():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'I got: False' == 'I got: False'
    
  • I change assertNotEqual to assertEqual for the first assertion in test_passing_booleans

    17    # def test_passing_booleans():
    18    def test_passing_booleans(self):
    19        # assert_equal(text(False), 'I got: False')
    20        # self.assertNotEqual(text(False), 'I got: False')
    21        self.assertEqual(text(False), 'I got: False')
    22        # assert_equal(text(True), 'I got: True')
    23        self.assertNotEqual(text(True), 'I got: True')
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'I got: True' == 'I got: True'
    
  • I change assertNotEqual to assertEqual for the second assertion in test_passing_booleans

    17    # def test_passing_booleans():
    18    def test_passing_booleans(self):
    19        # assert_equal(text(False), 'I got: False')
    20        # self.assertNotEqual(text(False), 'I got: False')
    21        self.assertEqual(text(False), 'I got: False')
    22        # assert_equal(text(True), 'I got: True')
    23        # self.assertNotEqual(text(True), 'I got: True')
    24        self.assertEqual(text(True), 'I got: True')
    25
    26
    27def test_passing_an_integer():
    

    the test passes.

  • I remove the commented lines from test_passing_booleans

    14    def test_passing_none(self):
    15        self.assertEqual(text(None), 'I got: None')
    16
    17    def test_passing_booleans(self):
    18        self.assertEqual(text(False), 'I got: False')
    19        self.assertEqual(text(True), 'I got: True')
    20
    21
    22def test_passing_an_integer():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_booleans to TestTelephone'
    

test_passing_an_integer with unittest

RED: make it fail


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

  • I move test_passing_an_integer to make it a method of the TestTelephone class

    17    def test_passing_booleans(self):
    18        self.assertEqual(text(False), 'I got: False')
    19        self.assertEqual(text(True), 'I got: True')
    20
    21    def test_passing_an_integer():
    22        an_integer = 1234
    23        assert_equal(text(an_integer), f'I got: {an_integer}')
    24
    25
    26def test_passing_a_float():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_an_integer()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_passing_an_integer

17      def test_passing_booleans(self):
18          self.assertEqual(text(False), 'I got: False')
19          self.assertEqual(text(True), 'I got: True')
20
21      # def test_passing_an_integer():
22      def test_passing_an_integer(self):
23          an_integer = 1234
24          assert_equal(text(an_integer), f'I got: {an_integer}')

green.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_an_integer

    21    # def test_passing_an_integer():
    22    def test_passing_an_integer(self):
    23        an_integer = 1234
    24        # assert_equal(text(an_integer), f'I got: {an_integer}')
    25        self.assertNotEqual(
    26            text(an_integer), f'I got: {an_integer}'
    27        )
    28
    29
    30def test_passing_a_float():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'I got: 1234' == 'I got: 1234'
    
  • I change assertNotEqual to assertEqual in test_passing_an_integer

    21    # def test_passing_an_integer():
    22    def test_passing_an_integer(self):
    23        an_integer = 1234
    24        # assert_equal(text(an_integer), f'I got: {an_integer}')
    25        # self.assertNotEqual(
    26        self.assertEqual(
    27            text(an_integer), f'I got: {an_integer}'
    28        )
    

    the test passes.

  • I remove the commented lines from test_passing_an_integer

    17    def test_passing_booleans(self):
    18        self.assertEqual(text(False), 'I got: False')
    19        self.assertEqual(text(True), 'I got: True')
    20
    21    def test_passing_an_integer(self):
    22        an_integer = 1234
    23        self.assertEqual(
    24            text(an_integer), f'I got: {an_integer}'
    25        )
    26
    27
    28def test_passing_a_float():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_an_integer to TestTelephone'
    

test_passing_a_float with unittest

RED: make it fail


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

  • I move test_passing_a_float to make it a method of the TestTelephone class

    21    def test_passing_an_integer(self):
    22        an_integer = 1234
    23        self.assertEqual(
    24            text(an_integer), f'I got: {an_integer}'
    25        )
    26
    27    def test_passing_a_float():
    28        a_float = 5.678
    29        assert_equal(text(a_float), f'I got: {a_float}')
    30
    31
    32def test_passing_a_string():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_float()
        takes 0 positional arguments but 1 was given
    

    because a method …


GREEN: make it pass


I add self to the parentheses of test_passing_a_float

21    def test_passing_an_integer(self):
22        an_integer = 1234
23        self.assertEqual(
24            text(an_integer), f'I got: {an_integer}'
25        )
26
27    # def test_passing_a_float():
28    def test_passing_a_float(self):
29        a_float = 5.678
30        assert_equal(text(a_float), f'I got: {a_float}')

green again.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_a_float

    27    # def test_passing_a_float():
    28    def test_passing_a_float(self):
    29        a_float = 5.678
    30        # assert_equal(text(a_float), f'I got: {a_float}')
    31        self.assertNotEqual(
    32            text(a_float), f'I got: {a_float}'
    33        )
    34
    35
    36def test_passing_a_string():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'I got: 5.678' == 'I got: 5.678'
    
  • I change assertNotEqual to assertEqual in test_passing_a_float

    27    # def test_passing_a_float():
    28    def test_passing_a_float(self):
    29        a_float = 5.678
    30        # assert_equal(text(a_float), f'I got: {a_float}')
    31        # self.assertNotEqual(
    32        self.assertEqual(
    33            text(a_float), f'I got: {a_float}'
    34        )
    

    the test passes.

  • I remove the commented lines from test_passing_a_float

    21    def test_passing_an_integer(self):
    22        an_integer = 1234
    23        self.assertEqual(
    24            text(an_integer), f'I got: {an_integer}'
    25        )
    26
    27    def test_passing_a_float(self):
    28        a_float = 5.678
    29        self.assertEqual(
    30            text(a_float), f'I got: {a_float}'
    31        )
    32
    33
    34def test_passing_a_string():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_float to TestTelephone'
    

test_passing_a_string with unittest

RED: make it fail


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

  • I move test_passing_a_string to make it a method of the TestTelephone class

    27    def test_passing_a_float(self):
    28        a_float = 5.678
    29        self.assertEqual(
    30            text(a_float), f'I got: {a_float}'
    31        )
    32
    33    def test_passing_a_string():
    34        a_string = 'hello'
    35        assert_equal(text('hello'), f'I got: {a_string}')
    36
    37
    38def test_passing_a_tuple():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_string()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_passing_a_string

27    def test_passing_a_float(self):
28        a_float = 5.678
29        self.assertEqual(
30            text(a_float), f'I got: {a_float}'
31        )
32
33    # def test_passing_a_string():
34    def test_passing_a_string(self):
35        a_string = 'hello'
36        assert_equal(text('hello'), f'I got: {a_string}')

the test is green again.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_a_string

    33    # def test_passing_a_string():
    34    def test_passing_a_string(self):
    35        a_string = 'hello'
    36        # assert_equal(text('hello'), f'I got: {a_string}')
    37        self.assertNotEqual(
    38            text('hello'), f'I got: {a_string}'
    39        )
    40
    41
    42def test_passing_a_tuple():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'I got: hello' == 'I got: hello'
    
  • I change assertNotEqual to assertEqual in test_passing_a_string

    33    # def test_passing_a_string():
    34    def test_passing_a_string(self):
    35        a_string = 'hello'
    36        # assert_equal(text('hello'), f'I got: {a_string}')
    37        # self.assertNotEqual(
    38        self.assertEqual(
    39            text('hello'), f'I got: {a_string}'
    40        )
    

    the test passes.

  • I remove the commented lines from test_passing_a_string

    27    def test_passing_a_float(self):
    28        a_float = 5.678
    29        self.assertEqual(
    30            text(a_float), f'I got: {a_float}'
    31        )
    32
    33    def test_passing_a_string(self):
    34        a_string = 'hello'
    35        self.assertEqual(
    36            text('hello'), f'I got: {a_string}'
    37        )
    38
    39
    40def test_passing_a_tuple():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_string to TestTelephone'
    

test_passing_a_tuple with unittest

RED: make it fail


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

  • I move test_passing_a_tuple to make it a method of the TestTelephone class

    33    def test_passing_a_string(self):
    34        a_string = 'hello'
    35        self.assertEqual(
    36            text('hello'), f'I got: {a_string}'
    37        )
    38
    39    def test_passing_a_tuple():
    40        a_tuple = (0, 1, 2, 'n')
    41        assert_equal(text(a_tuple), f"I got: {a_tuple}")
    42
    43
    44def test_passing_a_list():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_tuple()
        takes 0 positional arguments but 1 was given
    

GREEN: make it pass


I add self to the parentheses of test_passing_a_tuple

33    def test_passing_a_string(self):
34        a_string = 'hello'
35        self.assertEqual(
36            text('hello'), f'I got: {a_string}'
37        )
38
39    # def test_passing_a_tuple():
40    def test_passing_a_tuple(self):
41        a_tuple = (0, 1, 2, 'n')
42        assert_equal(text(a_tuple), f"I got: {a_tuple}")

green again.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_a_tuple

    39    # def test_passing_a_tuple():
    40    def test_passing_a_tuple(self):
    41        a_tuple = (0, 1, 2, 'n')
    42        # assert_equal(text(a_tuple), f"I got: {a_tuple}")
    43        self.assertNotEqual(
    44            text(a_tuple), f"I got: {a_tuple}"
    45        )
    46
    47
    48def test_passing_a_list():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: "I got: (0, 1, 2, 'n')"
                 == "I got: (0, 1, 2, 'n')"
    
  • I change assertNotEqual to assertEqual in test_passing_a_tuple

    39    # def test_passing_a_tuple():
    40    def test_passing_a_tuple(self):
    41        a_tuple = (0, 1, 2, 'n')
    42        # assert_equal(text(a_tuple), f"I got: {a_tuple}")
    43        # self.assertNotEqual(
    44        self.assertEqual(
    45            text(a_tuple), f"I got: {a_tuple}"
    46        )
    

    the test passes.

  • I remove the commented lines from test_passing_a_tuple

    33    def test_passing_a_string(self):
    34        a_string = 'hello'
    35        self.assertEqual(
    36            text('hello'), f'I got: {a_string}'
    37        )
    38
    39    def test_passing_a_tuple(self):
    40        a_tuple = (0, 1, 2, 'n')
    41        self.assertEqual(
    42            text(a_tuple), f"I got: {a_tuple}"
    43        )
    44
    45
    46def test_passing_a_list():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_tuple to TestTelephone'
    

test_passing_a_list with unittest

RED: make it fail


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

  • I move test_passing_a_list to make it a method of the TestTelephone class

    39    def test_passing_a_tuple(self):
    40        a_tuple = (0, 1, 2, 'n')
    41        self.assertEqual(
    42            text(a_tuple), f"I got: {a_tuple}"
    43        )
    44
    45    def test_passing_a_list():
    46        a_list = [0, 1, 2, 'n']
    47        assert_equal(text(a_list), f'I got: {a_list}')
    48
    49
    50def test_passing_a_set():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_list()
        takes 0 positional arguments but 1 was given
    

GREEN: make it pass


I add self to the parentheses of test_passing_a_list

39    def test_passing_a_tuple(self):
40        a_tuple = (0, 1, 2, 'n')
41        self.assertEqual(
42            text(a_tuple), f"I got: {a_tuple}"
43        )
44
45    # def test_passing_a_list():
46    def test_passing_a_list(self):
47        a_list = [0, 1, 2, 'n']
48        assert_equal(text(a_list), f'I got: {a_list}')
49
50
51def test_passing_a_set():

green.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_a_list

    45    # def test_passing_a_list():
    46    def test_passing_a_list(self):
    47        a_list = [0, 1, 2, 'n']
    48        # assert_equal(text(a_list), f'I got: {a_list}')
    49        self.assertNotEqual(
    50            text(a_list), f'I got: {a_list}'
    51        )
    52
    53
    54def test_passing_a_set():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: "I got: [0, 1, 2, 'n']"
                 == "I got: [0, 1, 2, 'n']"
    
  • I change assertNotEqual to assertEqual in test_passing_a_list

    45    # def test_passing_a_list():
    46    def test_passing_a_list(self):
    47        a_list = [0, 1, 2, 'n']
    48        # assert_equal(text(a_list), f'I got: {a_list}')
    49        # self.assertNotEqual(
    50        self.assertEqual(
    51            text(a_list), f'I got: {a_list}'
    52        )
    

    the test passes.

  • I remove the commented lines from test_passing_a_list

    39    def test_passing_a_tuple(self):
    40        a_tuple = (0, 1, 2, 'n')
    41        self.assertEqual(
    42            text(a_tuple), f"I got: {a_tuple}"
    43        )
    44
    45    def test_passing_a_list(self):
    46        a_list = [0, 1, 2, 'n']
    47        self.assertEqual(
    48            text(a_list), f'I got: {a_list}'
    49        )
    50
    51
    52def test_passing_a_set():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_list to TestTelephone'
    

test_passing_a_set with unittest

RED: make it fail


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

  • I move test_passing_a_set to make it a method of the TestTelephone class

    45    def test_passing_a_list(self):
    46        a_list = [0, 1, 2, 'n']
    47        self.assertEqual(
    48            text(a_list), f'I got: {a_list}'
    49        )
    50
    51    def test_passing_a_set():
    52        a_set = {0, 1, 2, 'n'}
    53        assert_equal(text(a_set), f'I got: {a_set}')
    54
    55
    56def test_passing_a_dictionary():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_set()
        takes 0 positional arguments but 1 was given
    

GREEN: make it pass


I add self to the parentheses of test_passing_a_set

45    def test_passing_a_list(self):
46        a_list = [0, 1, 2, 'n']
47        self.assertEqual(
48            text(a_list), f'I got: {a_list}'
49        )
50
51    # def test_passing_a_set():
52    def test_passing_a_set(self):
53        a_set = {0, 1, 2, 'n'}
54        assert_equal(text(a_set), f'I got: {a_set}')

green again.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_a_set

    51    # def test_passing_a_set():
    52    def test_passing_a_set(self):
    53        a_set = {0, 1, 2, 'n'}
    54        # assert_equal(text(a_set), f'I got: {a_set}')
    55        self.assertNotEqual(text(a_set), f'I got: {a_set}')
    56
    57
    58def test_passing_a_dictionary():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: "I got: {0, 1, 2, 'n'}"
                 == "I got: {0, 1, 2, 'n'}"
    
  • I change assertNotEqual to assertEqual in test_passing_a_set

    51    # def test_passing_a_set():
    52    def test_passing_a_set(self):
    53        a_set = {0, 1, 2, 'n'}
    54        # assert_equal(text(a_set), f'I got: {a_set}')
    55        # self.assertNotEqual(text(a_set), f'I got: {a_set}')
    56        self.assertEqual(text(a_set), f'I got: {a_set}')
    57
    58
    59def test_passing_a_dictionary():
    

    the test passes.

  • I remove the commented lines from test_passing_a_set

    45    def test_passing_a_list(self):
    46        a_list = [0, 1, 2, 'n']
    47        self.assertEqual(
    48            text(a_list), f'I got: {a_list}'
    49        )
    50
    51    def test_passing_a_set(self):
    52        a_set = {0, 1, 2, 'n'}
    53        self.assertEqual(text(a_set), f'I got: {a_set}')
    54
    55
    56def test_passing_a_dictionary():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_set to TestTelephone'
    

test_passing_a_dictionary with unittest

RED: make it fail


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

  • I move test_passing_a_dictionary to make it a method of the TestTelephone class

    51    def test_passing_a_set(self):
    52        a_set = {0, 1, 2, 'n'}
    53        self.assertEqual(text(a_set), f'I got: {a_set}')
    54
    55    def test_passing_a_dictionary():
    56        a_dictionary = {
    57            'key0': 'value0',
    58            'keyN': [0, 1, 2, 'n'],
    59        }
    60        reality = text(a_dictionary)
    61        my_expectation = f'I got: {a_dictionary}'
    62        assert_equal(reality, my_expectation)
    63
    64
    65def test_passing_a_class():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_dictionary()
        takes 0 positional arguments but 1 was given
    

GREEN: make it pass


I add self to the parentheses of test_passing_a_dictionary

51    def test_passing_a_set(self):
52        a_set = {0, 1, 2, 'n'}
53        self.assertEqual(text(a_set), f'I got: {a_set}')
54
55    # def test_passing_a_dictionary():
56    def test_passing_a_dictionary(self):
57        a_dictionary = {
58            'key0': 'value0',
59            'keyN': [0, 1, 2, 'n'],
60        }

the test is green again.


REFACTOR: make it better


  • I change the call to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertion in test_passing_a_dictionary

    55    # def test_passing_a_dictionary():
    56    def test_passing_a_dictionary(self):
    57        a_dictionary = {
    58            'key0': 'value0',
    59            'keyN': [0, 1, 2, 'n'],
    60        }
    61        reality = text(a_dictionary)
    62        my_expectation = f'I got: {a_dictionary}'
    63        # assert_equal(reality, my_expectation)
    64        self.assertNotEqual(reality, my_expectation)
    65
    66
    67def test_passing_a_class():
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        "I got: {'key0': 'value0', 'keyN': [0, 1, 2, 'n']}"
     == "I got: {'key0': 'value0', 'keyN': [0, 1, 2, 'n']}"
    
  • I change assertNotEqual to assertEqual in test_passing_a_dictionary

    55    # def test_passing_a_dictionary():
    56    def test_passing_a_dictionary(self):
    57        a_dictionary = {
    58            'key0': 'value0',
    59            'keyN': [0, 1, 2, 'n'],
    60        }
    61        reality = text(a_dictionary)
    62        my_expectation = f'I got: {a_dictionary}'
    63        # assert_equal(reality, my_expectation)
    64        # self.assertNotEqual(reality, my_expectation)
    65        self.assertEqual(reality, my_expectation)
    66
    67
    68def test_passing_a_class():
    

    the test passes.

  • I remove the commented lines from test_passing_a_dictionary

    51    def test_passing_a_set(self):
    52        a_set = {0, 1, 2, 'n'}
    53        self.assertEqual(text(a_set), f'I got: {a_set}')
    54
    55    def test_passing_a_dictionary(self):
    56        a_dictionary = {
    57            'key0': 'value0',
    58            'keyN': [0, 1, 2, 'n'],
    59        }
    60        reality = text(a_dictionary)
    61        my_expectation = f'I got: {a_dictionary}'
    62        self.assertEqual(reality, my_expectation)
    63
    64
    65def test_passing_a_class():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_dictionary to TestTelephone'
    

test_passing_a_class with unittest

RED: make it fail


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

  • I move test_passing_a_class to make it a method of the TestTelephone class

    62        self.assertEqual(reality, my_expectation)
    63
    64    def test_passing_a_class():
    65        assert_equal(
    66            text(object), "I got: <class 'object'>"
    67        )
    68        assert_equal(text(bool), "I got: <class 'bool'>")
    69        assert_equal(text(int), "I got: <class 'int'>")
    70        assert_equal(text(float), "I got: <class 'float'>")
    71        assert_equal(text(str), "I got: <class 'str'>")
    72        assert_equal(text(tuple), "I got: <class 'tuple'>")
    73        assert_equal(text(list), "I got: <class 'list'>")
    74        assert_equal(text(set), "I got: <class 'set'>")
    75        assert_equal(text(dict), "I got: <class 'dict'>")
    76
    77
    78# Exceptions seen
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestTelephone.test_passing_a_class()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_passing_a_class

62        self.assertEqual(reality, my_expectation)
63
64    # def test_passing_a_class():
65    def test_passing_a_class(self):
66        assert_equal(
67            text(object), "I got: <class 'object'>"
68        )

green again.


REFACTOR: make it better


  • I change the calls to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertions in test_passing_a_class

    64    # def test_passing_a_class():
    65    def test_passing_a_class(self):
    66        # assert_equal(
    67        self.assertNotEqual(
    68            text(object), "I got: <class 'object'>"
    69        )
    70        # assert_equal(text(bool), "I got: <class 'bool'>")
    71        self.assertNotEqual(
    72            text(bool), "I got: <class 'bool'>"
    73        )
    
    74        # assert_equal(text(int), "I got: <class 'int'>")
    75        self.assertNotEqual(text(int), "I got: <class 'int'>")
    76        # assert_equal(text(float), "I got: <class 'float'>")
    77        self.assertNotEqual(
    78            text(float), "I got: <class 'float'>"
    79        )
    80        # assert_equal(text(str), "I got: <class 'str'>")
    81        self.assertNotEqual(text(str), "I got: <class 'str'>")
    82        # assert_equal(text(tuple), "I got: <class 'tuple'>")
    83        self.assertNotEqual(
    84            text(tuple), "I got: <class 'tuple'>"
    85        )
    
    86        # assert_equal(text(list), "I got: <class 'list'>")
    87        self.assertNotEqual(
    88            text(list), "I got: <class 'list'>"
    89        )
    90        # assert_equal(text(set), "I got: <class 'set'>")
    91        self.assertNotEqual(text(set), "I got: <class 'set'>")
    92        # assert_equal(text(dict), "I got: <class 'dict'>")
    93        self.assertNotEqual(
    94            text(dict), "I got: <class 'dict'>"
    95        )
    96
    97
    98# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: "I got: <class 'object'>"
                 == "I got: <class 'object'>"
    
  • I change assertNotEqual to assertEqual in test_passing_a_class

    64    # def test_passing_a_class():
    65    def test_passing_a_class(self):
    66        # assert_equal(
    67        # self.assertNotEqual(
    68        self.assertEqual(
    69            text(object), "I got: <class 'object'>"
    70        )
    71        # assert_equal(text(bool), "I got: <class 'bool'>")
    72        # self.assertNotEqual(
    73        self.assertEqual(
    74            text(bool), "I got: <class 'bool'>"
    75        )
    
    76        # assert_equal(text(int), "I got: <class 'int'>")
    77        # self.assertNotEqual(text(int), "I got: <class 'int'>")
    78        self.assertEqual(text(int), "I got: <class 'int'>")
    79        # assert_equal(text(float), "I got: <class 'float'>")
    80        # self.assertNotEqual(
    81        self.assertEqual(
    82            text(float), "I got: <class 'float'>"
    83        )
    
    84        # assert_equal(text(str), "I got: <class 'str'>")
    85        # self.assertNotEqual(text(str), "I got: <class 'str'>")
    86        self.assertEqual(text(str), "I got: <class 'str'>")
    87        # assert_equal(text(tuple), "I got: <class 'tuple'>")
    88        # self.assertNotEqual(
    89        self.assertEqual(
    90            text(tuple), "I got: <class 'tuple'>"
    91        )
    
     92        # assert_equal(text(list), "I got: <class 'list'>")
     93        # self.assertNotEqual(
     94        self.assertEqual(
     95            text(list), "I got: <class 'list'>"
     96        )
     97        # assert_equal(text(set), "I got: <class 'set'>")
     98        # self.assertNotEqual(text(set), "I got: <class 'set'>")
     99        self.assertEqual(text(set), "I got: <class 'set'>")
    100        # assert_equal(text(dict), "I got: <class 'dict'>")
    101        # self.assertNotEqual(
    102        self.assertEqual(
    103            text(dict), "I got: <class 'dict'>"
    104        )
    105
    106
    107# Exceptions seen
    

    the test passes.

  • I remove the commented lines from test_passing_a_class

    62        self.assertEqual(reality, my_expectation)
    63
    64    def test_passing_a_class(self):
    65        self.assertEqual(
    66            text(object), "I got: <class 'object'>"
    67        )
    68        self.assertEqual(
    69            text(bool), "I got: <class 'bool'>"
    70        )
    71        self.assertEqual(text(int), "I got: <class 'int'>")
    72        self.assertEqual(
    73            text(float), "I got: <class 'float'>"
    74        )
    75        self.assertEqual(text(str), "I got: <class 'str'>")
    
    76        self.assertEqual(
    77            text(tuple), "I got: <class 'tuple'>"
    78        )
    79        self.assertEqual(
    80            text(list), "I got: <class 'list'>"
    81        )
    82        self.assertEqual(text(set), "I got: <class 'set'>")
    83        self.assertEqual(
    84            text(dict), "I got: <class 'dict'>"
    85        )
    86
    87
    88# Exceptions seen
    89# AssertionError
    90# NameError
    91# TypeError
    92# AttributeError
    
  • I remove my assert_equal function since it is no longer used

    1import src.telephone
    2import unittest
    3
    4
    5text = src.telephone.text
    6
    7
    8class TestTelephone(unittest.TestCase):
    

    all tests are still green.

  • I add a git commit message in the other terminal

    git commit -am \
    'move test_passing_a_class to TestTelephone'
    

close the project

  • I close test_telephone.py

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

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


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.


code from the chapter

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


what is next?

Would you like to make sure the person project always calculates the correct age?


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.