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
11
12
13class TestClasses(unittest.TestCase):
14
15 def test_making_a_class_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_a_class_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 def test_making_a_class_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)
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 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 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)
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 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 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)
99
100 def test_dir_object(self):
101 reality = dir(object)
102 my_expectation = [
103 '__class__', '__delattr__', '__dir__',
104 '__doc__', '__eq__', '__format__', '__ge__',
105 '__getattribute__', '__getstate__', '__gt__',
106 '__hash__', '__init__', '__init_subclass__',
107 '__le__', '__lt__', '__ne__', '__new__',
108 '__reduce__', '__reduce_ex__', '__repr__',
109 '__setattr__', '__sizeof__', '__str__',
110 '__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 classesthe terminal shows I am in the
classesfolder.../pumping_python/classesI open
test_classes.pyI use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
test_classes.py ............. [100%] =================== 13 passed in J.KLs ===================
add TestClasses class
RED: make it fail
I add a class named
Classestotest_classes.py1class WPass: pass 2 3 4class WParentheses(): pass 5 6 7class WObject(object): pass 8 9 10class Classes(object): 11 12 def test_failure(self): 13 self.assertEqual(True, False) 14 15 16def test_making_a_class_w_pass():the test is still green.
I change the name of the class to
TestClasses7class WObject(object): pass 8 9 10# class Class(object): 11class TestClasses(object): 12 13 def test_failure(self): 14 self.assertEqual(True, False)the terminal is my friend, and shows AttributeError
AttributeError: 'TestClasses' 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
TestClasses7class WObject(object): pass 8 9 10# class Class(object): 11# class TestClasses(object): 12class TestClasses(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 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 Class(object): 14# class TestClasses(object): 15class TestClasses(unittest.TestCase): 16 17 def test_failure(self): 18 # self.assertEqual(True, False) 19 self.assertEqual(True, True) 20 21 22def test_making_a_class_w_pass():the test passes.
REFACTOR: make it better
I remove the commented lines
10class WObject(object): pass 11 12 13class TestClasses(unittest.TestCase): 14 15 def test_failure(self): 16 self.assertEqual(True, True) 17 18 19def test_making_a_class_w_pass():I open a new terminal then make sure I am in the
classesfoldercd classesI add a git commit message in the new terminal
git commit -am \ 'add TestClasses class'
test_making_a_class_w_pass with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_making_a_class_w_pass to make it a method of the TestClasses class and replace
test_failure13class TestClasses(unittest.TestCase): 14 15 def test_making_a_class_w_pass(): 16 assert isinstance(WPass(), object) 17 assert issubclass(WPass, object) 18 19 20def test_making_a_class_w_parentheses():the terminal is my friend, and shows TypeError
TypeError: TestClasses.test_making_a_class_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_a_class_w_pass
15 # def test_making_a_class_w_pass():
16 def test_making_a_class_w_pass(self):
the test is green again.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
15 # def test_making_a_class_w_pass(): 16 def test_making_a_class_w_pass(self): 17 assert isinstance(WPass(), object) 18 self.assertNotIsInstance(WPass(), object) 19 20 assert issubclass(WPass, object) 21 22 23def test_making_a_class_w_parentheses():the terminal is my friend, and shows AssertionError
AssertionError: <tests.test_classes.WPass object at 0xffff01234a567> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
15 # def test_making_a_class_w_pass(): 16 def test_making_a_class_w_pass(self): 17 assert isinstance(WPass(), object) 18 # self.assertNotIsInstance(WPass(), object) 19 self.assertIsInstance(WPass(), object) 20 21 assert issubclass(WPass, object) 22 23 24def test_making_a_class_w_parentheses():the test passes.
I add a call to the assertNotIsSubclass method
15 # def test_making_a_class_w_pass(): 16 def test_making_a_class_w_pass(self): 17 assert isinstance(WPass(), object) 18 # self.assertNotIsInstance(WPass(), object) 19 self.assertIsInstance(WPass(), object) 20 21 assert issubclass(WPass, object) 22 self.assertNotIsSubclass(WPass, object) 23 24 25def test_making_a_class_w_parentheses():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tests.test_classes.WPass'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
15 # def test_making_a_class_w_pass(): 16 def test_making_a_class_w_pass(self): 17 assert isinstance(WPass(), object) 18 # self.assertNotIsInstance(WPass(), object) 19 self.assertIsInstance(WPass(), object) 20 21 assert issubclass(WPass, object) 22 # self.assertNotIsSubclass(WPass, object) 23 self.assertIsSubclass(WPass, object) 24 25 26def test_making_a_class_w_parentheses():the test passes.
I remove the commented lines from test_making_a_class_w_pass
13class TestClasses(unittest.TestCase): 14 15 def test_making_a_class_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_a_class_w_parentheses():I add a git commit message in the other terminal
git commit -am \ 'move test_making_a_class_w_pass to TestClasses'
test_making_a_class_w_parentheses with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_making_a_class_w_parentheses to make it a method of the TestClasses class
20 self.assertIsSubclass(WPass, object) 21 22 def test_making_a_class_w_parentheses(): 23 assert isinstance(WParentheses(), object) 24 assert issubclass(WParentheses, object) 25 26 27def test_making_a_class_w_object():the terminal is my friend, and shows TypeError
TypeError: TestClasses.test_making_a_class_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_a_class_w_parentheses
22 # def test_making_a_class_w_parentheses():
23 def test_making_a_class_w_parentheses(self):
green again.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
22 # def test_making_a_class_w_parentheses(): 23 def test_making_a_class_w_parentheses(self): 24 assert isinstance(WParentheses(), object) 25 self.assertNotIsInstance( 26 WParentheses(), object 27 ) 28 29 assert issubclass(WParentheses, object) 30 31 32def test_making_a_class_w_object():the terminal is my friend, and shows AssertionError
AssertionError: <tests.test_classes.WParentheses object at 0xffff45ab67cd8> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
22 # def test_making_a_class_w_parentheses(): 23 def test_making_a_class_w_parentheses(self): 24 assert isinstance(WParentheses(), object) 25 # self.assertNotIsInstance( 26 self.assertIsInstance( 27 WParentheses(), object 28 ) 29 30 assert issubclass(WParentheses, object) 31 32 33def test_making_a_class_w_object():the test passes.
I add a call to the assertNotIsSubclass method
22 # def test_making_a_class_w_parentheses(): 23 def test_making_a_class_w_parentheses(self): 24 assert isinstance(WParentheses(), object) 25 # self.assertNotIsInstance( 26 self.assertIsInstance( 27 WParentheses(), object 28 ) 29 30 assert issubclass(WParentheses, object) 31 self.assertNotIsSubclass( 32 WParentheses, object 33 ) 34 35 36def test_making_a_class_w_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tests.test_classes.WParentheses'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
22 # def test_making_a_class_w_parentheses(): 23 def test_making_a_class_w_parentheses(self): 24 assert isinstance(WParentheses(), object) 25 # self.assertNotIsInstance( 26 self.assertIsInstance( 27 WParentheses(), object 28 ) 29 30 assert issubclass(WParentheses, object) 31 # self.assertNotIsSubclass( 32 self.assertIsSubclass( 33 WParentheses, object 34 ) 35 36 37def test_making_a_class_w_object():the test passes.
I remove the commented lines from test_making_a_class_w_parentheses
20 self.assertIsSubclass(WPass, object) 21 22 def test_making_a_class_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_a_class_w_object():I add a git commit message in the other terminal
git commit -am \ 'move test_making_a_class_w_parentheses to TestClasses'
test_making_a_class_w_object with unittest
RED: make it fail
I go back to the terminal where the tests are running.
I move test_making_a_class_w_object to make it a method of the TestClasses class
27 self.assertIsSubclass(WParentheses, object) 28 29 def test_making_a_class_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: TestClasses.test_making_a_class_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_a_class_w_object
29 # def test_making_a_class_w_object():
30 def test_making_a_class_w_object(self):
green.
REFACTOR: make it better
I add a call to the assertNotIsInstance method
29 # def test_making_a_class_w_object(): 30 def test_making_a_class_w_object(self): 31 assert isinstance(WObject(), object) 32 self.assertNotIsInstance(WObject(), object) 33 34 assert issubclass(WObject, object) 35 36 37def test_is_none_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <tests.test_classes.WObject object at 0xffff345a6b789> is an instance of <class 'object'>I change assertNotIsInstance to assertIsInstance
29 # def test_making_a_class_w_object(): 30 def test_making_a_class_w_object(self): 31 assert isinstance(WObject(), object) 32 # self.assertNotIsInstance(WObject(), object) 33 self.assertIsInstance(WObject(), object) 34 35 assert issubclass(WObject, object) 36 37 38def test_is_none_an_object():the test passes.
I add a call to the assertNotIsSubclass method
29 # def test_making_a_class_w_object(): 30 def test_making_a_class_w_object(self): 31 assert isinstance(WObject(), object) 32 # self.assertNotIsInstance(WObject(), object) 33 self.assertIsInstance(WObject(), object) 34 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_classes.WObject'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
29 # def test_making_a_class_w_object(): 30 def test_making_a_class_w_object(self): 31 assert isinstance(WObject(), object) 32 # self.assertNotIsInstance(WObject(), object) 33 self.assertIsInstance(WObject(), object) 34 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_a_class_w_object
27 self.assertIsSubclass(WParentheses, object) 28 29 def test_making_a_class_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_a_class_w_object to TestClasses'
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 TestClasses class
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: TestClasses.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
36 # def test_is_none_an_object():
37 def test_is_none_an_object(self):
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) 43 44 45def test_is_a_boolean_an_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) 44 45 46def test_is_a_boolean_an_object():the test passes.
I add a call to the assertIsSubclass 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 self.assertIsInstance(None, object) 41 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 classI comment out the assertion
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
44 # def test_is_a_boolean_an_object():
45 def test_is_a_boolean_an_object(self):
the test is green again.
REFACTOR: make it better
I add a call 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) 51 self.assertNotIsSubclass(bool, object) 52 53 54def test_is_an_integer_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'bool'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
51 # def test_is_an_integer_an_object():
52 def test_is_an_integer_an_object(self):
green again.
REFACTOR: make it better
I add a call 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) 59 60 61def test_is_a_float_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'int'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
58 # def test_is_a_float_an_object():
59 def test_is_a_float_an_object(self):
green.
REFACTOR: make it better
I add a call 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) 66 67 68def test_is_a_string_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'float'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
65 # def test_is_a_string_an_object():
66 def test_is_a_string_an_object(self):
green again.
REFACTOR: make it better
I add a call 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) 73 74 75def test_is_a_tuple_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'str'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
72 # def test_is_a_tuple_an_object():
73 def test_is_a_tuple_an_object(self):
the test is green again.
REFACTOR: make it better
I add a call 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) 80 81 82def test_is_a_list_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'tuple'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
79 # def test_is_a_list_an_object():
80 def test_is_a_list_an_object(self):
green again.
REFACTOR: make it better
I add a call 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) 87 88 89def test_is_a_set_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'list'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
86 # def test_is_a_set_an_object():
87 def test_is_a_set_an_object(self):
green.
REFACTOR: make it better
I add a call 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) 94 95 96def test_is_a_dictionary_an_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'set'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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: TestClasses.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
93 # def test_is_a_dictionary_an_object():
94 def test_is_a_dictionary_an_object(self):
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) 101 102 103def test_dir_object():the terminal is my friend, and shows AssertionError
AssertionError: <class 'dict'> is a subclass of <class 'object'>I change assertNotIsSubclass to assertIsSubclass
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) 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
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 TestClasses'
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 TestClasses class
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__', '__ge__', 105 '__getattribute__', '__getstate__', '__gt__', 106 '__hash__', '__init__', '__init_subclass__', 107 '__le__', '__lt__', '__ne__', '__new__', 108 '__reduce__', '__reduce_ex__', '__repr__', 109 '__setattr__', '__sizeof__', '__str__', 110 '__subclasshook__' 111 ] 112 assert reality == my_expectation 113 114 115# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: TestClasses.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
100 # def test_dir_object():
101 def test_dir_object(self):
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__', '__ge__', 106 '__getattribute__', '__getstate__', '__gt__', 107 '__hash__', '__init__', '__init_subclass__', 108 '__le__', '__lt__', '__ne__', '__new__', 109 '__reduce__', '__reduce_ex__', '__repr__', 110 '__setattr__', '__sizeof__', '__str__', 111 '__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
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 TestClasses'
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_classes.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
classescd ..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.