test objects with unittest
I want to use the unittest library in the class project.
preview
I have these tests by the end of the chapter
1import unittest
2
3
4class WPass: pass
5
6
7class WParentheses(): pass
8
9
10class WObject(object): pass
13class Testobjects(unittest.TestCase):
14
15 def test_making_an_object_w_pass(self):
16 assert isinstance(WPass(), object)
17 self.assertIsInstance(WPass(), object)
18
19 assert issubclass(WPass, object)
20 self.assertIsSubclass(WPass, object)
21
22 def test_making_an_object_w_parentheses(self):
23 assert isinstance(WParentheses(), object)
24 self.assertIsInstance(WParentheses(), object)
25
26 assert issubclass(WParentheses, object)
27 self.assertIsSubclass(WParentheses, object)
29 def test_making_an_object_w_object(self):
30 assert isinstance(WObject(), object)
31 self.assertIsInstance(WObject(), object)
32
33 assert issubclass(WObject, object)
34 self.assertIsSubclass(WObject, object)
35
36 def test_is_none_an_object(self):
37 assert isinstance(None, object)
38 self.assertIsInstance(None, object)
39
40 # fails because None is not a class
41 # assert issubclass(None, object)
42 # self.assertIsSubclass(None, object)
44 def test_is_a_boolean_an_object(self):
45 assert isinstance(bool, object)
46 self.assertIsInstance(bool, object)
47
48 assert issubclass(bool, object)
49 self.assertIsSubclass(bool, object)
50
51 def test_is_an_integer_an_object(self):
52 assert isinstance(int, object)
53 self.assertIsInstance(int, object)
54
55 assert issubclass(int, object)
56 self.assertIsSubclass(int, object)
58 def test_is_a_float_an_object(self):
59 assert isinstance(float, object)
60 self.assertIsInstance(float, object)
61
62 assert issubclass(float, object)
63 self.assertIsSubclass(float, object)
64
65 def test_is_a_string_an_object(self):
66 assert isinstance(str, object)
67 self.assertIsInstance(str, object)
68
69 assert issubclass(str, object)
70 self.assertIsSubclass(str, object)
72 def test_is_a_tuple_an_object(self):
73 assert isinstance(tuple, object)
74 self.assertIsInstance(tuple, object)
75
76 assert issubclass(tuple, object)
77 self.assertIsSubclass(tuple, object)
78
79 def test_is_a_list_an_object(self):
80 assert isinstance(list, object)
81 self.assertIsInstance(list, object)
82
83 assert issubclass(list, object)
84 self.assertIsSubclass(list, object)
86 def test_is_a_set_an_object(self):
87 assert isinstance(set, object)
88 self.assertIsInstance(set, object)
89
90 assert issubclass(set, object)
91 self.assertIsSubclass(set, object)
92
93 def test_is_a_dictionary_an_object(self):
94 assert isinstance(dict, object)
95 self.assertIsInstance(dict, object)
96
97 assert issubclass(dict, object)
98 self.assertIsSubclass(dict, object)
100 def test_dir_object(self):
101 reality = dir(object)
102 my_expectation = [
103 '__class__', '__delattr__', '__dir__',
104 '__doc__', '__eq__', '__format__',
105 '__ge__', '__getattribute__',
106 '__getstate__', '__gt__', '__hash__',
107 '__init__', '__init_subclass__', '__le__',
108 '__lt__', '__ne__', '__new__', '__reduce__',
109 '__reduce_ex__', '__repr__', '__setattr__',
110 '__sizeof__', '__str__', '__subclasshook__'
111 ]
112 assert reality == my_expectation
113 self.assertEqual(reality, my_expectation)
114
115
116# Exceptions seen
117# AssertionError
118# NameError
119# TypeError
120# AttributeError
open the project
I open a terminal
I change directory to the project
cd objectsthe terminal shows I am in the
objectsfolder.../pumping_python/objectsI open
test_objects.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
test_objects.py ............. [100%] =================== 13 passed in J.KLs ===================
add Testobjects class
RED: make it fail
I add a class named
objectstotest_objects.py1class WPass: pass 2 3 4class WParentheses(): pass 5 6 7class WObject(object): pass 8 9 10class objects(object): 11 12 def test_failure(self): 13 self.assertEqual(True, False) 14 15 16def test_making_an_object_w_pass():the test is still green.
I change the name of the class to
Testobjects7class WObject(object): pass 8 9 10# class objects(object): 11class Testobjects(object): 12 13 def test_failure(self): 14 self.assertEqual(True, False)the terminal is my friend, and shows AttributeError
AttributeError: 'Testobjects' object has no attribute 'assertEqual'I add AttributeError to the list of Exceptions seen
93# Exceptions seen 94# AssertionError 95# NameError 96# TypeError 97# AttributeError
GREEN: make it pass
I add unittest.TestCase as the parent class of
Testobjects7class WObject(object): pass 8 9 10# class objects(object): 11# class Testobjects(object): 12class Testobjects(unittest.TestCase): 13 14 def test_failure(self): 15 self.assertEqual(True, False)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 unittest 2 3 4class WPass: passthe terminal is my friend, and shows AssertionError
AssertionError: True != FalseI change False to True in the assertion
13# class objects(object): 14# class Testobjects(object): 15class Testobjects(unittest.TestCase): 16 17 def test_failure(self): 18 # self.assertEqual(True, False) 19 self.assertEqual(True, True) 20 21 22def test_making_an_object_w_pass():the test passes.
REFACTOR: make it better
I remove the commented lines
10class WObject(object): pass 11 12 13class Testobjects(unittest.TestCase): 14 15 def test_failure(self): 16 self.assertEqual(True, True) 17 18 19def test_making_an_object_w_pass():I open a new terminal then make sure I am in the
objectsfoldercd objectsI add a git commit message in the new terminal
git commit -am \ 'add Testobjects class'
test_making_an_object_w_pass with unittest
RED: make it fail
I go back to the terminal where the tests are running
I remove test_failure
I move test_making_an_object_w_pass to make it a method of the Testobjects class .. code-block:: python
- lineno-start:
13
- emphasize-lines:
3-5
class Testobjects(unittest.TestCase):
- def test_making_an_object_w_pass():
assert isinstance(WPass(), object) assert issubclass(WPass, object)
def test_making_an_object_w_parentheses():
the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_making_an_object_w_pass() 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_making_an_object_w_pass
13class Testobjects(unittest.TestCase):
14
15 # def test_making_an_object_w_pass():
16 def test_making_an_object_w_pass(self):
17 assert isinstance(WPass(), object)
18 assert issubclass(WPass, object)
the test is green again.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
15 # def test_making_an_object_w_pass(): 16 def test_making_an_object_w_pass(self): 17 assert isinstance(WPass(), object) 18 self.assertNotIsInstance(WPass(), object) 19 20 assert issubclass(WPass, object)the terminal is my friend, and shows AssertionError
AssertionError: <tests.test_objects.WPass object at 0xffff01234a567> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
15 # def test_making_an_object_w_pass(): 16 def test_making_an_object_w_pass(self): 17 assert isinstance(WPass(), object) 18 # self.assertNotIsInstance(WPass(), object) 19 self.assertIsInstance(WPass(), object) 20 21 assert issubclass(WPass, object)the test passes.
I add a call to the assertNotIsSubclass method
21 assert issubclass(WPass, object) 22 self.assertNotIsSubclass(WPass, object) 23 24 25def test_making_an_object_w_parentheses():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tests.test_objects.WPass'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
21 assert issubclass(WPass, object) 22 # self.assertNotIsSubclass(WPass, object) 23 self.assertIsSubclass(WPass, object) 24 25 26def test_making_an_object_w_parentheses():the test passes.
I remove the commented lines from test_making_an_object_w_pass
13class Testobjects(unittest.TestCase): 14 15 def test_making_an_object_w_pass(self): 16 assert isinstance(WPass(), object) 17 self.assertIsInstance(WPass(), object) 18 19 assert issubclass(WPass, object) 20 self.assertIsSubclass(WPass, object) 21 22 23def test_making_an_object_w_parentheses():I add a git commit message in the other terminal
git commit -am \ 'move test_making_an_object_w_pass to Testobjects'
test_making_an_object_w_parentheses with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_making_an_object_w_parentheses to make it a method of the Testobjects class
19 assert issubclass(WPass, object) 20 self.assertIsSubclass(WPass, object) 21 22 def test_making_an_object_w_parentheses(): 23 assert isinstance(WParentheses(), object) 24 assert issubclass(WParentheses, object) 25 26 27def test_making_an_object_w_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_making_an_object_w_parentheses() 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_making_an_object_w_parentheses
19 assert issubclass(WPass, object)
20 self.assertIsSubclass(WPass, object)
21
22 # def test_making_an_object_w_parentheses():
23 def test_making_an_object_w_parentheses(self):
24 assert isinstance(WParentheses(), object)
25 assert issubclass(WParentheses, object)
green again.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
22 # def test_making_an_object_w_parentheses(): 23 def test_making_an_object_w_parentheses(self): 24 assert isinstance(WParentheses(), object) 25 self.assertNotIsInstance( 26 WParentheses(), object 27 ) 28 29 assert issubclass(WParentheses, object)the terminal is my friend, and shows AssertionError
AssertionError: <tests.test_objects.WParentheses object at 0xffff45ab67cd8> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
22 # def test_making_an_object_w_parentheses(): 23 def test_making_an_object_w_parentheses(self): 24 assert isinstance(WParentheses(), object) 25 # self.assertNotIsInstance( 26 self.assertIsInstance( 27 WParentheses(), object 28 ) 29 30 assert issubclass(WParentheses, object)the test passes.
I add a call to the assertNotIsSubclass method
30 assert issubclass(WParentheses, object) 31 self.assertNotIsSubclass( 32 WParentheses, object 33 ) 34 35 36def test_making_an_object_w_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tests.test_objects.WParentheses'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
30 assert issubclass(WParentheses, object) 31 # self.assertNotIsSubclass( 32 self.assertIsSubclass( 33 WParentheses, object 34 ) 35 36 37def test_making_an_object_w_object():the test passes.
I remove the commented lines from test_making_an_object_w_parentheses
19 assert issubclass(WPass, object) 20 self.assertIsSubclass(WPass, object) 21 22 def test_making_an_object_w_parentheses(self): 23 assert isinstance(WParentheses(), object) 24 self.assertIsInstance(WParentheses(), object) 25 26 assert issubclass(WParentheses, object) 27 self.assertIsSubclass(WParentheses, object) 28 29 30def test_making_an_object_w_object():I add a git commit message in the other terminal
git commit -am \ 'move test_making_an_object_w_parentheses to Testobjects'
test_making_an_object_w_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_making_an_object_w_object to make it a method of the Testobjects class
26 assert issubclass(WParentheses, object) 27 self.assertIsSubclass(WParentheses, object) 28 29 def test_making_an_object_w_object(): 30 assert isinstance(WObject(), object) 31 assert issubclass(WObject, object) 32 33 34def test_is_none_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_making_an_object_w_object() 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_making_an_object_w_object
green.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
29 # def test_making_an_object_w_object(): 30 def test_making_an_object_w_object(self): 31 assert isinstance(WObject(), object) 32 self.assertNotIsInstance(WObject(), object) 33 34 assert issubclass(WObject, object)the terminal is my friend, and shows AssertionError
AssertionError: <tests.test_objects.WObject object at 0xffff345a6b789> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
29 # def test_making_an_object_w_object(): 30 def test_making_an_object_w_object(self): 31 assert isinstance(WObject(), object) 32 # self.assertNotIsInstance(WObject(), object) 33 self.assertIsInstance(WObject(), object) 34 35 assert issubclass(WObject, object)the test passes.
I add a call to the assertNotIsSubclass method
35 assert issubclass(WObject, object) 36 self.assertNotIsSubclass(WObject, object) 37 38 39def test_is_none_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tests.test_objects.WObject'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
35 assert issubclass(WObject, object) 36 # self.assertNotIsSubclass(WObject, object) 37 self.assertIsSubclass(WObject, object) 38 39 40def test_is_none_an_object():the test passes.
I remove the commented lines from test_making_an_object_w_object
26 assert issubclass(WParentheses, object) 27 self.assertIsSubclass(WParentheses, object) 28 29 def test_making_an_object_w_object(self): 30 assert isinstance(WObject(), object) 31 self.assertIsInstance(WObject(), object) 32 33 assert issubclass(WObject, object) 34 self.assertIsSubclass(WObject, object) 35 36 37def test_is_none_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_making_an_object_w_object to Testobjects'
test_is_none_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_none_an_object to make it a method of the Testobjects class
33 assert issubclass(WObject, object) 34 self.assertIsSubclass(WObject, object) 35 36 def test_is_none_an_object(): 37 assert isinstance(None, object) 38 # fails because None is not a class 39 # assert issubclass(None, object) 40 41 42def test_is_a_boolean_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_none_an_object() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_is_none_an_object
33 assert issubclass(WObject, object)
34 self.assertIsSubclass(WObject, object)
35
36 # def test_is_none_an_object():
37 def test_is_none_an_object(self):
38 assert isinstance(None, object)
39 # fails because None is not a class
40 # assert issubclass(None, object)
green again.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
36 # def test_is_none_an_object(): 37 def test_is_none_an_object(self): 38 assert isinstance(None, object) 39 self.assertNotIsInstance(None, object) 40 41 # fails because None is not a class 42 # assert issubclass(None, object)the terminal is my friend, and shows AssertionError
AssertionError: None is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
36 # def test_is_none_an_object(): 37 def test_is_none_an_object(self): 38 assert isinstance(None, object) 39 # self.assertNotIsInstance(None, object) 40 self.assertIsInstance(None, object) 41 42 # fails because None is not a class 43 # assert issubclass(None, object)the test passes.
I add a call to the assertIsSubclass method
42 # fails because None is not a class 43 # assert issubclass(None, object) 44 self.assertIsSubclass(None, object) 45 46 47def test_is_a_boolean_an_object():the terminal is my friend, and shows AssertionError
AssertionError: None is not a classcompare this with the error message for
assert issubclass(None, object)TypeError: issubclass() arg 1 must be a classI comment out the assertion
42 # fails because None is not a class 43 # assert issubclass(None, object) 44 # self.assertIsSubclass(None, object) 45 46 47def test_is_a_boolean_an_object():the test passes.
I remove the other commented lines from test_is_none_an_object
33 assert issubclass(WObject, object) 34 self.assertIsSubclass(WObject, object) 35 36 def test_is_none_an_object(self): 37 assert isinstance(None, object) 38 self.assertIsInstance(None, object) 39 40 # fails because None is not a class 41 # assert issubclass(None, object) 42 # self.assertIsSubclass(None, object) 43 44 45def test_is_a_boolean_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_none_an_object to Testobjects'
test_is_a_boolean_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_boolean_an_object to make it a method of the Testobjects class
40 # fails because None is not a class 41 # assert issubclass(None, object) 42 # self.assertIsSubclass(None, object) 43 44 def test_is_a_boolean_an_object(): 45 assert isinstance(bool, object) 46 assert issubclass(bool, object) 47 48 49def test_is_an_integer_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_boolean_an_object() 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_is_a_boolean_an_object
40 # fails because None is not a class
41 # assert issubclass(None, object)
42 # self.assertIsSubclass(None, object)
43
44 # def test_is_a_boolean_an_object():
45 def test_is_a_boolean_an_object(self):
46 assert isinstance(bool, object)
47 assert issubclass(bool, object)
the test is green again.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
44 # def test_is_a_boolean_an_object(): 45 def test_is_a_boolean_an_object(self): 46 assert isinstance(bool, object) 47 self.assertNotIsInstance(bool, object) 48 49 assert issubclass(bool, object) 50 self.assertNotIsSubclass(bool, object) 51 52 53def test_is_an_integer_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'bool'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
44 # def test_is_a_boolean_an_object(): 45 def test_is_a_boolean_an_object(self): 46 assert isinstance(bool, object) 47 # self.assertNotIsInstance(bool, object) 48 self.assertIsInstance(bool, object) 49 50 assert issubclass(bool, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'bool'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
50 assert issubclass(bool, object) 51 # self.assertNotIsSubclass(bool, object) 52 self.assertIsSubclass(bool, object) 53 54 55def test_is_an_integer_an_object():the test passes.
I remove the commented lines from test_is_a_boolean_an_object
40 # fails because None is not a class 41 # assert issubclass(None, object) 42 # self.assertIsSubclass(None, object) 43 44 def test_is_a_boolean_an_object(self): 45 assert isinstance(bool, object) 46 self.assertIsInstance(bool, object) 47 48 assert issubclass(bool, object) 49 self.assertIsSubclass(bool, object) 50 51 52def test_is_an_integer_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_boolean_an_object to Testobjects'
test_is_an_integer_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_an_integer_an_object to make it a method of the Testobjects class
48 assert issubclass(bool, object) 49 self.assertIsSubclass(bool, object) 50 51 def test_is_an_integer_an_object(): 52 assert isinstance(int, object) 53 assert issubclass(int, object) 54 55 56def test_is_a_float_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_an_integer_an_object() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_is_an_integer_an_object
48 assert issubclass(bool, object)
49 self.assertIsSubclass(bool, object)
50
51 # def test_is_an_integer_an_object():
52 def test_is_an_integer_an_object(self):
53 assert isinstance(int, object)
54 assert issubclass(int, object)
green again.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
51 # def test_is_an_integer_an_object(): 52 def test_is_an_integer_an_object(self): 53 assert isinstance(int, object) 54 self.assertNotIsInstance(int, object) 55 56 assert issubclass(int, object) 57 self.assertNotIsSubclass(int, object) 58 59 60def test_is_a_float_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'int'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
51 # def test_is_an_integer_an_object(): 52 def test_is_an_integer_an_object(self): 53 assert isinstance(int, object) 54 # self.assertNotIsInstance(int, object) 55 self.assertIsInstance(int, object) 56 57 assert issubclass(int, object) 58 self.assertNotIsSubclass(int, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'int'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
57 assert issubclass(int, object) 58 # self.assertNotIsSubclass(int, object) 59 self.assertIsSubclass(int, object) 60 61 62def test_is_a_float_an_object():the test passes.
I remove the commented lines from test_is_an_integer_an_object
48 assert issubclass(bool, object) 49 self.assertIsSubclass(bool, object) 50 51 def test_is_an_integer_an_object(self): 52 assert isinstance(int, object) 53 self.assertIsInstance(int, object) 54 55 assert issubclass(int, object) 56 self.assertIsSubclass(int, object) 57 58 59def test_is_a_float_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_an_integer_an_object to Testobjects'
test_is_a_float_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_float_an_object to make it a method of the Testobjects class
55 assert issubclass(int, object) 56 self.assertIsSubclass(int, object) 57 58 def test_is_a_float_an_object(): 59 assert isinstance(float, object) 60 assert issubclass(float, object) 61 62 63def test_is_a_string_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_float_an_object() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_is_a_float_an_object
55 assert issubclass(int, object)
56 self.assertIsSubclass(int, object)
57
58 # def test_is_a_float_an_object():
59 def test_is_a_float_an_object(self):
60 assert isinstance(float, object)
61 assert issubclass(float, object)
62
63
64def test_is_a_string_an_object():
green.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
58 # def test_is_a_float_an_object(): 59 def test_is_a_float_an_object(self): 60 assert isinstance(float, object) 61 self.assertNotIsInstance(float, object) 62 63 assert issubclass(float, object) 64 self.assertNotIsSubclass(float, object) 65 66 67def test_is_a_string_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'float'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
58 # def test_is_a_float_an_object(): 59 def test_is_a_float_an_object(self): 60 assert isinstance(float, object) 61 # self.assertNotIsInstance(float, object) 62 self.assertIsInstance(float, object) 63 64 assert issubclass(float, object) 65 self.assertNotIsSubclass(float, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'float'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
64 assert issubclass(float, object) 65 # self.assertNotIsSubclass(float, object) 66 self.assertIsSubclass(float, object) 67 68 69def test_is_a_string_an_object():the test passes.
I remove the commented lines from test_is_a_float_an_object
55 assert issubclass(int, object) 56 self.assertIsSubclass(int, object) 57 58 def test_is_a_float_an_object(self): 59 assert isinstance(float, object) 60 self.assertIsInstance(float, object) 61 62 assert issubclass(float, object) 63 self.assertIsSubclass(float, object) 64 65 66def test_is_a_string_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_float_an_object to Testobjects'
test_is_a_string_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_string_an_object to make it a method of the Testobjects class
62 assert issubclass(float, object) 63 self.assertIsSubclass(float, object) 64 65 def test_is_a_string_an_object(): 66 assert isinstance(str, object) 67 assert issubclass(str, object) 68 69 70def test_is_a_tuple_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_string_an_object() takes 0 positional arguments but 1 was given
GREEN: make it pass
I add self to the parentheses of test_is_a_string_an_object
62 assert issubclass(float, object)
63 self.assertIsSubclass(float, object)
64
65 # def test_is_a_string_an_object():
66 def test_is_a_string_an_object(self):
67 assert isinstance(str, object)
68 assert issubclass(str, object)
green again.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
65 # def test_is_a_string_an_object(): 66 def test_is_a_string_an_object(self): 67 assert isinstance(str, object) 68 self.assertNotIsInstance(str, object) 69 70 assert issubclass(str, object) 71 self.assertNotIsSubclass(str, object) 72 73 74def test_is_a_tuple_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'str'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
65 # def test_is_a_string_an_object(): 66 def test_is_a_string_an_object(self): 67 assert isinstance(str, object) 68 # self.assertNotIsInstance(str, object) 69 self.assertIsInstance(str, object) 70 71 assert issubclass(str, object) 72 self.assertNotIsSubclass(str, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'str'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
71 assert issubclass(str, object) 72 # self.assertNotIsSubclass(str, object) 73 self.assertIsSubclass(str, object) 74 75 76def test_is_a_tuple_an_object():the test passes.
I remove the commented lines from test_is_a_string_an_object
62 assert issubclass(float, object) 63 self.assertIsSubclass(float, object) 64 65 def test_is_a_string_an_object(self): 66 assert isinstance(str, object) 67 self.assertIsInstance(str, object) 68 69 assert issubclass(str, object) 70 self.assertIsSubclass(str, object) 71 72 73def test_is_a_tuple_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_string_an_object to Testobjects'
test_is_a_tuple_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_tuple_an_object to make it a method of the Testobjects class
69 assert issubclass(str, object) 70 self.assertIsSubclass(str, object) 71 72 def test_is_a_tuple_an_object(): 73 assert isinstance(tuple, object) 74 assert issubclass(tuple, object) 75 76 77def test_is_a_list_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_tuple_an_object() takes 0 positional arguments but 1 was givenbecause …
GREEN: make it pass
I add self to the parentheses of test_is_a_tuple_an_object
69 assert issubclass(str, object)
70 self.assertIsSubclass(str, object)
71
72 # def test_is_a_tuple_an_object():
73 def test_is_a_tuple_an_object(self):
74 assert isinstance(tuple, object)
75 assert issubclass(tuple, object)
the test is green again.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
72 # def test_is_a_tuple_an_object(): 73 def test_is_a_tuple_an_object(self): 74 assert isinstance(tuple, object) 75 self.assertNotIsInstance(tuple, object) 76 77 assert issubclass(tuple, object) 78 self.assertNotIsSubclass(tuple, object) 79 80 81def test_is_a_list_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tuple'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
72 # def test_is_a_tuple_an_object(): 73 def test_is_a_tuple_an_object(self): 74 assert isinstance(tuple, object) 75 # self.assertNotIsInstance(tuple, object) 76 self.assertIsInstance(tuple, object) 77 78 assert issubclass(tuple, object) 79 self.assertNotIsSubclass(tuple, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'tuple'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
78 assert issubclass(tuple, object) 79 # self.assertNotIsSubclass(tuple, object) 80 self.assertIsSubclass(tuple, object) 81 82 83def test_is_a_list_an_object():the test passes.
I remove the commented lines from test_is_a_tuple_an_object
69 assert issubclass(str, object) 70 self.assertIsSubclass(str, object) 71 72 def test_is_a_tuple_an_object(self): 73 assert isinstance(tuple, object) 74 self.assertIsInstance(tuple, object) 75 76 assert issubclass(tuple, object) 77 self.assertIsSubclass(tuple, object) 78 79 80def test_is_a_list_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_tuple_an_object to Testobjects'
test_is_a_list_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_list_an_object to make it a method of the Testobjects class
76 assert issubclass(tuple, object) 77 self.assertIsSubclass(tuple, object) 78 79 def test_is_a_list_an_object(): 80 assert isinstance(list, object) 81 assert issubclass(list, object) 82 83 84def test_is_a_set_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_list_an_object() 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_is_a_list_an_object
76 assert issubclass(tuple, object)
77 self.assertIsSubclass(tuple, object)
78
79 # def test_is_a_list_an_object():
80 def test_is_a_list_an_object(self):
81 assert isinstance(list, object)
82 assert issubclass(list, object)
green again.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
79 # def test_is_a_list_an_object(): 80 def test_is_a_list_an_object(self): 81 assert isinstance(list, object) 82 self.assertNotIsInstance(list, object) 83 84 assert issubclass(list, object) 85 self.assertNotIsSubclass(list, object) 86 87 88def test_is_a_set_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'list'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
79 # def test_is_a_list_an_object(): 80 def test_is_a_list_an_object(self): 81 assert isinstance(list, object) 82 # self.assertNotIsInstance(list, object) 83 self.assertIsInstance(list, object) 84 85 assert issubclass(list, object) 86 self.assertNotIsSubclass(list, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'list'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
85 assert issubclass(list, object) 86 # self.assertNotIsSubclass(list, object) 87 self.assertIsSubclass(list, object) 88 89 90def test_is_a_set_an_object():the test passes.
I remove the commented lines from test_is_a_list_an_object
76 assert issubclass(tuple, object) 77 self.assertIsSubclass(tuple, object) 78 79 def test_is_a_list_an_object(self): 80 assert isinstance(list, object) 81 self.assertIsInstance(list, object) 82 83 assert issubclass(list, object) 84 self.assertIsSubclass(list, object) 85 86 87def test_is_a_set_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_list_an_object to Testobjects'
test_is_a_set_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_set_an_object to make it a method of the Testobjects class
83 assert issubclass(list, object) 84 self.assertIsSubclass(list, object) 85 86 def test_is_a_set_an_object(): 87 assert isinstance(set, object) 88 assert issubclass(set, object) 89 90 91def test_is_a_dictionary_an_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_set_an_object() takes 0 positional arguments but 1 was givenbecause a method of an instance takes the instance of the class (
self) …
GREEN: make it pass
I add self to the parentheses of test_is_a_set_an_object
83 assert issubclass(list, object)
84 self.assertIsSubclass(list, object)
85
86 # def test_is_a_set_an_object():
87 def test_is_a_set_an_object(self):
88 assert isinstance(set, object)
89 assert issubclass(set, object)
green.
REFACTOR: make it better
I add calls to the assertNotIsInstance and assertNotIsSubclass methods
86 # def test_is_a_set_an_object(): 87 def test_is_a_set_an_object(self): 88 assert isinstance(set, object) 89 self.assertNotIsInstance(set, object) 90 91 assert issubclass(set, object) 92 self.assertNotIsSubclass(set, object) 93 94 95def test_is_a_dictionary_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'set'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
86 # def test_is_a_set_an_object(): 87 def test_is_a_set_an_object(self): 88 assert isinstance(set, object) 89 # self.assertNotIsInstance(set, object) 90 self.assertIsInstance(set, object) 91 92 assert issubclass(set, object) 93 self.assertNotIsSubclass(set, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'set'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
92 assert issubclass(set, object) 93 # self.assertNotIsSubclass(set, object) 94 self.assertIsSubclass(set, object) 95 96 97def test_is_a_dictionary_an_object():the test passes.
I remove the commented lines from test_is_a_set_an_object
83 assert issubclass(list, object) 84 self.assertIsSubclass(list, object) 85 86 def test_is_a_set_an_object(self): 87 assert isinstance(set, object) 88 self.assertIsInstance(set, object) 89 90 assert issubclass(set, object) 91 self.assertIsSubclass(set, object) 92 93 94def test_is_a_dictionary_an_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_set_an_object to Testobjects'
test_is_a_dictionary_an_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_is_a_dictionary_an_object to make it a method of the Testobjects class
90 assert issubclass(set, object) 91 self.assertIsSubclass(set, object) 92 93 def test_is_a_dictionary_an_object(): 94 assert isinstance(dict, object) 95 assert issubclass(dict, object) 96 97 98def test_dir_object():the terminal is my friend, and shows TypeError
TypeError: Testobjects.test_is_a_dictionary_an_object() 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_is_a_dictionary_an_object
90 assert issubclass(set, object)
91 self.assertIsSubclass(set, object)
92
93 # def test_is_a_dictionary_an_object():
94 def test_is_a_dictionary_an_object(self):
95 assert isinstance(dict, object)
96 assert issubclass(dict, object)
green again.
REFACTOR: make it better
I add a call to the assertNotIsInstance and assertNotIsSubclass methods
93 # def test_is_a_dictionary_an_object(): 94 def test_is_a_dictionary_an_object(self): 95 assert isinstance(dict, object) 96 self.assertNotIsInstance(dict, object) 97 98 assert issubclass(dict, object) 99 self.assertNotIsSubclass(dict, object) 100 101 102def test_dir_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'dict'> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
93 # def test_is_a_dictionary_an_object(): 94 def test_is_a_dictionary_an_object(self): 95 assert isinstance(dict, object) 96 # self.assertNotIsInstance(dict, object) 97 self.assertIsInstance(dict, object) 98 99 assert issubclass(dict, object) 100 self.assertNotIsSubclass(dict, object)the terminal is my friend, and shows AssertionError
AssertionError: <class 'dict'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
99 assert issubclass(dict, object) 100 # self.assertNotIsSubclass(dict, object) 101 self.assertIsSubclass(dict, object) 102 103 104def test_dir_object():the test passes.
I remove the commented lines from test_is_a_dictionary_an_object
90 assert issubclass(set, object) 91 self.assertIsSubclass(set, object) 92 93 def test_is_a_dictionary_an_object(self): 94 assert isinstance(dict, object) 95 self.assertIsInstance(dict, object) 96 97 assert issubclass(dict, object) 98 self.assertIsSubclass(dict, object) 99 100 101def test_dir_object():I add a git commit message in the other terminal
git commit -am \ 'move test_is_a_dictionary_an_object to Testobjects'
test_dir_object with unittest
RED: make it fail
I go back to the terminal where the tests are running
I move test_dir_object to make it a method of the Testobjects class
97 assert issubclass(dict, object) 98 self.assertIsSubclass(dict, object) 99 100 def test_dir_object(): 101 reality = dir(object) 102 my_expectation = [ 103 '__class__', '__delattr__', '__dir__', 104 '__doc__', '__eq__', '__format__', 105 '__ge__', '__getattribute__', 106 '__getstate__', '__gt__', '__hash__', 107 '__init__', '__init_subclass__', '__le__', 108 '__lt__', '__ne__', '__new__', '__reduce__', 109 '__reduce_ex__', '__repr__', '__setattr__', 110 '__sizeof__', '__str__', '__subclasshook__' 111 ] 112 assert reality == my_expectation 113 114 115# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: Testobjects.test_dir_object() 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_dir_object
97 assert issubclass(dict, object)
98 self.assertIsSubclass(dict, object)
99
100 # def test_dir_object():
101 def test_dir_object(self):
102 reality = dir(object)
103 my_expectation = [
the test is green again.
REFACTOR: make it better
I add a call to the assertNotEqual method
100 # def test_dir_object(): 101 def test_dir_object(self): 102 reality = dir(object) 103 my_expectation = [ 104 '__class__', '__delattr__', '__dir__', 105 '__doc__', '__eq__', '__format__', 106 '__ge__', '__getattribute__', 107 '__getstate__', '__gt__', '__hash__', 108 '__init__', '__init_subclass__', '__le__', 109 '__lt__', '__ne__', '__new__', '__reduce__', 110 '__reduce_ex__', '__repr__', '__setattr__', 111 '__sizeof__', '__str__', '__subclasshook__' 112 ] 113 assert reality == my_expectation 114 self.assertNotEqual(reality, my_expectation) 115 116 117# Exceptions seenthe terminal is my friend, and shows AssertionError.
I change assertNotEqual to assertEqual
113 assert reality == my_expectation 114 # self.assertNotEqual(reality, my_expectation) 115 self.assertEqual(reality, my_expectation)the test passes.
I remove the commented lines from test_dir_object
97 assert issubclass(dict, object) 98 self.assertIsSubclass(dict, object) 99 100 def test_dir_object(self): 101 reality = dir(object) 102 my_expectation = [111 ] 112 assert reality == my_expectation 113 self.assertEqual(reality, my_expectation) 114 115 116# Exceptions seen 117# AssertionError 118# NameError 119# TypeError 120# AttributeErrorI add a git commit message in the other terminal
git commit -am \ 'move test_dir_object to Testobjects'
review
I can use the unittest library to write tests with the methods of the unittest.TestCase class or I can write them with bare assert statements.
close the project
I close
test_objects.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
objectscd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
code from the chapter
what is next?
Would you like to test the functions project with the unittest library?
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.