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_an_object(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

    terminal
    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

    terminal
    uv run pytest-watcher . --now --patterns "**/*.py"
    

    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

    the terminal is my friend, and shows AttributeError

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

GREEN: make it pass



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

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

    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



test_passing_booleans with unittest

RED: make it fail



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



test_passing_an_integer with unittest

RED: make it fail



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



test_passing_a_float with unittest

RED: make it fail



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



test_passing_a_string with unittest

RED: make it fail



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



test_passing_a_tuple with unittest

RED: make it fail



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



test_passing_a_list with unittest

RED: make it fail



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



test_passing_a_set with unittest

RED: make it fail



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



test_passing_a_dictionary with unittest

RED: make it fail



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



test_passing_an_object with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_passing_an_object

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

green again.


REFACTOR: make it better



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

    terminal
    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.