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
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')
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 )
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 )
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}')
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)
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
the terminal shows I am in the
telephonefolder.../pumping_python/telephoneI open
test_telephone.pyI use pytest-watcher to run the tests automatically
the terminal shows
test_telephone.py .......... [100%] =================== 10 passed in D.EFs ===================
add TestTelephone class
RED: make it fail
I add an object named
Telephonetotest_telephone.py1import 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
TestTelephonethe terminal is my friend, and shows AttributeError
GREEN: make it pass
I add unittest.TestCase as the parent object of
TestTelephonethe terminal is my friend, and shows NameError
- caption:
terminal
- 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.textthe terminal is my friend, and shows AssertionError
I change False to True in test_failure
the test passes.
REFACTOR: make it better
test_passing_none with unittest
RED: make it fail
I go back to the terminal where the tests are running
I remove test_failure
I move test_passing_none to make it a method of the TestTelephone class
the terminal is my friend, and shows TypeError
because a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_none
the test passes.
I remove the commented lines from test_passing_none
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
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
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual for the first assertion in test_passing_booleans
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual for the second assertion in test_passing_booleans
the test passes.
I remove the commented lines from test_passing_booleans
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
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 object (
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_an_integer
the test passes.
I remove the commented lines from test_passing_an_integer
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
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
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_a_float
the test passes.
I remove the commented lines from test_passing_a_float
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
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 object (
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_a_string
the test passes.
I remove the commented lines from test_passing_a_string
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
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_a_tuple
the test passes.
I remove the commented lines from test_passing_a_tuple
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
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_a_list
the test passes.
I remove the commented lines from test_passing_a_list
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
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_a_set
the test passes.
I remove the commented lines from test_passing_a_set
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
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
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_a_dictionary
the test passes.
I remove the commented lines from test_passing_a_dictionary
test_passing_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_passing_an_object to make it a method of the TestTelephone class
the terminal is my friend, and shows TypeError
TypeError: TestTelephone.test_passing_an_object() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the object (
self) it belongs to as the first argument.
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
I change the calls to my assert_equal function to the assertNotEqual method of the unittest.TestCase class for the assertions in test_passing_an_object
the terminal is my friend, and shows AssertionError
I change assertNotEqual to assertEqual in test_passing_an_object
the test passes.
I remove the commented lines from test_passing_an_object
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.
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
telephonethe 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.