another way to write tests
I used unittest in how to make a Python Test Driven Development environment manually to run tests manually before I learned to run them automatically with pytest-watcher.
The unittest library is part of The Python Standard Library and can also be used to write tests, it is like a toolbox with different tools I can use to test code.
preview
I have these tests by the end of the chapter
questions about unittest
start the project
I name this project
unittestI open a terminal
I change directory to the
unittestfolder in thepumping_pythonfoldercd unittestthe terminal shows
cd: no such file or directory: unittestI use uv to make a directory for the project and initialize it
uv init unittestthe terminal shows
Initialized project `unittest` at `.../pumping_python/unittest`I change directory to
unittestcd unittestthe terminal shows I am in the
unittestfolder.../pumping_python/unittestI make a directory for the tests
mkdir testsI make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNew-Item tests/__init__.pyI make an empty file for the tests in the
testsfoldertouch tests/test_unittest.pyNew-Item tests/test_unittest.pyI open
test_unittest.pyfrom thetestsfolderI add the first failing test to
test_unittest.py1def test_failure(): 2 assert False is TrueI go back to the terminal
I use echo to add pytest to
requirements.txtecho "pytest" > requirements.txtI add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line.
I use Out-File to add pytest to
requirements.txt"pytest" | Out-File requirements.txt -Encoding UTF8I add pytest-watcher to the requirements file
"pytest-watcher" >> requirements.txt
I use uv to install pytest-watcher with the requirements file
uv add --requirement requirements.txtI add the new files and folder to git for tracking
git add .I add a git commit message
git commit -am 'setup project'I use
pytest-watcherto run the testsuv run pytest-watcher . --now --patterns "**/*.py"the terminal is my friend, and shows AssertionError
======================== FAILURES ======================== ______________________ test_failure ______________________ def test_failure(): > assert False is True E assert False is True test_unittest.py:2: AssertionError ================ short test summary info ================= FAILED test_unittest.py::test_failure - assert False is True =================== 1 failed in X.YZs ====================if the terminal does not show the same error, then check if
your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyyou ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
and try
uv run pytest-watcher . --now --patterns "**/*.py"againI add AssertionError to the list of Exceptions seen, in
test_unittest.py1def test_failure(): 2 assert False is True 3 4 5# Exceptions seen 6# AssertionErrorI change True to False in the assertion
1def test_failure(): 2 # assert False is True 3 assert False is False 4 5 6# Exceptions seen 7# AssertionErrorthe test passes.
test_dir_unittest
I want to see what comes with the unittest module.
RED: make it fail
I change
test_failureto test_dir_unittest intest_unittest.py1def test_dir_unittest(): 2 reality = dir(unittest) 3 my_expectation = [] 4 assert reality == my_expectation 5 6 7# Exceptions seenthe terminal is my friend, and shows NameError
NameError: name 'unittest' is not defined. Did you forget to import 'unittest'?I add NameError to the list of Exceptions seen
7# Exceptions seen 8# AssertionError 9# NameError
GREEN: make it pass
I add an import statement for unittest at the top of the file
1import unittest 2 3 4def test_dir_unittest():the terminal is my friend, and shows AssertionError
E AssertionError: assert ['BaseTestSui...tLoader', ...] == [] E E Left contains 44 more items, first extra item: 'BaseTestSuite' E Use -v to get more diffI click in the terminal where the tests are running then press v on the keyboard for pytest-watcher to show me more of the difference between
realityandmy_expectationand it shows AssertionErrorE ...Full output truncated (44 lines hidden), use '-vv' to showI press w on the keyboard in the terminal where the tests are running, to show the menu for pytest-watcher and it shows
[pytest-watcher] Current runner args: [-v] Controls: > Enter : Invoke test runner > r : reset all runner args > c : change runner args > f : run only failed tests (--lf) > p : drop to pdb on fail (--pdb) > v : increase verbosity (-v) > e : Erase terminal screen > q : quit pytest-watcherI press c on the keyboard to change runner args, and the terminal shows
[pytest-watcher] Current runner args: [] Controls: > Enter : Invoke test runner > r : reset all runner args > c : change runner args > f : run only failed tests (--lf) > p : drop to pdb on fail (--pdb) > v : increase verbosity (-v) > e : Erase terminal screen > q : quit pytest-watcher Enter new runner args: -vvI type -+v+v then press enter to show the full difference, and the terminal shows AssertionError with the full list.
I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as
my_expectationCaution
Your list of attributes and methods may be different depending on your Python version
4def test_dir_unittest(): 5 reality = dir(unittest) 6 my_expectation = [ 7 'BaseTestSuite', 'FunctionTestCase', 8 'IsolatedAsyncioTestCase', 'SkipTest', 9 'TestCase', 'TestLoader', 'TestProgram', 10 'TestResult', 'TestSuite', 'TextTestResult', 11 'TextTestRunner', '__all__', '__builtins__', 12 '__cached__', '__dir__', '__doc__', 13 '__file__', '__getattr__', '__loader__', 14 '__name__', '__package__', '__path__', 15 '__spec__', '__unittest', 'addModuleCleanup', 16 'case', 'defaultTestLoader', 'doModuleCleanups', 17 'enterModuleContext', 'expectedFailure', 18 'installHandler', 'loader', 'main', 19 'registerResult', 'removeHandler', 'removeResult', 20 'result', 'runner', 'signals', 'skip', 'skipIf', 21 'skipUnless', 'suite', 'util' 22 ] 23 assert reality == my_expectation 24 25 26# Exceptions seenthe test passes because when
import unittestruns, Python brings in an object for the unittest module from The Python Standard Library so I can use it intest_unittest.pyasunittest.This means that there is a file or folder on the computer named
unittestthat got added when I installed Python.I open a new terminal then make sure I am in the
unittestfoldercd unittestI add a git commit message in the new terminal
git commit -am \ 'add test_dir_unittest'
test_dir_unittest_testcase
One of the names in the list of attributes and methods of unittest is TestCase, this is the class (toolbox) that contains the things I will use to write tests for code.
RED: make it fail
I add a new test for the attributes and methods of TestCase
23 assert reality == my_expectation
24
25
26def test_dir_unittest_testcase():
27 reality = dir(unittest.TestCase)
28 my_expectation = []
29 assert reality == my_expectation
30
31
32# Exceptions seen
the terminal is my friend, and shows AssertionError
E AssertionError: assert
['__call__', ...__doc__', ...] == []
E
E Left contains 108 more items,
first extra item: '__call__'
E Use -v to get more diff
GREEN: make it pass
I click in the terminal where the tests are running then press v on the keyboard for pytest-watcher to show me more of the difference between
realityandmy_expectationand it shows AssertionErrorE ...Full output truncated (108 lines hidden), use '-vv' to showI press w on the keyboard in the terminal where the tests are running, to show the menu for pytest-watcher and it shows
[pytest-watcher] Current runner args: [-v] Controls: > Enter : Invoke test runner > r : reset all runner args > c : change runner args > f : run only failed tests (--lf) > p : drop to pdb on fail (--pdb) > v : increase verbosity (-v) > e : Erase terminal screen > q : quit pytest-watcherI press c on the keyboard to change runner args, and the terminal shows
[pytest-watcher] Current runner args: [] Controls: > Enter : Invoke test runner > r : reset all runner args > c : change runner args > f : run only failed tests (--lf) > p : drop to pdb on fail (--pdb) > v : increase verbosity (-v) > e : Erase terminal screen > q : quit pytest-watcher Enter new runner args: -vvI type
-vvthen press enter to show the full difference, and the terminal shows AssertionError with the entire list.I copy (ctrl/command+c) the values from the terminal and paste (ctrl/command+v) them as
my_expectation4def test_dir_unittest_testcase(): 5 reality = dir(unittest.TestCase) 6 my_expectation = [ 7 '__call__', '__class__', '__delattr__', 8 '__dict__', '__dir__', '__doc__', '__eq__', 9 '__firstlineno__', '__format__', '__ge__', 10 '__getattribute__', '__getstate__', '__gt__', 11 '__hash__', '__init__', '__init_subclass__', 12 '__le__', '__lt__', '__module__', '__ne__', 13 '__new__', '__reduce__', '__reduce_ex__', 14 '__repr__', '__setattr__', '__sizeof__', 15 '__static_attributes__', '__str__', 16 '__subclasshook__', '__weakref__', 17 '_addDuration', '_addExpectedFailure', 18 '_addUnexpectedSuccess', '_assertNotWarns', 19 '_baseAssertEqual', '_callCleanup', 20 '_callSetUp', '_callTearDown', '_callTestMethod', 21 '_diffThreshold', '_formatMessage', 22 '_getAssertEqualityFunc', '_tail_type_check', 23 '_truncateMessage', 'addClassCleanup', 24 'addCleanup', 'addTypeEqualityFunc', 25 'assertAlmostEqual', 'assertCountEqual', 26 'assertDictEqual', 'assertEndsWith', 27 'assertEqual', 'assertFalse', 'assertGreater', 28 'assertGreaterEqual', 'assertHasAttr', 29 'assertIn', 'assertIs', 'assertIsInstance', 30 'assertIsNone', 'assertIsNot', 31 'assertIsNotNone', 'assertIsSubclass', 32 'assertLess', 'assertLessEqual', 33 'assertListEqual', 'assertLogs', 34 'assertMultiLineEqual', 'assertNoLogs', 35 'assertNotAlmostEqual', 'assertNotEndsWith', 36 'assertNotEqual', 'assertNotHasAttr', 37 'assertNotIn', 'assertNotIsInstance', 38 'assertNotIsSubclass', 'assertNotRegex', 39 'assertNotStartsWith', 'assertRaises', 40 'assertRaisesRegex', 'assertRegex', 41 'assertSequenceEqual', 'assertSetEqual', 42 'assertStartsWith', 'assertTrue', 43 'assertTupleEqual', 'assertWarns', 44 'assertWarnsRegex', 'countTestCases', 45 'debug', 'defaultTestResult', 46 'doClassCleanups', 'doCleanups', 47 'enterClassContext', 'enterContext', 'fail', 48 'failureException', 'id', 'longMessage', 49 'maxDiff', 'run', 'setUp', 'setUpClass', 50 'shortDescription', 'skipTest', 'subTest', 51 'tearDown', 'tearDownClass' 52 ] 53 assert reality == my_expectation 54 55 56# Exceptions seenThe test passes.
The __init__ method is in the list of attributes and methods because all objects inherit from ‘object’.
REFACTOR: make it better
I see names that begin with
assertand look like some of the assertions I have written so far.assert X is not Yassert X is Yassert X != Yassert X == Yassert not isinstance(X, Y)assert isinstance(X, Y)assert not issubclass(X, Y)assert issubclass(X, Y)I type these names below the test as a TODO list to test what I can do with them
75assert reality == my_expectation 76 77 78'assertIsNot' 79'assertIs' 80'assertNotEqual' 81'assertEqual' 82'assertNotIsInstance' 83'assertIsInstance' 84'assertNotIsSubclass' 85'assertIsSubclass' 86 87 88# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_dir_unittest_testcase'
test_assertIsNot
RED: make it fail
I go back to the terminal where the tests are running
I add a test for assertIsNot
75 assert reality == my_expectation 76 77 78def test_assertIsNot(): 79 unittest.TestCase.assertIsNot() 80 81 82'assertIsNot'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIsNot() missing 3 required positional arguments: 'self', 'expr1', and 'expr2'because a method of an instance takes the instance of the class (
self) it belongs to as the first argument.The definition of the assertIsNot method of the TestCase class of the unittest library (
unittest.TestCase.assertIsNot) has two required positional arguments. I imagine Python follows this path when unittest.TestCase.assertIsNot is calledunittest.TestCase.assertIsNot └── unittest └── class TestCase: └── def assertIsNot(self, expr1, expr2): └── return something
I add TypeError to the list of Exceptions seen
89'assertIsSubclass' 90 91 92# Exceptions seen 93# AssertionError 94# NameError 95# TypeError
GREEN: make it pass
I change the call to use an instance instead of the unittest.TestCase class
78def test_assertIsNot(): 79 # unittest.TestCase.assertIsNot() 80 unittest.TestCase().assertIsNot() 81 82 83'assertIsNot'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIsNot() missing 2 required positional arguments: 'expr1' and 'expr2'I no longer need to provide
selfbecause it is the instance of the class (unittest.TestCase()).I add two things to the call to unittest.TestCase.assertIsNot
78def test_assertIsNot(): 79 # unittest.TestCase.assertIsNot() 80 # unittest.TestCase().assertIsNot() 81 unittest.TestCase().assertIsNot(None, None) 82 83 84'assertIsNot'the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: Nonebecause None is None.
I change the assertion to make it True
78def test_assertIsNot(): 79 # unittest.TestCase.assertIsNot() 80 # unittest.TestCase().assertIsNot() 81 # unittest.TestCase().assertIsNot(None, None) 82 unittest.TestCase().assertIsNot(None, False) 83 84 85'assertIsNot'the test passes.
REFACTOR: make it better
I add an assertion to compare it with the assertIsNot method
78def test_assertIsNot(): 79 # unittest.TestCase.assertIsNot() 80 # unittest.TestCase().assertIsNot() 81 # unittest.TestCase().assertIsNot(None, None) 82 assert None is not None 83 unittest.TestCase().assertIsNot(None, False) 84 85 86'assertIsNot'the terminal is my friend, and shows AssertionError
E assert None is not NoneI change the assertion to make it True
78def test_assertIsNot(): 79 # unittest.TestCase.assertIsNot() 80 # unittest.TestCase().assertIsNot() 81 # unittest.TestCase().assertIsNot(None, None) 82 # assert None is not None 83 assert None is not False 84 unittest.TestCase().assertIsNot(None, False) 85 86 87'assertIsNot' 88'assertIs'the test passes.
I remove assertIsNot from the TODO list
84 unittest.TestCase().assertIsNot(None, False) 85 86 87'assertIs' 88'assertNotEqual'I add a git commit message in the other terminal
git commit -am \ 'add test_assertIsNot'
I imagine Python follows this path when unittest.TestCase.assertIsNot is called
unittest.TestCase.assertIsNot
└── unittest
└── class TestCase:
└── def assertIsNot(self, expr1, expr2):
└── assert expr1 is not expr2
Compare the error message for assertIsNot(None, None) with the one for assert None is not None
AssertionError: unexpectedly identical: None
vs
assert None is not None
Which do you like better?
test_assertIs
RED: make it fail
I go back to the terminal where the tests are running
I add a test for the assertIs method
78def test_assertIsNot(): 79 # unittest.TestCase.assertIsNot() 80 # unittest.TestCase().assertIsNot() 81 # unittest.TestCase().assertIsNot(None, None) 82 # assert None is not None 83 assert None is not False 84 unittest.TestCase().assertIsNot(None, False) 85 86 87def test_assertIs(): 88 assert False is True 89 90 91'assertIs'the terminal is my friend, and shows AssertionError
E assert False is True
GREEN: make it pass
I change the assertion to make it True
87def test_assertIs():
88 # assert False is True
89 assert False is False
90
91
92'assertIs'
the test passes.
REFACTOR: make it better
I add a call to the assertIs method
87def test_assertIs(): 88 # assert False is True 89 assert False is False 90 unittest.TestCase.assertIs() 91 92 93'assertIs'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIs() missing 3 required positional arguments: 'self', 'expr1', and 'expr2'because a method of an instance takes the instance of the class (
self) it belongs to as the first argument.I change the call to use an instance instead of the unittest.TestCase class
87def test_assertIs(): 88 # assert False is True 89 assert False is False 90 # unittest.TestCase.assertIs() 91 unittest.TestCase().assertIs() 92 93 94'assertIs'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIs() missing 2 required positional arguments: 'expr1' and 'expr2'I no longer need to provide
selfbecause it is the instance of the class (unittest.TestCase())I add two things to the call to unittest.TestCase.assertIs
87def test_assertIs(): 88 # assert False is True 89 assert False is False 90 # unittest.TestCase.assertIs() 91 # unittest.TestCase().assertIs() 92 unittest.TestCase().assertIs(False, True) 93 94 95'assertIs'the terminal is my friend, and shows AssertionError
AssertionError: False is not Truebecause False is not True.
I change the assertion to make it True
87def test_assertIs(): 88 # assert False is True 89 assert False is False 90 # unittest.TestCase.assertIs() 91 # unittest.TestCase().assertIs() 92 # unittest.TestCase().assertIs(False, True) 93 unittest.TestCase().assertIs(False, False) 94 95 96'assertIs' 97'assertNotEqual'the test passes.
I remove assertIs from the TODO list
93 unittest.TestCase().assertIs(False, False) 94 95 96'assertNotEqual' 97'assertEqual'I add a git commit message in the other terminal
git commit -am \ 'add test_assertIs'
I imagine Python follows this path when unittest.TestCase.assertIs is called
unittest.TestCase.assertIs
└── unittest
└── class TestCase:
└── def assertIs(self, expr1, expr2):
└── assert expr1 is expr2
Compare the error message for assertIs(False, True) with the one for assert False is True
AssertionError: False is not True
vs
E assert False is True
test_assertNotEqual
RED: make it fail
I go back to the terminal where the tests are running
I add a test for assertNotEqual
87def test_assertIs(): 88 # assert False is True 89 assert False is False 90 # unittest.TestCase.assertIs() 91 # unittest.TestCase().assertIs() 92 # unittest.TestCase().assertIs(False, True) 93 unittest.TestCase().assertIs(False, False) 94 95 96def test_assertNotEqual(): 97 unittest.TestCase().assertNotEqual() 98 99 100'assertNotEqual'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertNotEqual() missing 2 required positional arguments: 'first' and 'second'the definition of the assertNotEqual method of the TestCase class of the unittest library (
unittest.TestCase.assertNotEqual) has two required positional arguments (firstandsecond).
GREEN: make it pass
I add two things to the call to unittest.TestCase.assertNotEqual
96def test_assertNotEqual(): 97 # unittest.TestCase().assertNotEqual() 98 unittest.TestCase().assertNotEqual(True, True) 99 100 101'assertNotEqual'the terminal is my friend, and shows AssertionError
AssertionError: True == Truebecause True is True.
I change the assertion to make it True
96def test_assertNotEqual(): 97 # unittest.TestCase().assertNotEqual() 98 # unittest.TestCase().assertNotEqual(True, True) 99 unittest.TestCase().assertNotEqual(True, 0) 100 101 102'assertNotEqual'the test passes.
REFACTOR: make it better
I add an assertion to compare it with the assertNotEqual method
96def test_assertNotEqual(): 97 # unittest.TestCase().assertNotEqual() 98 # unittest.TestCase().assertNotEqual(True, True) 99 assert True != True 100 unittest.TestCase().assertNotEqual(True, 0) 101 102 103'assertNotEqual'the terminal is my friend, and shows AssertionError
E assert True != TrueI change the assertion to make it True
96def test_assertNotEqual(): 97 # unittest.TestCase().assertNotEqual() 98 # unittest.TestCase().assertNotEqual(True, True) 99 # assert True != True 100 assert True != 0 101 unittest.TestCase().assertNotEqual(True, 0) 102 103 104'assertNotEqual' 105'assertEqual'the test passes.
I remove assertNotEqual from the TODO list
101 unittest.TestCase().assertNotEqual(True, 0) 102 103 104'assertEqual' 105'assertNotIsInstance'I add a git commit message in the other terminal
git commit -am \ 'add test_assertNotEqual'
I imagine Python follows this path when unittest.TestCase.assertNotEqual is called
unittest.TestCase.assertNotEqual
└── unittest
└── class TestCase:
└── def assertNotEqual(self, first, second):
└── assert first != second
Compare the error message for assertNotEqual(True, True) with the one for assert True != True
AssertionError: True == True
vs
E assert True != True
test_assertEqual
RED: make it fail
I go back to the terminal where the tests are running
I add a test for the assertEqual method
96def test_assertNotEqual(): 97 # unittest.TestCase().assertNotEqual() 98 # unittest.TestCase().assertNotEqual(True, True) 99 # assert True != True 100 assert True != 0 101 unittest.TestCase().assertNotEqual(True, 0) 102 103 104def test_assertEqual(): 105 assert 0.0 == '0.0' 106 107 108'assertEqual'the terminal is my friend, and shows AssertionError
AssertionError: assert 0.0 == '0.0'
GREEN: make it pass
I change the assertion to make it True
104def test_assertEqual():
105 # assert 0.0 == '0.0'
106 assert 0.0 == 0.0
107
108
109'assertEqual'
the test passes.
REFACTOR: make it better
I add a call to the assertEqual method
104def test_assertEqual(): 105 # assert 0.0 == '0.0' 106 assert 0.0 == 0.0 107 unittest.TestCase().assertEqual(0.0, '0.0') 108 109 110'assertEqual'the terminal is my friend, and shows AssertionError
AssertionError: 0.0 != '0.0'I change the assertion to make it True
104def test_assertEqual(): 105 # assert 0.0 == '0.0' 106 assert 0.0 == 0.0 107 # unittest.TestCase().assertEqual(0.0, '0.0') 108 unittest.TestCase().assertEqual(0.0, 0.0) 109 110 111'assertEqual' 112'assertNotIsInstance'the test passes.
I remove assertEqual from the TODO list
108 unittest.TestCase().assertEqual(0.0, 0.0) 109 110 111'assertNotIsInstance' 112'assertIsInstance'I add a git commit message in the other terminal
git commit -am \ 'add test_assertEqual'
I imagine Python follows this path when unittest.TestCase.assertEqual is called
unittest.TestCase.assertEqual
└── unittest
└── class TestCase:
└── def assertEqual(self, first, second):
└── assert first == second
Compare the error message for assertEqual(0.0, '0.0') with the one for assert 0.0 == '0.0'
AssertionError: 0.0 != '0.0'
vs
AssertionError: assert 0.0 == '0.0'
test_assertNotIsInstance
RED: make it fail
I go back to the terminal where the tests are running
I add a test for the assertNotIsInstance method
104def test_assertEqual(): 105 # assert 0.0 == '0.0' 106 assert 0.0 == 0.0 107 # unittest.TestCase().assertEqual(0.0, '0.0') 108 unittest.TestCase().assertEqual(0.0, 0.0) 109 110 111def test_assertNotIsInstance(): 112 unittest.TestCase.assertNotIsInstance() 113 114 115'assertNotIsInstance'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertNotIsInstance() missing 3 required positional arguments: 'self', 'obj', and 'cls'The definition of the assertNotIsInstance method of the TestCase class of the unittest library (
unittest.TestCase.assertNotIsInstance) has three required positional arguments (self,objandcls)A method of an instance takes the instance of the class (
self) it belongs to as the first argument.objis for the instance being tested.clsis for the object.
GREEN: make it pass
I change the call to use an instance instead of the unittest.TestCase class
111def test_assertNotIsInstance(): 112 # unittest.TestCase.assertNotIsInstance() 113 unittest.TestCase().assertNotIsInstance() 114 115 116'assertNotIsInstance'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertNotIsInstance() missing 2 required positional arguments: 'obj' and 'cls'I add two things to the call to unittest.TestCase.assertNotIsInstance
111def test_assertNotIsInstance(): 112 # unittest.TestCase.assertNotIsInstance() 113 # unittest.TestCase().assertNotIsInstance() 114 unittest.TestCase().assertNotIsInstance( 115 unittest.TestCase(), unittest.TestCase 116 ) 117 118 119'assertNotIsInstance'the terminal is my friend, and shows AssertionError
AssertionError: <unittest.case.TestCase testMethod=runTest> is an instance of <class 'unittest.case.TestCase'>I change the assertion to make it True
111def test_assertNotIsInstance(): 112 # unittest.TestCase.assertNotIsInstance() 113 # unittest.TestCase().assertNotIsInstance() 114 unittest.TestCase().assertNotIsInstance( 115 # unittest.TestCase(), unittest.TestCase 116 unittest.TestCase, unittest.TestCase 117 ) 118 119 120'assertNotIsInstance'the test passes because an object is not an instance of itself.
REFACTOR: make it better
I add an assertion to compare it with the assertNotIsInstance method
111def test_assertNotIsInstance(): 112 # unittest.TestCase.assertNotIsInstance() 113 # unittest.TestCase().assertNotIsInstance() 114 assert not isinstance( 115 unittest.TestCase(), unittest.TestCase 116 ) 117 unittest.TestCase().assertNotIsInstance( 118 # unittest.TestCase(), unittest.TestCase 119 unittest.TestCase, unittest.TestCase 120 ) 121 122 123'assertNotIsInstance'the terminal is my friend, and shows AssertionError
E AssertionError: assert not TrueI change the assertion to make it True
111def test_assertNotIsInstance(): 112 # unittest.TestCase.assertNotIsInstance() 113 # unittest.TestCase().assertNotIsInstance() 114 assert not isinstance( 115 # unittest.TestCase(), unittest.TestCase 116 unittest.TestCase, unittest.TestCase 117 ) 118 unittest.TestCase().assertNotIsInstance( 119 # unittest.TestCase(), unittest.TestCase 120 unittest.TestCase, unittest.TestCase 121 ) 122 123 124'assertNotIsInstance' 125'assertIsInstance'the test passes.
I remove assertNotIsInstance from the TODO list
118 unittest.TestCase().assertNotIsInstance( 119 # unittest.TestCase(), unittest.TestCase 120 unittest.TestCase, unittest.TestCase 121 ) 122 123 124'assertIsInstance' 125'assertNotIsSubclass'I add a git commit message in the other terminal
git commit -am \ 'add test_assertNotIsInstance'
I imagine Python follows this path when unittest.TestCase.assertNotIsInstance is called
unittest.TestCase.assertNotIsInstance
└── unittest
└── class TestCase:
└── def assertNotIsInstance(self, obj, cls):
└── assert not isinstance(obj, cls)
Compare the error message for unittest.TestCase().assertNotIsInstance(unittest.TestCase(), unittest.TestCase) with the one for assert not isinstance(unittest.TestCase(), unittest.TestCase)
AssertionError:
<unittest.case.TestCase testMethod=runTest>
is an instance of
<class 'unittest.case.TestCase'>
vs
AssertionError: assert not True
test_assertIs_instance
RED: make it fail
I go back to the terminal where the tests are running
I add a test for assertIsInstance
118 unittest.TestCase().assertNotIsInstance( 119 # unittest.TestCase(), unittest.TestCase 120 unittest.TestCase, unittest.TestCase 121 ) 122 123 124def test_assertIs_instance(): 125 unittest.TestCase.assertIsInstance() 126 127 128'assertIsInstance'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIsInstance() missing 3 required positional arguments: 'self', 'obj', and 'cls'A method of an instance takes the instance of the class (
self) it belongs to as the first argument.objis for the instance being tested.clsis for the object.
GREEN: make it pass
I change the call to use an instance instead of the unittest.TestCase class
124def test_assertIs_instance(): 125 # unittest.TestCase.assertIsInstance() 126 unittest.TestCase().assertIsInstance() 127 128 129'assertIsInstance'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIsInstance() missing 2 required positional arguments: 'obj' and 'cls'I add two things to the call to unittest.TestCase.assertIsInstance
124def test_assertIs_instance(): 125 # unittest.TestCase.assertIsInstance() 126 # unittest.TestCase().assertIsInstance() 127 unittest.TestCase().assertIsInstance( 128 unittest.TestCase, unittest.TestCase 129 ) 130 131 132'assertIsInstance'the terminal is my friend, and shows AssertionError
AssertionError: <class 'unittest.case.TestCase'> is not an instance of <class 'unittest.case.TestCase'>I change the assertion to make it True
124def test_assertIs_instance(): 125 # unittest.TestCase.assertIsInstance() 126 # unittest.TestCase().assertIsInstance() 127 unittest.TestCase().assertIsInstance( 128 # unittest.TestCase, unittest.TestCase 129 unittest.TestCase(), unittest.TestCase 130 ) 131 132 133'assertIsInstance'the test passes.
REFACTOR: make it better
I add an assertion to compare it with the assertIsInstance method
124def test_assertIs_instance(): 125 # unittest.TestCase.assertIsInstance() 126 # unittest.TestCase().assertIsInstance() 127 assert isinstance( 128 unittest.TestCase, unittest.TestCase 129 ) 130 unittest.TestCase().assertIsInstance( 131 # unittest.TestCase, unittest.TestCase 132 unittest.TestCase(), unittest.TestCase 133 ) 134 135 136'assertIsInstance'the terminal is my friend, and shows AssertionError
AssertionError: assert FalseI change the assertion to make it True
124def test_assertIs_instance(): 125 # unittest.TestCase.assertIsInstance() 126 # unittest.TestCase().assertIsInstance() 127 assert isinstance( 128 # unittest.TestCase, unittest.TestCase 129 unittest.TestCase(), unittest.TestCase 130 ) 131 unittest.TestCase().assertIsInstance( 132 # unittest.TestCase, unittest.TestCase 133 unittest.TestCase(), unittest.TestCase 134 ) 135 136 137'assertIsInstance'the test passes.
I add variables
124def test_assertIs_instance(): 125 an_object = unittest.TestCase 126 an_instance = an_object() 127 # unittest.TestCase.assertIsInstance() 128 # unittest.TestCase().assertIsInstance() 129 assert isinstance( 130 # unittest.TestCase, unittest.TestCase 131 unittest.TestCase(), unittest.TestCase 132 ) 133 unittest.TestCase().assertIsInstance( 134 # unittest.TestCase, unittest.TestCase 135 unittest.TestCase(), unittest.TestCase 136 ) 137 138 139'assertIsInstance' 140'assertNotIsSubclass'I use the variables to remove repetition of
unittest.TestCaseandunittest.TestCase()124def test_assertIs_instance(): 125 an_object = unittest.TestCase 126 an_instance = an_object() 127 # unittest.TestCase.assertIsInstance() 128 # unittest.TestCase().assertIsInstance() 129 assert isinstance( 130 # unittest.TestCase, unittest.TestCase 131 # unittest.TestCase(), unittest.TestCase 132 an_instance, an_object 133 ) 134 unittest.TestCase().assertIsInstance( 135 # unittest.TestCase, unittest.TestCase 136 # unittest.TestCase(), unittest.TestCase 137 an_instance, an_object 138 ) 139 140 141'assertIsInstance'the test is still green.
I remove assertIsInstance from the TODO list
134 unittest.TestCase().assertIsInstance( 135 # unittest.TestCase, unittest.TestCase 136 # unittest.TestCase(), unittest.TestCase 137 an_instance, an_object 138 ) 139 140 141'assertNotIsSubclass' 142'assertIsSubclass'I add a git commit message in the other terminal
git commit -am \ 'add test_assertIs_instance'
I imagine Python follows this path when unittest.TestCase.assertIsInstance is called
unittest.TestCase.assertIsInstance
└── unittest
└── class TestCase:
└── def assertIsInstance(self, obj, cls):
└── assert isinstance(obj, cls)
Compare the error message for unittest.TestCase().assertIsInstance(unittest.TestCase, unittest.TestCase) with the one for assert isinstance(unittest.TestCase, unittest.TestCase)
AssertionError:
<class 'unittest.case.TestCase'>
is not an instance of
<class 'unittest.case.TestCase'>
vs
AssertionError: assert False
test_assertNotIsSubclass
Caution
test_assertNotIsSubclass will only work if your Python version is 3.14 or newer
RED: make it fail
I go back to the terminal where the tests are running
I add a test for assertNotIsSubclass
134 unittest.TestCase().assertIsInstance( 135 # unittest.TestCase, unittest.TestCase 136 # unittest.TestCase(), unittest.TestCase 137 an_instance, an_object 138 ) 139 140 141def test_assertNotIsSubclass(): 142 unittest.TestCase().assertNotIsSubclass() 143 144 145'assertNotIsSubclass'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertNotIsSubclass() missing 2 required positional arguments: 'cls' and 'superclass'clsis for the subclass being tested.superclassis for the parent class of the subclass being tested.
GREEN: make it pass
I add two things to the call to unittest.TestCase.assertNotIsSubclass
141def test_assertNotIsSubclass(): 142 # unittest.TestCase().assertNotIsSubclass() 143 unittest.TestCase().assertNotIsSubclass( 144 unittest.TestCase(), object 145 ) 146 147 148'assertNotIsSubclass'the terminal is my friend, and shows AssertionError
AssertionError: <unittest.case.TestCase testMethod=runTest> is not a classbecause an instance is not a class.
I change the assertion to use an object
141def test_assertNotIsSubclass(): 142 # unittest.TestCase().assertNotIsSubclass() 143 unittest.TestCase().assertNotIsSubclass( 144 # unittest.TestCase(), object 145 unittest.TestCase, object 146 ) 147 148 149'assertNotIsSubclass'the terminal is my friend, and shows AssertionError
AssertionError: <class 'unittest.case.TestCase'> is a subclass of <class 'object'>because all objects inherit from ‘object’.
I change the assertion to make it True
141def test_assertNotIsSubclass(): 142 # unittest.TestCase().assertNotIsSubclass() 143 unittest.TestCase().assertNotIsSubclass( 144 # unittest.TestCase(), object 145 # unittest.TestCase, object 146 unittest.TestCase, dict 147 ) 148 149 150'assertNotIsSubclass'the test passes because the unittest.TestCase class is not a subclass of the dict class (the class for dictionaries).
REFACTOR: make it better
I add an assertion to compare it with the assertNotIsSubobject method
141def test_assertNotIsSubclass(): 142 # unittest.TestCase().assertNotIsSubclass() 143 assert not issubclass( 144 unittest.TestCase, object 145 ) 146 unittest.TestCase().assertNotIsSubclass( 147 # unittest.TestCase(), object 148 # unittest.TestCase, object 149 unittest.TestCase, dict 150 ) 151 152 153'assertNotIsSubclass'the terminal is my friend, and shows AssertionError
E AssertionError: assert not TrueI change the assertion to make it True
141def test_assertNotIsSubclass(): 142 # unittest.TestCase().assertNotIsSubclass() 143 assert not issubclass( 144 # unittest.TestCase, object 145 unittest.TestCase, list 146 ) 147 unittest.TestCase().assertNotIsSubclass( 148 # unittest.TestCase(), object 149 # unittest.TestCase, object 150 unittest.TestCase, dict 151 ) 152 153 154'assertNotIsSubclass' 155'assertIsSubclass'the test passes because the unittest.TestCase class is not a subclass of the list class (the class for lists).
I remove assertNotIsSubclass from the TODO list
147 unittest.TestCase().assertNotIsSubclass( 148 # unittest.TestCase(), object 149 # unittest.TestCase, object 150 unittest.TestCase, dict 151 ) 152 153 154'assertIsSubclass' 155 156 157# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_assertNotIsSubclass'
I imagine Python follows this path when unittest.TestCase.assertNotIsSubclass is called
unittest.TestCase.assertNotIsSubclass
└── unittest
└── class TestCase:
└── def assertNotIsSubclass(self, cls, superclass):
└── assert not issubclass(cls, superclass)
Compare the error message for unittest.TestCase().assertNotIsSubclass(unittest.TestCase, object) with the one for assert not issubclass(unittest.TestCase, object)
AssertionError:
<class 'unittest.case.TestCase'>
is a subclass of <class 'object'>
vs
AssertionError: assert not True
test_assertIs_subclass
Caution
test_assertIs_subclass will only work if your Python version is 3.14 or newer
RED: make it fail
I go back to the terminal where the tests are running
I add a test for the assertIsSubobject method
147 unittest.TestCase().assertNotIsSubclass( 148 # unittest.TestCase(), object 149 # unittest.TestCase, object 150 unittest.TestCase, dict 151 ) 152 153 154def test_assertIs_subclass(): 155 assert issubclass(unittest.TestCase(), set) 156 157 158'assertIsSubclass'the terminal is my friend, and shows TypeError
TypeError: issubclass() arg 1 must be a class
GREEN: make it pass
I change the instance to the object
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 assert issubclass(unittest.TestCase, set) 157 158 159'assertIsSubclass'the terminal is my friend, and shows AssertionError
AssertionError: assert FalseI change the assertion to make it True
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 # assert issubclass(unittest.TestCase, set) 157 assert issubclass(unittest.TestCase, object) 158 159 160'assertIsSubclass'the test passes.
I add a call to assertIsSubclass
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 # assert issubclass(unittest.TestCase, set) 157 assert issubclass(unittest.TestCase, object) 158 unittest.TestCase.assertIsSubclass() 159 160 161'assertIsSubclass'the terminal is my friend, and shows TypeError
TypeError: TestCase.assertIsSubclass() missing 3 required positional arguments: 'self', 'cls', and 'superclass'A method of an instance takes the instance of the class (
self) it belongs to as the first argument.clsis for the subclass being testedsuperclassis for the parent class of the subclass being tested.
I change the call to use an instance instead of the unittest.TestCase class, then add two things to the parentheses
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 # assert issubclass(unittest.TestCase, set) 157 assert issubclass(unittest.TestCase, object) 158 # unittest.TestCase.assertIsSubclass() 159 unittest.TestCase().assertIsSubclass( 160 unittest.TestCase(), set 161 ) 162 163 164'assertIsSubclass'the terminal is my friend, and shows AssertionError
AssertionError: <unittest.case.TestCase testMethod=runTest> is not a classbecause an instance is not a class.
I change the instance to the object
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 # assert issubclass(unittest.TestCase, set) 157 assert issubclass(unittest.TestCase, object) 158 # unittest.TestCase.assertIsSubclass() 159 unittest.TestCase().assertIsSubclass( 160 # unittest.TestCase(), set 161 unittest.TestCase, set 162 ) 163 164 165'assertIsSubclass'the terminal is my friend, and shows AssertionError
AssertionError: <class 'unittest.case.TestCase'> is not a subclass of <class 'set'>I change the assertion to make it True
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 # assert issubclass(unittest.TestCase, set) 157 assert issubclass(unittest.TestCase, object) 158 # unittest.TestCase.assertIsSubclass() 159 unittest.TestCase().assertIsSubclass( 160 # unittest.TestCase(), set 161 # unittest.TestCase, set 162 unittest.TestCase, object 163 ) 164 165 166'assertIsSubclass' 167 168 169# Exceptions seenthe test passes because all objects inherit from ‘object’.
REFACTOR: make it better
I remove assertIsSubclass from the TODO list
154def test_assertIs_subclass(): 155 # assert issubclass(unittest.TestCase(), set) 156 # assert issubclass(unittest.TestCase, set) 157 assert issubclass(unittest.TestCase, object) 158 # unittest.TestCase.assertIsSubclass() 159 unittest.TestCase().assertIsSubclass( 160 # unittest.TestCase(), set 161 # unittest.TestCase, set 162 unittest.TestCase, object 163 ) 164 165 166# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'add test_assertIs_subclass'
I imagine Python follows this path when unittest.TestCase.assertIsSubclass is called
unittest.TestCase.assertIsSubclass
└── unittest
└── class TestCase:
└── def assertIsSubclass(self, cls, superclass):
└── assert issubclass(cls, superclass)
Compare the error message for unittest.TestCase().assertIsSubclass(unittest.TestCase, tuple) with the one for assert issubclass(unittest.TestCase, set)
AssertionError:
<class 'unittest.case.TestCase'>
is not a subclass of <class 'tuple'>
vs
AssertionError: assert False
Which of the error messages in this chapter do you like better?
extract TOOLBOX variable
I make an instance of the unittest.TestCase class to use its assert methods in every test. I can use a variable to remove repetition of that process (unittest.TestCase()) from every test.
I add a variable for
unittest.TestCase()at the top of the file1import unittest 2 3 4TOOLBOX = unittest.TestCase() 5 6 7def test_dir_unittest():I use the variable for
unittest.TestCase()in test_assertIsNot81def test_assertIsNot(): 82 # unittest.TestCase.assertIsNot() 83 # unittest.TestCase().assertIsNot() 84 # unittest.TestCase().assertIsNot(None, None) 85 # assert None is not None 86 assert None is not False 87 # unittest.TestCase().assertIsNot(None, False) 88 TOOLBOX.assertIsNot(None, False) 89 90 91def test_assertIs():the test is still green.
I remove the commented lines from test_assertIsNot
81def test_assertIsNot(): 82 assert None is not False 83 TOOLBOX.assertIsNot(None, False) 84 85 86def test_assertIs():I use the variable for
unittest.TestCase()in test_assertIs86def test_assertIs(): 87 # assert False is True 88 assert False is False 89 # unittest.TestCase.assertIs() 90 # unittest.TestCase().assertIs() 91 # unittest.TestCase().assertIs(False, True) 92 # unittest.TestCase().assertIs(False, False) 93 TOOLBOX.assertIs(False, False) 94 95 96def test_assertNotEqual():still green.
I remove the commented lines from test_assertIs
86def test_assertIs(): 87 assert False is False 88 TOOLBOX.assertIs(False, False) 89 90 91def test_assertNotEqual():I use the variable for
unittest.TestCase()in test_assertNotEqual91def test_assertNotEqual(): 92 # unittest.TestCase().assertNotEqual() 93 # unittest.TestCase().assertNotEqual(True, True) 94 # assert True != True 95 assert True != 0 96 # unittest.TestCase().assertNotEqual(True, 0) 97 TOOLBOX.assertNotEqual(True, 0) 98 99 100def test_assertEqual():green.
I remove the commented lines from test_assertNotEqual
91def test_assertNotEqual(): 92 assert True != 0 93 TOOLBOX.assertNotEqual(True, 0) 94 95 96def test_assertEqual():I use the variable for
unittest.TestCase()in test_assertEqual96def test_assertEqual(): 97 # assert 0.0 == '0.0' 98 assert 0.0 == 0.0 99 # unittest.TestCase().assertEqual(0.0, '0.0') 100 # unittest.TestCase().assertEqual(0.0, 0.0) 101 TOOLBOX.assertEqual(0.0, 0.0) 102 103 104def test_assertNotIsInstance():still green.
I remove the commented lines from test_assertEqual
96def test_assertEqual(): 97 assert 0.0 == 0.0 98 TOOLBOX.assertEqual(0.0, 0.0) 99 100 101def test_assertNotIsInstance():I use the variable for
unittest.TestCase()in test_assertNotIsInstance101def test_assertNotIsInstance(): 102 # unittest.TestCase.assertNotIsInstance() 103 # unittest.TestCase().assertNotIsInstance() 104 assert not isinstance( 105 # unittest.TestCase(), unittest.TestCase 106 unittest.TestCase, unittest.TestCase 107 ) 108 # unittest.TestCase().assertNotIsInstance( 109 TOOLBOX.assertNotIsInstance( 110 # unittest.TestCase(), unittest.TestCase 111 unittest.TestCase, unittest.TestCase 112 ) 113 114 115def test_assertIs_instance():the test is still green.
I remove the commented lines from test_assertNotIsInstance
101def test_assertNotIsInstance(): 102 assert not isinstance( 103 unittest.TestCase, unittest.TestCase 104 ) 105 TOOLBOX.assertNotIsInstance( 106 unittest.TestCase, unittest.TestCase 107 ) 108 109 110def test_assertIs_instance():I use the variable for
unittest.TestCase()in test_assertIs_instance110def test_assertIs_instance(): 111 an_object = unittest.TestCase 112 an_instance = an_object() 113 # unittest.TestCase.assertIsInstance() 114 # unittest.TestCase().assertIsInstance() 115 assert isinstance( 116 # unittest.TestCase, unittest.TestCase 117 # unittest.TestCase(), unittest.TestCase 118 an_instance, an_object 119 ) 120 # unittest.TestCase().assertIsInstance( 121 TOOLBOX.assertIsInstance( 122 # unittest.TestCase, unittest.TestCase 123 # unittest.TestCase(), unittest.TestCase 124 an_instance, an_object 125 ) 126 127 128def test_assertNotIsSubclass():still green.
I remove the commented lines from test_assertIs_instance
110def test_assertIs_instance(): 111 an_object = unittest.TestCase 112 an_instance = an_object() 113 114 assert isinstance(an_instance, an_object) 115 TOOLBOX.assertIsInstance( 116 an_instance, an_object 117 ) 118 119 120def test_assertNotIsSubclass():I use the variable for
unittest.TestCase()in test_assertNotIsSubclass120def test_assertNotIsSubclass(): 121 # unittest.TestCase().assertNotIsSubclass() 122 assert not issubclass( 123 # unittest.TestCase, object 124 unittest.TestCase, list 125 ) 126 # unittest.TestCase().assertNotIsSubclass( 127 TOOLBOX.assertNotIsSubclass( 128 # unittest.TestCase(), object 129 # unittest.TestCase, object 130 unittest.TestCase, dict 131 ) 132 133 134def test_assertIs_subclass():green.
I remove the commented lines from test_assertNotIsSubclass
120def test_assertNotIsSubclass(): 121 assert not issubclass( 122 unittest.TestCase, list 123 ) 124 TOOLBOX.assertNotIsSubclass( 125 unittest.TestCase, dict 126 ) 127 128 129def test_assertIs_subclass():I use the variable for
unittest.TestCase()in test_assertIs_subclass129def test_assertIs_subclass(): 130 # assert issubclass(unittest.TestCase(), set) 131 # assert issubclass(unittest.TestCase, set) 132 assert issubclass(unittest.TestCase, object) 133 # unittest.TestCase.assertIsSubclass() 134 # unittest.TestCase().assertIsSubclass( 135 TOOLBOX.assertIsSubclass( 136 # unittest.TestCase(), set 137 # unittest.TestCase, set 138 unittest.TestCase, object 139 ) 140 141 142# Exceptions seenstill green.
I remove the commented lines from test_assertIs_subclass
129def test_assertIs_subclass(): 130 assert issubclass(unittest.TestCase, object) 131 TOOLBOX.assertIsSubclass( 132 unittest.TestCase, object 133 ) 134 135 136# Exceptions seenI add a git commit message in the other terminal
git commit -am \ 'extract TOOLBOX variable'
The unittest.TestCase() instance is now made once then used in every test for its assert methods.
The problem with this solution is that anyone reading it has to know what the TOOLBOX variable points to at the beginning of the file to understand why the calls work.
extract TestUnittest object
I can put the test functions together in an object since they are related.
RED: make it fail
I go back to the terminal where the tests are running
I add an object named
Unittest4TOOLBOX = unittest.TestCase() 5 6 7class Unittest(object): 8 9 def test_failure(): 10 TOOLBOX.assertEqual(True, False) 11 12 13def test_dir_unittest():this is a problem. I expect
TOOLBOX.assertEqual(True, False)to fail since True is not equal to FalseI add
Testto the name of the object4TOOLBOX = unittest.TestCase() 5 6 7# class Unittest(object): 8class TestUnittest(object): 9 10 def test_failure():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_failure() 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 the staticmethod decorator to
test_failuresince I can use it if I do not want to addselfto the method definition. This way I do not send more information than what the method needs when it does not use anything that belongs to the object7# class Unittest(object): 8class TestUnittest(object): 9 10 @staticmethod 11 def test_failure(): 12 TOOLBOX.assertEqual(True, False) 13 14 15def test_dir_unittest():the terminal is my friend, and shows AssertionError
AssertionError: True != FalseI change False to True in the assertion
7# class Unittest(object): 8class TestUnittest(object): 9 10 @staticmethod 11 def test_failure(): 12 # TOOLBOX.assertEqual(True, False) 13 TOOLBOX.assertEqual(True, True) 14 15 16def test_dir_unittest():the test passes.
REFACTOR: make it better
I remove
test_failureI move test_dir_unittest to replace
test_failureas a method of the TestUnittest class7# class Unittest(object): 8class TestUnittest(object): 9 10 def test_dir_unittest(): 11 reality = dir(unittest) 12 my_expectation = [ 13 'BaseTestSuite', 'FunctionTestCase', 14 'IsolatedAsyncioTestCase', 'SkipTest', 15 ...Caution
Indentation matters in Python. It is how it knows what blocks belong to what function/method, object or module (Use 4 spaces)
the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_dir_unittest() takes 0 positional arguments but 1 was givenI add the staticmethod decorator to test_dir_unittest
7# class Unittest(object): 8class TestUnittest(object): 9 10 @staticmethod 11 def test_dir_unittest():the test passes.
I add a call to the assertNotEqual method
30 assert reality == my_expectation 31 TOOLBOX.assertNotEqual(reality, my_expectation) 32 33 34def test_dir_unittest_testcase():the terminal is my friend, and shows AssertionError.
I change assertNotEqual to assertEqual
30 assert reality == my_expectation 31 # TOOLBOX.assertNotEqual(reality, my_expectation) 32 TOOLBOX.assertEqual(reality, my_expectation) 33 34 35def test_dir_unittest_testcase():the test passes.
I move test_dir_unittest_testcase to make it a method of the TestUnittest class
32 TOOLBOX.assertEqual(reality, my_expectation) 33 34 def test_dir_unittest_testcase(): 35 reality = dir(unittest.TestCase) 36 my_expectation = [ 37 '__call__', '__class__', '__delattr__', 38 ...TypeError: TestUnittest.test_dir_unittest_testcase() takes 0 positional arguments but 1 was givenI add the staticmethod decorator to test_dir_unittest_testcase
32 TOOLBOX.assertEqual(reality, my_expectation) 33 34 @staticmethod 35 def test_dir_unittest_testcase():the test passes.
I add a call to the assertNotEqual method
84 assert reality == my_expectation 85 TOOLBOX.assertNotEqual(reality, my_expectation) 86 87 88def test_assertIsNot():the terminal is my friend, and shows AssertionError.
I change assertNotEqual to assertEqual
84 assert reality == my_expectation 85 # TOOLBOX.assertNotEqual(reality, my_expectation) 86 TOOLBOX.assertEqual(reality, my_expectation) 87 88 89def test_assertIsNot():the test passes.
I indent (move with four spaces) test_assertIsNot to make it a method of the TestUnittest class
86 TOOLBOX.assertEqual(reality, my_expectation) 87 88 def test_assertIsNot(): 89 assert None is not False 90 TOOLBOX.assertIsNot(None, False) 91 92 93def test_assertIs():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertIsNot() 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.I add the staticmethod decorator to test_assertIsNot
86 TOOLBOX.assertEqual(reality, my_expectation) 87 88 @staticmethod 89 def test_assertIsNot():the test passes.
I move test_assertIs to make it a method of the TestUnittest class
91 TOOLBOX.assertIsNot(None, False) 92 93 def test_assertIs(): 94 assert False is False 95 TOOLBOX.assertIs(False, False) 96 97 98def test_assertNotEqual():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertIs() 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.I add the staticmethod decorator to test_assertIs
91 TOOLBOX.assertIsNot(None, False) 92 93 @staticmethod 94 def test_assertIs():the test passes.
I indent test_assertNotEqual to make it a method of the TestUnittest class
96 TOOLBOX.assertIs(False, False) 97 98 def test_assertNotEqual(): 99 assert True != 0 100 TOOLBOX.assertNotEqual(True, 0) 101 102 103def test_assertEqual():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertNotEqual() takes 0 positional arguments but 1 was givenbecause …
I add the staticmethod decorator to test_assertNotEqual
96 TOOLBOX.assertIs(False, False) 97 98 @staticmethod 99 def test_assertNotEqual():the test passes.
I move test_assertEqual to make it a method of the TestUnittest class
101 TOOLBOX.assertNotEqual(True, 0) 102 103 def test_assertEqual(): 104 assert 0.0 == 0.0 105 TOOLBOX.assertEqual(0.0, 0.0) 106 107 108def test_assertNotIsInstance():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertEqual() 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.I add the staticmethod decorator to test_assertEqual
101 TOOLBOX.assertNotEqual(True, 0) 102 103 @staticmethod 104 def test_assertEqual():the test passes.
I move test_assertNotIsInstance to make it a method of the TestUnittest class
106 TOOLBOX.assertEqual(0.0, 0.0) 107 108 def test_assertNotIsInstance(): 109 assert not isinstance( 110 unittest.TestCase, unittest.TestCase 111 ) 112 TOOLBOX.assertNotIsInstance( 113 unittest.TestCase, unittest.TestCase 114 ) 115 116 117def test_assertIs_instance():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertNotIsInstance() takes 0 positional arguments but 1 was givenI add the staticmethod decorator to test_assertNotIsInstance
106 TOOLBOX.assertEqual(0.0, 0.0) 107 108 @staticmethod 109 def test_assertNotIsInstance():the test passes.
I move test_assertIs_instance to make it a method of the TestUnittest class
113 TOOLBOX.assertNotIsInstance( 114 unittest.TestCase, unittest.TestCase 115 ) 116 117 def test_assertIs_instance(): 118 an_object = unittest.TestCase 119 an_instance = an_object() 120 121 assert isinstance(an_instance, an_object) 122 TOOLBOX.assertIsInstance( 123 an_instance, an_object 124 ) 125 126 127def test_assertNotIsSubclass():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertIs_instance() 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.I add the staticmethod decorator to test_assertIs_instance
113 TOOLBOX.assertNotIsInstance( 114 unittest.TestCase, unittest.TestCase 115 ) 116 117 @staticmethod 118 def test_assertIs_instance():the test passes.
I move test_assertNotIsSubclass to make it a method of the TestUnittest class
123 TOOLBOX.assertIsInstance( 124 an_instance, an_object 125 ) 126 127 def test_assertNotIsSubclass(): 128 assert not issubclass( 129 unittest.TestCase, list 130 ) 131 TOOLBOX.assertNotIsSubclass( 132 unittest.TestCase, dict 133 ) 134 135 136def test_assertIs_subclass():the terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertNotIsSubclass() takes 0 positional arguments but 1 was givenI add the staticmethod decorator to test_assertNotIsSubclass
123 TOOLBOX.assertIsInstance( 124 an_instance, an_object 125 ) 126 127 @staticmethod 128 def test_assertNotIsSubclass():the test passes.
I move test_assertIs_subclass to make it a method of the TestUnittest class
132 TOOLBOX.assertNotIsSubclass( 133 unittest.TestCase, dict 134 ) 135 136 def test_assertIs_subclass(): 137 assert issubclass(unittest.TestCase, object) 138 TOOLBOX.assertIsSubclass( 139 unittest.TestCase, object 140 ) 141 142 143# Exceptions seenthe terminal is my friend, and shows TypeError
TypeError: TestUnittest.test_assertIs_subclass() 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.I add the staticmethod decorator to test_assertIs_subclass
132 TOOLBOX.assertNotIsSubclass( 133 unittest.TestCase, dict 134 ) 135 136 @staticmethod 137 def test_assertIs_subclass():the test passes.
I add the
TOOLBOXvariable to TestUnittest to make it an object attribute7# class Unittest(object): 8class TestUnittest(object): 9 10 TOOLBOX = unittest.TestCase() 11 12 @staticmethod 13 def test_dir_unittest():I use the new object attribute for
TOOLBOXin test_dir_unittest32 assert reality == my_expectation 33 # TOOLBOX.assertNotEqual(reality, my_expectation) 34 # TOOLBOX.assertEqual(reality, my_expectation) 35 self.TOOLBOX.assertEqual(reality, my_expectation) 36 37 @staticmethod 38 def test_dir_unittest_testcase():the terminal is my friend, and shows NameError
NameError: name 'self' is not definedI add
selfto the parentheses of test_dir_unittest7# class Unittest(object): 8class TestUnittest(object): 9 10 TOOLBOX = unittest.TestCase() 11 12 @staticmethod 13 # def test_dir_unittest(): 14 def test_dir_unittest(self):the terminal is my friend, and shows
E fixture 'self' not foundI remove the staticmethod decorator since I am using
selfin the test_dir_unittest7# class Unittest(object): 8class TestUnittest(object): 9 10 TOOLBOX = unittest.TestCase() 11 12 # @staticmethod 13 # def test_dir_unittest(): 14 def test_dir_unittest(self): 15 reality = dir(unittest)the test is green again.
I use the new object attribute for
TOOLBOXin test_dir_unittest_testcase88 assert reality == my_expectation 89 # TOOLBOX.assertNotEqual(reality, my_expectation) 90 # TOOLBOX.assertEqual(reality, my_expectation) 91 self.TOOLBOX.assertEqual(reality, my_expectation) 92 93 @staticmethod 94 def test_assertIsNot():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_dir_unittest_testcase36 self.TOOLBOX.assertEqual(reality, my_expectation) 37 38 # @staticmethod 39 # def test_dir_unittest_testcase(): 40 def test_dir_unittest_testcase(self): 41 reality = dir(unittest.TestCase)green again.
I use the object attribute for
TOOLBOXin test_assertIsNot94 @staticmethod 95 def test_assertIsNot(): 96 assert None is not False 97 # TOOLBOX.assertIsNot(None, False) 98 self.TOOLBOX.assertIsNot(None, False) 99 100 @staticmethod 101 def test_assertIs():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertIsNot94 # @staticmethod 95 # def test_assertIsNot(): 96 def test_assertIsNot(self):the test is green again.
I use the object attribute for
TOOLBOXin test_assertIs101 @staticmethod 102 def test_assertIs(): 103 assert False is False 104 # TOOLBOX.assertIs(False, False) 105 self.TOOLBOX.assertIs(False, False) 106 107 @staticmethod 108 def test_assertNotEqual():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertIs101 # @staticmethod 102 # def test_assertIs(): 103 def test_assertIs(self):green again.
I use the object attribute for
TOOLBOXin test_assertNotEqual108 @staticmethod 109 def test_assertNotEqual(): 110 assert True != 0 111 # TOOLBOX.assertNotEqual(True, 0) 112 self.TOOLBOX.assertNotEqual(True, 0) 113 114 @staticmethod 115 def test_assertEqual():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertNotEqual108 # @staticmethod 109 # def test_assertNotEqual(): 110 def test_assertNotEqual(self):the test is green again.
I use the object attribute for
TOOLBOXin test_assertEqual115 @staticmethod 116 def test_assertEqual(): 117 assert 0.0 == 0.0 118 # TOOLBOX.assertEqual(0.0, 0.0) 119 self.TOOLBOX.assertEqual(0.0, 0.0) 120 121 @staticmethod 122 def test_assertNotIsInstance():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertEqual115 # @staticmethod 116 # def test_assertEqual(): 117 def test_assertEqual(self):green again.
I use the object attribute for
TOOLBOXin test_assertNotIsInstance122 @staticmethod 123 def test_assertNotIsInstance(): 124 assert not isinstance( 125 unittest.TestCase, unittest.TestCase 126 ) 127 # TOOLBOX.assertNotIsInstance( 128 self.TOOLBOX.assertNotIsInstance( 129 unittest.TestCase, unittest.TestCase 130 ) 131 132 @staticmethod 133 def test_assertIs_instance():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertNotIsInstance122 # @staticmethod 123 # def test_assertNotIsInstance(): 124 def test_assertNotIsInstance(self):the test is green again.
I use the object attribute for
TOOLBOXin test_assertIs_instance133 @staticmethod 134 def test_assertIs_instance(): 135 an_object = unittest.TestCase 136 an_instance = an_object() 137 138 assert isinstance(an_instance, an_object) 139 # TOOLBOX.assertIsInstance( 140 self.TOOLBOX.assertIsInstance( 141 an_instance, an_object 142 ) 143 144 @staticmethod 145 def test_assertNotIsSubclass():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertIs_instance133 # @staticmethod 134 # def test_assertIs_instance(): 135 def test_assertIs_instance(self):the test is green again.
I use the object attribute for
TOOLBOXin test_assertNotIsSubclass145 @staticmethod 146 def test_assertNotIsSubclass(): 147 assert not issubclass( 148 unittest.TestCase, list 149 ) 150 # TOOLBOX.assertNotIsSubclass( 151 self.TOOLBOX.assertNotIsSubclass( 152 unittest.TestCase, dict 153 ) 154 155 @staticmethod 156 def test_assertIs_subclass():NameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertNotIsSubclass145 # @staticmethod 146 # def test_assertNotIsSubclass(): 147 def test_assertNotIsSubclass(self):the test is green again.
I use the object attribute for
TOOLBOXin test_assertIs_subclass156 @staticmethod 157 def test_assertIs_subclass(): 158 assert issubclass(unittest.TestCase, object) 159 # TOOLBOX.assertIsSubclass( 160 self.TOOLBOX.assertIsSubclass( 161 unittest.TestCase, object 162 ) 163 164 165# Exceptions seenNameError: name 'self' is not definedI add
selfto the parentheses and comment out the staticmethod decorator for test_assertIs_subclass156 # @staticmethod 157 # def test_assertIs_subclass(): 158 def test_assertIs_subclass(self):the test is green again.
I remove the
TOOLBOXvariable because it is no longer used1import unittest 2 3 4# class Unittest(object): 5class TestUnittest(object):I add a git commit message in the other terminal
git commit -am 'extract TestUnittest object'
use unittest.TestCase
I can use the unittest.TestCase class as a parent of the TestUnittest class which will allow me to use self to access its attributes and methods.
It also means I will not need the TOOLBOX object attribute which points to an instance of the unittest.TestCase class.
RED: make it fail
I go back to the terminal where the tests are running
I change the call to the assertEqual method in test_dir_unittest
30 assert reality == my_expectation 31 # TOOLBOX.assertNotEqual(reality, my_expectation) 32 # TOOLBOX.assertEqual(reality, my_expectation) 33 # self.TOOLBOX.assertEqual(reality, my_expectation) 34 self.assertEqual(reality, my_expectation) 35 36 # @staticmethod 37 # def test_dir_unittest_testcase(): 38 def test_dir_unittest_testcase(self):the terminal is my friend, and shows AttributeError
AttributeError: 'TestUnittest' object has no attribute 'assertEqual'I add AttributeError to the list of Exceptions seen
164# Exceptions seen 165# AssertionError 166# NameError 167# TypeError 168# AttributeError
GREEN: make it pass
I add unittest.TestCase as the parent of the TestUnittest class so that it can have all the attributes and methods of unittest.TestCase
4# class Unittest(object):
5# class TestUnittest(object):
6class TestUnittest(unittest.TestCase):
7
8 TOOLBOX = unittest.TestCase()
9
10 # @staticmethod
11 # def test_dir_unittest():
12 def test_dir_unittest(self):
13 reality = dir(unittest)
the test passes because
TestUnittest is now a subclass (child) of the unittest.TestCase class.
children inherit the attributes and methods of their parents.
selfis TestUnittest() inside TestUnittest.selfhas all the attributes and methods of unittest.TestCase because unittest.TestCase is the parent class.
class TestUnittest(unittest.TestCase):
└── def test_dir_unittest(self):
│ ...
└── self.assertEqual(reality, my_expectation)
└── TestUnittest().assertEqual(reality, my_expectation)
└── unittest.TestCase().assertEqual(
reality, my_expectation
)
└── unittest
└── class TestCase():
└── def assertEqual(self, first, second):
├── first = reality
├── second = my_expectation
...
REFACTOR: make it better
I remove the commented lines from test_dir_unittest and TestUnittest
1import unittest 2 3 4class TestUnittest(unittest.TestCase): 5 6 TOOLBOX = unittest.TestCase() 7 8 def test_dir_unittest(self): 9 reality = dir(unittest) 10 my_expectation = [26 ] 27 assert reality == my_expectation 28 self.assertEqual(reality, my_expectation) 29 30 # @staticmethod 31 # def test_dir_unittest_testcase(): 32 def test_dir_unittest_testcase(self):I add an assertion to test_assertIs_subclass to show that TestUnittest is a subclass of the unittest.TestCase class
148 # @staticmethod 149 # def test_assertIs_subclass(): 150 def test_assertIs_subclass(self): 151 assert issubclass(unittest.TestCase, object) 152 # TOOLBOX.assertIsSubclass( 153 self.TOOLBOX.assertIsSubclass( 154 unittest.TestCase, object 155 ) 156 self.assertNotIsSubclass( 157 TestUnittest, unittest.TestCase 158 ) 159 160 161# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: <class 'tests.test_unittest.TestUnittest'> is a subclass of <class 'unittest.case.TestCase'>I change the assertion to make it True
148 # @staticmethod 149 # def test_assertIs_subclass(): 150 def test_assertIs_subclass(self): 151 assert issubclass(unittest.TestCase, object) 152 # TOOLBOX.assertIsSubclass( 153 self.TOOLBOX.assertIsSubclass( 154 unittest.TestCase, object 155 ) 156 # self.assertNotIsSubclass( 157 self.assertIsSubclass( 158 TestUnittest, unittest.TestCase 159 ) 160 161 162# Exceptions seenthe test passes.
class TestUnittest(unittest.TestCase): └── def test_assertIs_subclass(self): │ ... └── self.assertIsSubclass(TestUnittest, unittest.TestCase) └── TestUnittest().assertIsSubclass( TestUnittest, unittest.TestCase ) └── unittest.TestCase().assertIsSubclass( TestUnittest, unittest.TestCase ) └── unittest └── class TestCase(): └── def assertIsSubclass( self, cls, superclass ): ├── cls = TestUnittest ├── superclass = unittest.TestCase ...I change
self.TOOLBOX.assertIsSubclasstoself.assertIsSubclass148 # @staticmethod 149 # def test_assertIs_subclass(): 150 def test_assertIs_subclass(self): 151 assert issubclass(unittest.TestCase, object) 152 # TOOLBOX.assertIsSubclass( 153 # self.TOOLBOX.assertIsSubclass( 154 self.assertIsSubclass( 155 unittest.TestCase, object 156 ) 157 # self.assertNotIsSubclass( 158 self.assertIsSubclass( 159 TestUnittest, unittest.TestCase 160 ) 161 162 163# Exceptions seenthe test is still green.
I remove the commented lines from test_assertIs_subclass
144 self.TOOLBOX.assertNotIsSubclass( 145 unittest.TestCase, dict 146 ) 147 148 def test_assertIs_subclass(self): 149 assert issubclass(unittest.TestCase, object) 150 self.assertIsSubclass( 151 unittest.TestCase, object 152 ) 153 self.assertIsSubclass( 154 TestUnittest, unittest.TestCase 155 ) 156 157 158# Exceptions seen 159# AssertionError 160# NameError 161# TypeError 162# AttributeErrorI change the call to the assertNotIsSubobject method in test_assertNotIsSubclass
137 # @staticmethod 138 # def test_assertNotIsSubclass(): 139 def test_assertNotIsSubclass(self): 140 assert not issubclass( 141 unittest.TestCase, list 142 ) 143 # TOOLBOX.assertNotIsSubclass( 144 # self.TOOLBOX.assertNotIsSubclass( 145 self.assertNotIsSubclass( 146 unittest.TestCase, dict 147 ) 148 149 def test_assertIs_subclass(self):still green.
class TestUnittest(unittest.TestCase): └── def test_assertNotIsSubclass(self): │ ... └── self.assertNotIsSubclass(unittest.TestCase, dict) └── TestUnittest().assertNotIsSubclass( unittest.TestCase, dict ) └── unittest.TestCase().assertNotIsSubclass( unittest.TestCase, dict ) └── unittest └── class TestCase(): └── def assertNotIsSubclass( self, cls, superclass ): ├── cls = unittest.TestCase ├── superclass = dict ...I remove the commented lines from test_assertNotIsSubclass
133 self.TOOLBOX.assertIsInstance( 134 an_instance, an_object 135 ) 136 137 def test_assertNotIsSubclass(self): 138 assert not issubclass( 139 unittest.TestCase, list 140 ) 141 self.assertNotIsSubclass( 142 unittest.TestCase, dict 143 ) 144 145 def test_assertIs_subclass(self):I add an assertion to test_assertIs_instance to show that
selfin the TestUnittest class is an instance of the unittest.TestCase class125 # @staticmethod 126 # def test_assertIs_instance(): 127 def test_assertIs_instance(self): 128 an_object = unittest.TestCase 129 an_instance = an_object() 130 131 assert isinstance(an_instance, an_object) 132 # TOOLBOX.assertIsInstance( 133 self.TOOLBOX.assertIsInstance( 134 an_instance, an_object 135 ) 136 self.assertNotIsInstance( 137 self, unittest.TestCase 138 ) 139 140 def test_assertNotIsSubclass(self):the terminal is my friend, and shows AssertionError
AssertionError: < tests.test_unittest.TestUnittest testMethod=test_assertIs_instance > is an instance of <class 'unittest.case.TestCase'>I change assertNotIsInstance to assertIsInstance
125 # @staticmethod 126 # def test_assertIs_instance(): 127 def test_assertIs_instance(self): 128 an_object = unittest.TestCase 129 an_instance = an_object() 130 131 assert isinstance(an_instance, an_object) 132 # TOOLBOX.assertIsInstance( 133 # self.TOOLBOX.assertIsInstance( 134 self.TOOLBOX.assertIsInstance( 135 an_instance, an_object 136 ) 137 # self.assertNotIsInstance( 138 self.assertIsInstance( 139 self, unittest.TestCase 140 ) 141 142 def test_assertNotIsSubclass(self):the test passes.
class TestUnittest(unittest.TestCase): └── def test_assertIs_instance(self): │ ... └── self.assertIsInstance(self, unittest.TestCase) └── TestUnittest().assertIsInstance( self, unittest.TestCase ) └── unittest.TestCase().assertIsInstance( self, unittest.TestCase ) └── unittest └── class TestCase(): └── def assertIsInstance( self, obj, cls ): ├── obj = self ├── cls = unittest.TestCase ...I change
self.TOOLBOX.assertIsInstancetoself.assertIsInstance125# @staticmethod 126 # def test_assertIs_instance(): 127 def test_assertIs_instance(self): 128 an_object = unittest.TestCase 129 an_instance = an_object() 130 131 assert isinstance(an_instance, an_object) 132 # TOOLBOX.assertIsInstance( 133 # self.TOOLBOX.assertIsInstance( 134 # self.TOOLBOX.assertIsInstance( 135 self.assertIsInstance( 136 an_instance, an_object 137 ) 138 # self.assertNotIsInstance( 139 self.assertIsInstance( 140 self, unittest.TestCase 141 ) 142 143 def test_assertNotIsSubclass(self):I remove the commented lines from test_assertIs_instance
121 self.TOOLBOX.assertNotIsInstance( 122 unittest.TestCase, unittest.TestCase 123 ) 124 125 def test_assertIs_instance(self): 126 an_object = unittest.TestCase 127 an_instance = an_object() 128 129 assert isinstance(an_instance, an_object) 130 self.assertIsInstance( 131 an_instance, an_object 132 ) 133 self.assertIsInstance( 134 self, unittest.TestCase 135 ) 136 137 def test_assertNotIsSubclass(self):I change the call to the assertNotIsInstance method in test_assertNotIsInstance
114 # @staticmethod 115 # def test_assertNotIsInstance(): 116 def test_assertNotIsInstance(self): 117 assert not isinstance( 118 unittest.TestCase, unittest.TestCase 119 ) 120 # TOOLBOX.assertNotIsInstance( 121 # self.TOOLBOX.assertNotIsInstance( 122 self.assertNotIsInstance( 123 unittest.TestCase, unittest.TestCase 124 ) 125 126 def test_assertIs_instance(self):the test is still green.
class TestUnittest(unittest.TestCase): └── def test_assertNotIsInstance(self): │ ... └── self.assertNotIsInstance( unittest.TestCase, unittest.TestCase ) └── TestUnittest().assertIsInstance( unittest.TestCase, unittest.TestCase ) └── unittest.TestCase().assertIsInstance( unittest.TestCase, unittest.TestCase ) └── unittest └── class TestCase(): └── def assertNotIsInstance( self, obj, cls ): ├── obj = unittest.TestCase ├── cls = unittest.TestCase ...I remove the commented lines from test_assertNotIsInstance
112 self.TOOLBOX.assertEqual(0.0, 0.0) 113 114 def test_assertNotIsInstance(self): 115 assert not isinstance( 116 unittest.TestCase, unittest.TestCase 117 ) 118 self.assertNotIsInstance( 119 unittest.TestCase, unittest.TestCase 120 ) 121 122 def test_assertIs_instance(self):I change the call to assertEqual in test_assertEqual
107 # @staticmethod 108 # def test_assertEqual(): 109 def test_assertEqual(self): 110 assert 0.0 == 0.0 111 # TOOLBOX.assertEqual(0.0, 0.0) 112 # self.TOOLBOX.assertEqual(0.0, 0.0) 113 self.assertEqual(0.0, 0.0) 114 115 def test_assertNotIsInstance(self):still green.
class TestUnittest(unittest.TestCase): └── def test_assertEqual(self): │ ... └── self.assertEqual(0.0, 0.0) └── TestUnittest().assertEqual(0.0, 0.0) └── unittest.TestCase().assertEqual(0.0, 0.0) └── unittest └── class TestCase(): └── def assertEqual( self, first, second ): ├── first = 0.0 ├── second = 0.0 ...I remove the commented lines from test_assertEqual
105 self.TOOLBOX.assertNotEqual(True, 0) 106 107 def test_assertEqual(self): 108 assert 0.0 == 0.0 109 self.assertEqual(0.0, 0.0) 110 111 def test_assertNotIsInstance(self):I change the call to the assertNotEqual method in test_assertNotEqual
100 # @staticmethod 101 # def test_assertNotEqual(): 102 def test_assertNotEqual(self): 103 assert True != 0 104 # TOOLBOX.assertNotEqual(True, 0) 105 # self.TOOLBOX.assertNotEqual(True, 0) 106 self.assertNotEqual(True, 0) 107 108 def test_assertEqual(self):still green.
class TestUnittest(unittest.TestCase): └── def test_assertNotEqual(self): │ ... └── self.assertNotEqual(True, 0) └── TestUnittest().assertNotEqual(True, 0) └── unittest.TestCase().assertNotEqual(True, 0) └── unittest └── class TestCase(): └── def assertNotEqual( self, first, second ): ├── first = True ├── second = 0 ...I remove the commented lines from test_assertNotEqual
98 self.TOOLBOX.assertIs(False, False) 99 100 def test_assertNotEqual(self): 101 assert True != 0 102 self.assertNotEqual(True, 0) 103 104 def test_assertEqual(self):I change the call to the assertIs method in test_assertIs
93 # @staticmethod 94 # def test_assertIs(): 95 def test_assertIs(self): 96 assert False is False 97 # TOOLBOX.assertIs(False, False) 98 # self.TOOLBOX.assertIs(False, False) 99 self.assertIs(False, False) 100 101 def test_assertNotEqual(self):green.
class TestUnittest(unittest.TestCase): └── def test_assertIs(self): │ ... └── self.assertIs(False, False) └── TestUnittest().assertIs(False, False) └── unittest.TestCase().assertIs(False, False) └── unittest └── class TestCase(): └── def assertIs( self, expr1, expr2 ): ├── expr1 = False ├── expr2 = False ...I remove the commented lines from test_assertIs
91 self.TOOLBOX.assertIsNot(None, False) 92 93 def test_assertIs(self): 94 assert False is False 95 self.assertIs(False, False) 96 97 def test_assertNotEqual(self):I change the call to the assertIsNot method in test_assertIsNot
86 # @staticmethod 87 # def test_assertIsNot(): 88 def test_assertIsNot(self): 89 assert None is not False 90 # TOOLBOX.assertIsNot(None, False) 91 # self.TOOLBOX.assertIsNot(None, False) 92 self.assertIsNot(None, False) 93 94 def test_assertIs(self):the test is still green.
class TestUnittest(unittest.TestCase): └── def test_assertIsNot(self): │ ... └── self.assertIsNot(None, False) └── TestUnittest().assertIsNot(None, False) └── unittest.TestCase().assertIsNot(None, False) └── unittest └── class TestCase(): └── def assertIsNot( self, expr1, expr2 ): ├── expr1 = None ├── expr2 = False ...I remove the commented lines from test_assertIsNot
84 self.TOOLBOX.assertEqual(reality, my_expectation) 85 86 def test_assertIsNot(self): 87 assert None is not False 88 self.assertIsNot(None, False) 89 90 def test_assertIs(self):I change the call to the assertEqual method in test_dir_unittest_testcase
81 assert reality == my_expectation 82 # TOOLBOX.assertNotEqual(reality, my_expectation) 83 # TOOLBOX.assertEqual(reality, my_expectation) 84 # self.TOOLBOX.assertEqual(reality, my_expectation) 85 self.assertEqual(reality, my_expectation) 86 87 def test_assertIsNot(self):the test is still green.
I remove the commented lines from test_dir_unittest_testcase
27 assert reality == my_expectation 28 self.assertEqual(reality, my_expectation) 29 30 def test_dir_unittest_testcase(self): 31 reality = dir(unittest.TestCase) 32 my_expectation = [80 ] 81 assert reality == my_expectation 82 self.assertEqual(reality, my_expectation) 83 84 def test_assertIsNot(self):I remove the
TOOLBOXobject attribute since it is no longer used4class TestUnittest(unittest.TestCase): 5 6 def test_dir_unittest(self):all the tests are still passing.
I add a git commit message in the other terminal
git commit -am 'use unittest.TestCase'
close the project
I close
test_unittest.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
unittestcd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I can write tests with the unittest.TestCase class which comes with assert methods I can use in place of basic assert statements:
code from the chapter
what is next?
Would you like to test the other projects with unittest?
Would you like to test the assertion_error project with unittest?
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.