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
1import src.telephone
2import unittest
3
4
5class TestTelephone(unittest.TestCase):
6
7 def test_passing_none(self):
8 reality = src.telephone.text(None)
9 my_expectation = 'I got: None'
10 assert reality == my_expectation
11 self.assertEqual(reality, my_expectation)
12
13 def test_passing_booleans(self):
14 reality = src.telephone.text(False)
15 my_expectation = 'I got: False'
16 assert reality == my_expectation
17 self.assertEqual(reality, my_expectation)
18
19 reality = src.telephone.text(True)
20 my_expectation = 'I got: True'
21 assert reality == my_expectation
22 self.assertEqual(reality, my_expectation)
23
24 def test_passing_an_integer(self):
25 an_integer = 1234
26
27 reality = src.telephone.text(an_integer)
28 my_expectation = f'I got: {an_integer}'
29 assert reality == my_expectation
30 self.assertEqual(reality, my_expectation)
31
32 def test_passing_a_float(self):
33 a_float = 5.678
34
35 reality = src.telephone.text(a_float)
36 my_expectation = f'I got: {a_float}'
37 assert reality == my_expectation
38 self.assertEqual(reality, my_expectation)
39
40 def test_passing_a_string(self):
41 a_string = 'hello'
42
43 reality = src.telephone.text(a_string)
44 my_expectation = f'I got: {a_string}'
45 assert reality == my_expectation
46 self.assertEqual(reality, my_expectation)
47
48 def test_passing_a_tuple(self):
49 a_tuple = (0, 1, 2, 'n')
50
51 reality = src.telephone.text(a_tuple)
52 my_expectation = f'I got: {a_tuple}'
53 assert reality == my_expectation
54 self.assertEqual(reality, my_expectation)
55
56 def test_passing_a_list(self):
57 a_list = [0, 1, 2, 'n']
58
59 reality = src.telephone.text(a_list)
60 my_expectation = f'I got: {a_list}'
61 assert reality == my_expectation
62 self.assertEqual(reality, my_expectation)
63
64 def test_passing_a_set(self):
65 a_set = {0, 1, 2, 'n'}
66
67 reality = src.telephone.text(a_set)
68 my_expectation = f'I got: {a_set}'
69 assert reality == my_expectation
70 self.assertEqual(reality, my_expectation)
71
72 def test_passing_a_dictionary(self):
73 a_dictionary = {
74 'key0': 'value0',
75 'keyN': [0, 1, 2, 'n'],
76 }
77
78 reality = src.telephone.text(a_dictionary)
79 my_expectation = f'I got: {a_dictionary}'
80 assert reality == my_expectation
81 self.assertEqual(reality, my_expectation)
82
83 def test_passing_a_class(self):
84 reality = src.telephone.text(object)
85 my_expectation = "I got: <class 'object'>"
86 assert reality == my_expectation
87 self.assertEqual(reality, my_expectation)
88
89 reality = src.telephone.text(bool)
90 my_expectation = "I got: <class 'bool'>"
91 assert reality == my_expectation
92 self.assertEqual(reality, my_expectation)
93
94 reality = src.telephone.text(int)
95 my_expectation = "I got: <class 'int'>"
96 assert reality == my_expectation
97 self.assertEqual(reality, my_expectation)
98
99 reality = src.telephone.text(float)
100 my_expectation = "I got: <class 'float'>"
101 assert reality == my_expectation
102 self.assertEqual(reality, my_expectation)
103
104 reality = src.telephone.text(str)
105 my_expectation = "I got: <class 'str'>"
106 assert reality == my_expectation
107 self.assertEqual(reality, my_expectation)
108
109 reality = src.telephone.text(tuple)
110 my_expectation = "I got: <class 'tuple'>"
111 assert reality == my_expectation
112 self.assertEqual(reality, my_expectation)
113
114 reality = src.telephone.text(list)
115 my_expectation = "I got: <class 'list'>"
116 assert reality == my_expectation
117 self.assertEqual(reality, my_expectation)
118
119 reality = src.telephone.text(set)
120 my_expectation = "I got: <class 'set'>"
121 assert reality == my_expectation
122 self.assertEqual(reality, my_expectation)
123
124 reality = src.telephone.text(dict)
125 my_expectation = "I got: <class 'dict'>"
126 assert reality == my_expectation
127 self.assertEqual(reality, my_expectation)
128
129
130# Exceptions seen
131# AssertionError
132# NameError
133# TypeError
134# ModuleNotFoundError
135# AttributeError
open the project
I open a terminal
I change directory to the project
cd telephonethe terminal shows I am in the
telephonefolder.../pumping_python/telephoneI open
test_telephone.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
test_telephone.py .......... [100%] =================== 10 passed in D.EFs ===================
add TestTelephone class
RED: make it fail
I add a class named
Telephonetotest_telephone.py1import src.telephone 2 3 4class Telephone(object): 5 6 def test_failure(self): 7 self.assertEqual(True, False) 8 9 10def test_passing_none():the test is still green.
I change the name of the class to
TestTelephone1import src.telephone 2 3 4# class Telephone(object): 5class TestTelephone(object):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 class of
TestTelephone1import src.telephone 2 3 4# class Telephone(object): 5# class TestTelephone(object): 6class TestTelephone(unittest.TestCase):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 5# class Telephone(object): 6# class TestTelephone(object): 7class TestTelephone(unittest.TestCase):the terminal is my friend, and shows AssertionError
AssertionError: True != FalseI change False to True in the assertion
5# class Telephone(object): 6# class TestTelephone(object): 7class TestTelephone(unittest.TestCase): 8 9 def test_failure(self): 10 # self.assertEqual(True, False) 11 self.assertEqual(True, True) 12 13 14def test_passing_none():the test passes.
REFACTOR: make it better
I remove the commented lines
1import src.telephone 2import unittest 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
telephonefoldercd telephoneI add a git commit message in the new terminal
git commit -am \ 'add TestTelephone class'
test_passing_none with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_passing_none to make it a method of the TestTelephone class and replace
test_failure7class TestTelephone(unittest.TestCase): 8 9 def test_passing_none(): 10 reality = src.telephone.text(None) 11 my_expectation = 'I got: None' 12 assert reality == my_expectation 13 14 15def test_passing_booleans():the terminal is my friend, and shows TypeError
TypeError: TestTelephone.test_passing_none() takes 0 positional arguments but 1 was givenbecause 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_none
5class TestTelephone(unittest.TestCase):
6
7 # def test_passing_none():
8 def test_passing_none(self):
the test is green again.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_none
5class TestTelephone(unittest.TestCase): 6 7 # def test_passing_none(): 8 def test_passing_none(self): 9 reality = src.telephone.text(None) 10 my_expectation = 'I got: None' 11 assert reality == my_expectation 12 self.assertNotEqual(reality, my_expectation) 13 14 15def 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
5class TestTelephone(unittest.TestCase): 6 7 # def test_passing_none(): 8 def test_passing_none(self): 9 reality = src.telephone.text(None) 10 my_expectation = 'I got: None' 11 assert reality == my_expectation 12 # self.assertNotEqual(reality, my_expectation) 13 self.assertEqual(reality, my_expectation) 14 15 16def test_passing_booleans():the test passes.
I remove the commented lines
5class TestTelephone(unittest.TestCase): 6 7 def test_passing_none(self): 8 reality = src.telephone.text(None) 9 my_expectation = 'I got: None' 10 assert reality == my_expectation 11 self.assertEqual(reality, my_expectation) 12 13 14def 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
11 self.assertEqual(reality, my_expectation) 12 13 def test_passing_booleans(): 14 reality = src.telephone.text(False) 15 my_expectation = 'I got: False' 16 assert reality == my_expectation 17 18 reality = src.telephone.text(True) 19 my_expectation = 'I got: True' 20 assert reality == my_expectation 21 22 23def 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
GREEN: make it pass
I add self to the parentheses of test_passing_booleans
11 self.assertEqual(reality, my_expectation)
12
13 # def test_passing_booleans():
14 def test_passing_booleans(self):
green again.
REFACTOR: make it better
I add calls to the assertNotEqual method for the assertions in test_passing_booleans
11 self.assertEqual(reality, my_expectation) 12 13 # def test_passing_booleans(): 14 def test_passing_booleans(self): 15 reality = src.telephone.text(False) 16 my_expectation = 'I got: False' 17 assert reality == my_expectation 18 self.assertNotEqual(reality, my_expectation) 19 20 reality = src.telephone.text(True) 21 my_expectation = 'I got: True' 22 assert reality == my_expectation 23 self.assertNotEqual(reality, my_expectation) 24 25 26def 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
13 # def test_passing_booleans(): 14 def test_passing_booleans(self): 15 reality = src.telephone.text(False) 16 my_expectation = 'I got: False' 17 assert reality == my_expectation 18 # self.assertNotEqual(reality, my_expectation) 19 self.assertEqual(reality, my_expectation) 20 21 reality = src.telephone.text(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
21 reality = src.telephone.text(True) 22 my_expectation = 'I got: True' 23 assert reality == my_expectation 24 # self.assertNotEqual(reality, my_expectation) 25 self.assertEqual(reality, my_expectation) 26 27 28def test_passing_an_integer():the test passes.
I remove the commented lines
11 self.assertEqual(reality, my_expectation) 12 13 def test_passing_booleans(self): 14 reality = src.telephone.text(False) 15 my_expectation = 'I got: False' 16 assert reality == my_expectation 17 self.assertEqual(reality, my_expectation) 18 19 reality = src.telephone.text(True) 20 my_expectation = 'I got: True' 21 assert reality == my_expectation 22 self.assertEqual(reality, my_expectation) 23 24 25def 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
22 self.assertEqual(reality, my_expectation) 23 24 def test_passing_an_integer(): 25 an_integer = 1234 26 27 reality = src.telephone.text(an_integer) 28 my_expectation = f'I got: {an_integer}' 29 assert reality == my_expectation 30 31 32def 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 givenbecause 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
22 self.assertEqual(reality, my_expectation)
23
24 # def test_passing_an_integer():
25 def test_passing_an_integer(self):
green.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_an_integer
22 self.assertEqual(reality, my_expectation) 23 24 # def test_passing_an_integer(): 25 def test_passing_an_integer(self): 26 an_integer = 1234 27 28 reality = src.telephone.text(an_integer) 29 my_expectation = f'I got: {an_integer}' 30 assert reality == my_expectation 31 self.assertNotEqual(reality, my_expectation) 32 33 34def 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
22 self.assertEqual(reality, my_expectation) 23 24 # def test_passing_an_integer(): 25 def test_passing_an_integer(self): 26 an_integer = 1234 27 28 reality = src.telephone.text(an_integer) 29 my_expectation = f'I got: {an_integer}' 30 assert reality == my_expectation 31 # self.assertNotEqual(reality, my_expectation) 32 self.assertEqual(reality, my_expectation) 33 34 35def test_passing_a_float():the test passes.
I remove the commented lines
22 self.assertEqual(reality, my_expectation) 23 24 def test_passing_an_integer(self): 25 an_integer = 1234 26 27 reality = src.telephone.text(an_integer) 28 my_expectation = f'I got: {an_integer}' 29 assert reality == my_expectation 30 self.assertEqual(reality, my_expectation) 31 32 33def 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
30 self.assertEqual(reality, my_expectation) 31 32 def test_passing_a_float(): 33 a_float = 5.678 34 35 reality = src.telephone.text(a_float) 36 my_expectation = f'I got: {a_float}' 37 assert reality == my_expectation 38 39 40def 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 givenbecause a method …
GREEN: make it pass
I add self to the parentheses of test_passing_a_float
30 self.assertEqual(reality, my_expectation)
31
32 # def test_passing_a_float():
33 def test_passing_a_float(self):
green again.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_a_float
30 self.assertEqual(reality, my_expectation) 31 32 # def test_passing_a_float(): 33 def test_passing_a_float(self): 34 a_float = 5.678 35 36 reality = src.telephone.text(a_float) 37 my_expectation = f'I got: {a_float}' 38 assert reality == my_expectation 39 self.assertNotEqual(reality, my_expectation) 40 41 42def 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
30 self.assertEqual(reality, my_expectation) 31 32 # def test_passing_a_float(): 33 def test_passing_a_float(self): 34 a_float = 5.678 35 36 reality = src.telephone.text(a_float) 37 my_expectation = f'I got: {a_float}' 38 assert reality == my_expectation 39 # self.assertNotEqual(reality, my_expectation) 40 self.assertEqual(reality, my_expectation) 41 42 43def test_passing_a_string():the test passes.
I remove the commented lines
30 self.assertEqual(reality, my_expectation) 31 32 def test_passing_a_float(self): 33 a_float = 5.678 34 35 reality = src.telephone.text(a_float) 36 my_expectation = f'I got: {a_float}' 37 assert reality == my_expectation 38 self.assertEqual(reality, my_expectation) 39 40 41def 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
38 self.assertEqual(reality, my_expectation) 39 40 def test_passing_a_string(): 41 a_string = 'hello' 42 43 reality = src.telephone.text(a_string) 44 my_expectation = f'I got: {a_string}' 45 assert reality == my_expectation 46 47 48def 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 givenbecause 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
38 self.assertEqual(reality, my_expectation)
39
40 # def test_passing_a_string():
41 def test_passing_a_string(self):
the test is green again.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_a_string
38 self.assertEqual(reality, my_expectation) 39 40 # def test_passing_a_string(): 41 def test_passing_a_string(self): 42 a_string = 'hello' 43 44 reality = src.telephone.text(a_string) 45 my_expectation = f'I got: {a_string}' 46 assert reality == my_expectation 47 self.assertNotEqual(reality, my_expectation) 48 49 50def 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
38 self.assertEqual(reality, my_expectation) 39 40 # def test_passing_a_string(): 41 def test_passing_a_string(self): 42 a_string = 'hello' 43 44 reality = src.telephone.text(a_string) 45 my_expectation = f'I got: {a_string}' 46 assert reality == my_expectation 47 # self.assertNotEqual(reality, my_expectation) 48 self.assertEqual(reality, my_expectation) 49 50 51def test_passing_a_tuple():the test passes.
I remove the commented lines
38 self.assertEqual(reality, my_expectation) 39 40 def test_passing_a_string(self): 41 a_string = 'hello' 42 43 reality = src.telephone.text(a_string) 44 my_expectation = f'I got: {a_string}' 45 assert reality == my_expectation 46 self.assertEqual(reality, my_expectation) 47 48 49def 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
46 self.assertEqual(reality, my_expectation) 47 48 def test_passing_a_tuple(): 49 a_tuple = (0, 1, 2, 'n') 50 51 reality = src.telephone.text(a_tuple) 52 my_expectation = f'I got: {a_tuple}' 53 assert reality == my_expectation 54 55 56def 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
46 self.assertEqual(reality, my_expectation)
47
48 # def test_passing_a_tuple():
49 def test_passing_a_tuple(self):
green again.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_a_tuple
46 self.assertEqual(reality, my_expectation) 47 48 # def test_passing_a_tuple(): 49 def test_passing_a_tuple(self): 50 a_tuple = (0, 1, 2, 'n') 51 52 reality = src.telephone.text(a_tuple) 53 my_expectation = f'I got: {a_tuple}' 54 assert reality == my_expectation 55 self.assertNotEqual(reality, my_expectation) 56 57 58def 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
46 self.assertEqual(reality, my_expectation) 47 48 # def test_passing_a_tuple(): 49 def test_passing_a_tuple(self): 50 a_tuple = (0, 1, 2, 'n') 51 52 reality = src.telephone.text(a_tuple) 53 my_expectation = f'I got: {a_tuple}' 54 assert reality == my_expectation 55 # self.assertNotEqual(reality, my_expectation) 56 self.assertEqual(reality, my_expectation) 57 58 59def test_passing_a_list():the test passes.
I remove the commented lines
46 self.assertEqual(reality, my_expectation) 47 48 def test_passing_a_tuple(self): 49 a_tuple = (0, 1, 2, 'n') 50 51 reality = src.telephone.text(a_tuple) 52 my_expectation = f'I got: {a_tuple}' 53 assert reality == my_expectation 54 self.assertEqual(reality, my_expectation) 55 56 57def 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
54 self.assertEqual(reality, my_expectation) 55 56 def test_passing_a_list(): 57 a_list = [0, 1, 2, 'n'] 58 59 reality = src.telephone.text(a_list) 60 my_expectation = f'I got: {a_list}' 61 assert reality == my_expectation 62 63 64def 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
54 self.assertEqual(reality, my_expectation)
55
56 # def test_passing_a_list():
57 def test_passing_a_list(self):
green.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_a_list
54 self.assertEqual(reality, my_expectation) 55 56 # def test_passing_a_list(): 57 def test_passing_a_list(self): 58 a_list = [0, 1, 2, 'n'] 59 60 reality = src.telephone.text(a_list) 61 my_expectation = f'I got: {a_list}' 62 assert reality == my_expectation 63 self.assertNotEqual(reality, my_expectation) 64 65 66def 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
54 self.assertEqual(reality, my_expectation) 55 56 # def test_passing_a_list(): 57 def test_passing_a_list(self): 58 a_list = [0, 1, 2, 'n'] 59 60 reality = src.telephone.text(a_list) 61 my_expectation = f'I got: {a_list}' 62 assert reality == my_expectation 63 # self.assertNotEqual(reality, my_expectation) 64 self.assertEqual(reality, my_expectation) 65 66 67def test_passing_a_set():the test passes.
I remove the commented lines
54 self.assertEqual(reality, my_expectation) 55 56 def test_passing_a_list(self): 57 a_list = [0, 1, 2, 'n'] 58 59 reality = src.telephone.text(a_list) 60 my_expectation = f'I got: {a_list}' 61 assert reality == my_expectation 62 self.assertEqual(reality, my_expectation) 63 64 65def 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
62 self.assertEqual(reality, my_expectation) 63 64 def test_passing_a_set(): 65 a_set = {0, 1, 2, 'n'} 66 67 reality = src.telephone.text(a_set) 68 my_expectation = f'I got: {a_set}' 69 assert reality == my_expectation 70 71 72def 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
62 self.assertEqual(reality, my_expectation)
63
64 # def test_passing_a_set():
65 def test_passing_a_set(self):
green again.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_a_set
62 self.assertEqual(reality, my_expectation) 63 64 # def test_passing_a_set(): 65 def test_passing_a_set(self): 66 a_set = {0, 1, 2, 'n'} 67 68 reality = src.telephone.text(a_set) 69 my_expectation = f'I got: {a_set}' 70 assert reality == my_expectation 71 self.assertNotEqual(reality, my_expectation) 72 73 74def 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
62 self.assertEqual(reality, my_expectation) 63 64 # def test_passing_a_set(): 65 def test_passing_a_set(self): 66 a_set = {0, 1, 2, 'n'} 67 68 reality = src.telephone.text(a_set) 69 my_expectation = f'I got: {a_set}' 70 assert reality == my_expectation 71 # self.assertNotEqual(reality, my_expectation) 72 self.assertEqual(reality, my_expectation) 73 74 75def test_passing_a_dictionary():the test passes.
I remove the commented lines
62 self.assertEqual(reality, my_expectation) 63 64 def test_passing_a_set(self): 65 a_set = {0, 1, 2, 'n'} 66 67 reality = src.telephone.text(a_set) 68 my_expectation = f'I got: {a_set}' 69 assert reality == my_expectation 70 self.assertEqual(reality, my_expectation) 71 72 73def 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
70 self.assertEqual(reality, my_expectation) 71 72 def test_passing_a_dictionary(): 73 a_dictionary = { 74 'key0': 'value0', 75 'keyN': [0, 1, 2, 'n'], 76 } 77 78 reality = src.telephone.text(a_dictionary) 79 my_expectation = f'I got: {a_dictionary}' 80 assert reality == my_expectation 81 82 83def 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
70 self.assertEqual(reality, my_expectation)
71
72 # def test_passing_a_dictionary():
73 def test_passing_a_dictionary(self):
the test is green again.
REFACTOR: make it better
I add a call to the assertNotEqual method for the assertion in test_passing_a_dictionary
70 self.assertEqual(reality, my_expectation) 71 72 # def test_passing_a_dictionary(): 73 def test_passing_a_dictionary(self): 74 a_dictionary = { 75 'key0': 'value0', 76 'keyN': [0, 1, 2, 'n'], 77 } 78 79 reality = src.telephone.text(a_dictionary) 80 my_expectation = f'I got: {a_dictionary}' 81 assert reality == my_expectation 82 self.assertNotEqual(reality, my_expectation) 83 84 85def 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
70 self.assertEqual(reality, my_expectation) 71 72 # def test_passing_a_dictionary(): 73 def test_passing_a_dictionary(self): 74 a_dictionary = { 75 'key0': 'value0', 76 'keyN': [0, 1, 2, 'n'], 77 } 78 79 reality = src.telephone.text(a_dictionary) 80 my_expectation = f'I got: {a_dictionary}' 81 assert reality == my_expectation 82 # self.assertNotEqual(reality, my_expectation) 83 self.assertEqual(reality, my_expectation) 84 85 86def test_passing_a_class():the test passes.
I remove the commented lines
70 self.assertEqual(reality, my_expectation) 71 72 def test_passing_a_dictionary(self): 73 a_dictionary = { 74 'key0': 'value0', 75 'keyN': [0, 1, 2, 'n'], 76 } 77 78 reality = src.telephone.text(a_dictionary) 79 my_expectation = f'I got: {a_dictionary}' 80 assert reality == my_expectation 81 self.assertEqual(reality, my_expectation) 82 83 84def 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
81 self.assertEqual(reality, my_expectation) 82 83 def test_passing_a_class(): 84 reality = src.telephone.text(object) 85 my_expectation = "I got: <class 'object'>" 86 assert reality == my_expectation 87 88 reality = src.telephone.text(bool) 89 my_expectation = "I got: <class 'bool'>" 90 assert reality == my_expectation 91 92 reality = src.telephone.text(int) 93 my_expectation = "I got: <class 'int'>" 94 assert reality == my_expectation 95 96 reality = src.telephone.text(float) 97 my_expectation = "I got: <class 'float'>" 98 assert reality == my_expectation 99 100 reality = src.telephone.text(str) 101 my_expectation = "I got: <class 'str'>" 102 assert reality == my_expectation 103 104 reality = src.telephone.text(tuple) 105 my_expectation = "I got: <class 'tuple'>" 106 assert reality == my_expectation 107 108 reality = src.telephone.text(list) 109 my_expectation = "I got: <class 'list'>" 110 assert reality == my_expectation 111 112 reality = src.telephone.text(set) 113 my_expectation = "I got: <class 'set'>" 114 assert reality == my_expectation 115 116 reality = src.telephone.text(dict) 117 my_expectation = "I got: <class 'dict'>" 118 assert reality == my_expectation 119 120 121# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: TestTelephone.test_passing_a_class() takes 0 positional arguments but 1 was givenbecause 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
81 self.assertEqual(reality, my_expectation)
82
83 # def test_passing_a_class():
84 def test_passing_a_class(self):
green again.
REFACTOR: make it better
I add calls to the assertNotEqual method for the assertions in test_passing_a_class
81 self.assertEqual(reality, my_expectation) 82 83 # def test_passing_a_class(): 84 def test_passing_a_class(self): 85 reality = src.telephone.text(object) 86 my_expectation = "I got: <class 'object'>" 87 assert reality == my_expectation 88 self.assertNotEqual(reality, my_expectation) 89 90 reality = src.telephone.text(bool) 91 my_expectation = "I got: <class 'bool'>" 92 assert reality == my_expectation 93 self.assertNotEqual(reality, my_expectation) 94 95 reality = src.telephone.text(int) 96 my_expectation = "I got: <class 'int'>" 97 assert reality == my_expectation 98 self.assertNotEqual(reality, my_expectation) 99 100 reality = src.telephone.text(float) 101 my_expectation = "I got: <class 'float'>" 102 assert reality == my_expectation 103 self.assertNotEqual(reality, my_expectation) 104 105 reality = src.telephone.text(str) 106 my_expectation = "I got: <class 'str'>" 107 assert reality == my_expectation 108 self.assertNotEqual(reality, my_expectation) 109 110 reality = src.telephone.text(tuple) 111 my_expectation = "I got: <class 'tuple'>" 112 assert reality == my_expectation 113 self.assertNotEqual(reality, my_expectation) 114 115 reality = src.telephone.text(list) 116 my_expectation = "I got: <class 'list'>" 117 assert reality == my_expectation 118 self.assertNotEqual(reality, my_expectation) 119 120 reality = src.telephone.text(set) 121 my_expectation = "I got: <class 'set'>" 122 assert reality == my_expectation 123 self.assertNotEqual(reality, my_expectation) 124 125 reality = src.telephone.text(dict) 126 my_expectation = "I got: <class 'dict'>" 127 assert reality == my_expectation 128 self.assertNotEqual(reality, my_expectation) 129 130 131# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'object'>" == "I got: <class 'object'>"I change assertNotEqual to assertEqual for the assertion for object
81 self.assertEqual(reality, my_expectation) 82 83 # def test_passing_a_class(): 84 def test_passing_a_class(self): 85 reality = src.telephone.text(object) 86 my_expectation = "I got: <class 'object'>" 87 assert reality == my_expectation 88 # self.assertNotEqual(reality, my_expectation) 89 self.assertEqual(reality, my_expectation) 90 91 reality = src.telephone.text(bool)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'bool'>" == "I got: <class 'bool'>"I change assertNotEqual to assertEqual for the assertion for bool
91 reality = src.telephone.text(bool) 92 my_expectation = "I got: <class 'bool'>" 93 assert reality == my_expectation 94 # self.assertNotEqual(reality, my_expectation) 95 self.assertEqual(reality, my_expectation) 96 97 reality = src.telephone.text(int)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'int'>" == "I got: <class 'int'>"I change assertNotEqual to assertEqual for the assertion for int
97 reality = src.telephone.text(int) 98 my_expectation = "I got: <class 'int'>" 99 assert reality == my_expectation 100 # self.assertNotEqual(reality, my_expectation) 101 self.assertEqual(reality, my_expectation) 102 103 reality = src.telephone.text(float)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'float'>" == "I got: <class 'float'>"I change assertNotEqual to assertEqual for the assertion for float
103 reality = src.telephone.text(float) 104 my_expectation = "I got: <class 'float'>" 105 assert reality == my_expectation 106 # self.assertNotEqual(reality, my_expectation) 107 self.assertEqual(reality, my_expectation) 108 109 reality = src.telephone.text(str)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'str'>" == "I got: <class 'str'>"I change assertNotEqual to assertEqual for the assertion for str
109 reality = src.telephone.text(str) 110 my_expectation = "I got: <class 'str'>" 111 assert reality == my_expectation 112 # self.assertNotEqual(reality, my_expectation) 113 self.assertEqual(reality, my_expectation) 114 115 reality = src.telephone.text(tuple)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'tuple'>" == "I got: <class 'tuple'>"I change assertNotEqual to assertEqual for the assertion for tuple
115 reality = src.telephone.text(tuple) 116 my_expectation = "I got: <class 'tuple'>" 117 assert reality == my_expectation 118 # self.assertNotEqual(reality, my_expectation) 119 self.assertEqual(reality, my_expectation) 120 121 reality = src.telephone.text(list)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'list'>" == "I got: <class 'list'>"I change assertNotEqual to assertEqual for the assertion for list
121 reality = src.telephone.text(list) 122 my_expectation = "I got: <class 'list'>" 123 assert reality == my_expectation 124 # self.assertNotEqual(reality, my_expectation) 125 self.assertEqual(reality, my_expectation) 126 127 reality = src.telephone.text(set)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'set'>" == "I got: <class 'set'>"I change assertNotEqual to assertEqual for the assertion for set
127 reality = src.telephone.text(set) 128 my_expectation = "I got: <class 'set'>" 129 assert reality == my_expectation 130 # self.assertNotEqual(reality, my_expectation) 131 self.assertEqual(reality, my_expectation) 132 133 reality = src.telephone.text(dict)the terminal is my friend, and shows AssertionError
AssertionError: "I got: <class 'dict'>" == "I got: <class 'dict'>"I change assertNotEqual to assertEqual for the assertion for dict
133 reality = src.telephone.text(dict) 134 my_expectation = "I got: <class 'dict'>" 135 assert reality == my_expectation 136 # self.assertNotEqual(reality, my_expectation) 137 self.assertEqual(reality, my_expectation) 138 139 140# Exceptions seenthe test passes.
I remove the commented lines
81 self.assertEqual(reality, my_expectation) 82 83 def test_passing_a_class(self): 84 reality = src.telephone.text(object) 85 my_expectation = "I got: <class 'object'>" 86 assert reality == my_expectation 87 self.assertEqual(reality, my_expectation) 88 89 reality = src.telephone.text(bool) 90 my_expectation = "I got: <class 'bool'>" 91 assert reality == my_expectation 92 self.assertEqual(reality, my_expectation) 93 94 reality = src.telephone.text(int) 95 my_expectation = "I got: <class 'int'>" 96 assert reality == my_expectation 97 self.assertEqual(reality, my_expectation) 98 99 reality = src.telephone.text(float) 100 my_expectation = "I got: <class 'float'>" 101 assert reality == my_expectation 102 self.assertEqual(reality, my_expectation) 103 104 reality = src.telephone.text(str) 105 my_expectation = "I got: <class 'str'>" 106 assert reality == my_expectation 107 self.assertEqual(reality, my_expectation) 108 109 reality = src.telephone.text(tuple) 110 my_expectation = "I got: <class 'tuple'>" 111 assert reality == my_expectation 112 self.assertEqual(reality, my_expectation) 113 114 reality = src.telephone.text(list) 115 my_expectation = "I got: <class 'list'>" 116 assert reality == my_expectation 117 self.assertEqual(reality, my_expectation) 118 119 reality = src.telephone.text(set) 120 my_expectation = "I got: <class 'set'>" 121 assert reality == my_expectation 122 self.assertEqual(reality, my_expectation) 123 124 reality = src.telephone.text(dict) 125 my_expectation = "I got: <class 'dict'>" 126 assert reality == my_expectation 127 self.assertEqual(reality, my_expectation) 128 129 130# Exceptions seen 131# AssertionError 132# NameError 133# TypeError 134# ModuleNotFoundError 135# AttributeErrorI 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.pyI 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
telephonecd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
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
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.