what is None?
None is used when there is no value. In Mathematics we use 0 to represent no quantity. In some languages or domains we use NULL, in forms we use N/A when the options do not apply. In Python we can use None, it is the simplest object.
I used assertIs and assertIsNot in test_assertion_error_w_none in the assertion_error project, where I saw that
I want to use assert methods to compare None with the other Python basic objects to see what it is and what it is not.
preview
I have these tests by the end of the chapter
1import unittest
2
3
4class TestNone(unittest.TestCase):
5
6 def test_what_is_none(self):
7 self.assertIs(None, None)
8 self.assertIsNone(None)
9
10 def test_is_none_a_boolean(self):
11 self.assertIsNotNone(False)
12 self.assertIsNotNone(True)
13 self.assertIsInstance(False, bool)
14 self.assertIsInstance(True, bool)
15 self.assertNotIsInstance(None, bool)
16
17 def test_is_none_an_integer(self):
18 self.assertIsNotNone(-1)
19 self.assertIsNotNone(0)
20 self.assertIsNotNone(1)
21 self.assertIsInstance(-1, int)
22 self.assertIsInstance(0, int)
23 self.assertIsInstance(1, int)
24 self.assertNotIsInstance(None, int)
25
26 def test_is_none_a_float(self):
27 self.assertIsNotNone(-0.1)
28 self.assertIsNotNone(0.0)
29 self.assertIsNotNone(0.1)
30 self.assertIsInstance(-0.1, float)
31 self.assertIsInstance(0.0, float)
32 self.assertIsInstance(0.1, float)
33 self.assertNotIsInstance(None, float)
34
35 def test_is_none_a_string(self):
36 self.assertIsNotNone('')
37 self.assertIsNotNone("characters")
38 self.assertIsInstance("", str)
39 self.assertIsInstance('characters', str)
40 self.assertNotIsInstance(None, str)
41
42 def test_is_none_a_tuple(self):
43 self.assertIsNotNone(())
44 self.assertIsNotNone((0, 1, 2, 'n'))
45 self.assertIsInstance((), tuple)
46 self.assertIsInstance((0, 1, 2, 'n'), tuple)
47 self.assertNotIsInstance(None, tuple)
48
49 def test_is_none_a_list(self):
50 self.assertIsNotNone([])
51 self.assertIsNotNone([0, 1, 2, 'n'])
52 self.assertIsInstance([], list)
53 self.assertIsInstance([0, 1, 2, 'n'], list)
54 self.assertNotIsInstance(None, list)
55
56 def test_is_none_a_set(self):
57 self.assertIsNotNone(set())
58 self.assertIsNotNone({0, 1, 2, 'n'})
59 self.assertIsInstance({0, 1, 2, 'n'}, set)
60 self.assertNotIsInstance(None, set)
61
62 def test_is_none_a_dictionary(self):
63 self.assertIsNotNone(dict())
64 self.assertIsNotNone({'key': 'value'})
65 self.assertIsInstance({}, dict)
66 self.assertIsInstance(
67 {'key': 'value'}, dict
68 )
69 self.assertNotIsInstance(None, dict)
70
71
72# NOTES
73# None is NOT a dictionary
74# None is NOT a set
75# None is NOT a list
76# None is NOT a tuple
77# None is NOT a string
78# None is NOT a float
79# None is NOT an integer
80# None is NOT a boolean
81# None is None
82
83
84# Exceptions seen
85# AssertionError
questions about None
Questions to think about as I go through the chapter
start the project
I name this project
noneI open a terminal
I change directory to the
nonefolder in thepumping_pythonfoldercd nonethe terminal shows
cd: no such file or directory: noneI use uv to make a directory for the project and initialize it
uv init nonethe terminal shows
Initialized project `none` at `.../pumping_python/none`I change directory to
nonecd noneI 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 use the mv program to change the name of
main.pytotest_none.pyand move it to thetestsfoldermv main.py tests/test_none.pyMove-Item main.py tests/test_none.pyI open
test_none.pyI delete the text in the file then add the first failing test to
test_none.py1import unittest 2 3 4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertEqual(False, True)I go back to the terminal to make a requirements file for the Python packages I need
echo "pytest" > requirements.txtI add pytest-watcher to the requirements file
echo "pytest-watcher" >> requirements.txtI use uv to install pytest-watcher with the requirements file
uv add --requirement requirements.txtthe terminal shows that it installed pytest-watcher and its dependencies.
I 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-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal is my friend, and shows AssertionError
======================== FAILURES ======================== _________________ TestNone.test_failure __________________ self = <tests.test_none.TestNone testMethod=test_failure> def test_failure(self): > self.assertEqual(False, True) E AssertionError: False != True tests/test_none.py:7: AssertionError ================ short test summary info ================= FAILED tests/test_none.py::TestNone::test_failure - AssertionError: False != True =================== 1 failed in X.YZs ====================because True is NOT equal to False.
if the terminal does not show the same error, then check
if your
tests/__init__.pyhas two underscores (__) before and afterinitfor__init__.pynot_init_.pyif you ran
echo "pytest-watcher" >> requirements.txt, to addpytest-watcherto the requirements file
and try
uv run pytest-watcher . --nowagainI add AssertionError to the list of Exceptions seen in
test_none.py4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertEqual(False, True) 8 9 10# Exceptions seen 11# AssertionErrorI change True to False in the assertion
4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 # self.assertEqual(False, True) 8 self.assertEqual(False, False) 9 10 11# Exceptions seen 12# AssertionErrorthe test passes.
test_what_is_none
RED: make it fail
I change test_failure to test_what_is_none with a call to the assertIsNot method
4class TestNone(unittest.TestCase): 5 6 def test_what_is_none(self): 7 self.assertIsNot(None, None) 8 9 10# Exceptions seenthe terminal is my friend, and shows AssertionError
AssertionError: unexpectedly identical: None
GREEN: make it pass
I change assertIsNot to assertIs
6 def test_what_is_none(self):
7 # self.assertIsNot(None, None)
8 self.assertIs(None, None)
9
10
11# Exceptions seen
the test passes. So far this is the same as test_assertion_error_w_none.
another way to test if something is NOT None
I can also use another assert method from the unittest.TestCase class to test if something is NOT the same object as None - the assertIsNotNone method
RED: make it fail
I add an assertion with the assertIsNotNone method
6 def test_what_is_none(self):
7 # self.assertIsNot(None, None)
8 self.assertIs(None, None)
9 self.assertIsNotNone(None)
10
11
12# Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError: unexpectedly None
because assertIsNotNone raises AssertionError if the object given in parentheses is None.
assertIsNot(something, None)andassertIsNotNone(something)are the same.
another way to test if something is None
I can use the assertIsNone method from the unittest.TestCase class to test if something is the same object as None
GREEN: make it pass
I change assertIsNotNone to the assertIsNone method
6 def test_what_is_none(self):
7 # self.assertIsNot(None, None)
8 self.assertIs(None, None)
9 # self.assertIsNotNone(None)
10 self.assertIsNone(None)
11
12
13# Exceptions seen
the test passes because None is None and assertIsNone raises AssertionError if the object given in parentheses is NOT None.
assertIs(something, None)andassertIsNone(something)are the same.
REFACTOR: make it better
I remove the commented lines
4class TestNone(unittest.TestCase): 5 6 def test_what_is_none(self): 7 self.assertIs(None, None) 8 self.assertIsNone(None) 9 10 11# Exceptions seenI add comments
4class TestNone(unittest.TestCase): 5 6 def test_what_is_none(self): 7 self.assertIs(None, None) 8 self.assertIsNone(None) 9 10 11# NOTES 12# None is None 13 14 15# Exceptions seen 16# AssertionErrorthis is the same comment from the assertion_error project.
I open a new terminal then change directories to
nonecd noneI add a git commit message in the new terminal
git commit -am 'add test_what_is_none'
I know two new assert methods
test_is_none_a_boolean
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a boolean
6 def test_what_is_none(self): 7 self.assertIs(None, None) 8 self.assertIsNone(None) 9 10 def test_is_none_a_boolean(self): 11 self.assertIsNone(False) 12 13 14# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: False is not None
GREEN: make it pass
I change assertIsNone to the assertIsNotNone method
10 def test_is_none_a_boolean(self):
11 # self.assertIsNone(False)
12 self.assertIsNotNone(False)
13
14
15# NOTES
the test passes.
REFACTOR: make it better
I add a comment
15# NOTES 16# False is NOT None 17# None is None 18 19 20# Exceptions seen 21# AssertionErroranother comment from the assertion_error project.
I add an assertion for the other boolean
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 self.assertIsNone(True) 14 15 16# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is not NoneI change assertIsNone to assertIsNotNone
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 # self.assertIsNone(True) 14 self.assertIsNotNone(True) 15 16 17# NOTESthe test passes.
I add a comment
17# NOTES 18# True is NOT None 19# False is NOT None 20# None is Nonealso from the assertion_error project
I add a call to the assertNotIsInstance method to test if False is an instance of the bool class
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 # self.assertIsNone(True) 14 self.assertIsNotNone(True) 15 self.assertNotIsInstance(False, bool) 16 17 18# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: False is an instance of <class 'bool'>I change assertNotIsInstance to the assertIsInstance method
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 # self.assertIsNone(True) 14 self.assertIsNotNone(True) 15 # self.assertNotIsInstance(False, bool) 16 self.assertIsInstance(False, bool) 17 18 19# NOTESthe test passes.
I add a failing line for the other boolean with assertNotIsInstance
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 # self.assertIsNone(True) 14 self.assertIsNotNone(True) 15 # self.assertNotIsInstance(False, bool) 16 self.assertIsInstance(False, bool) 17 self.assertNotIsInstance(True, bool) 18 19 20# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: True is an instance of <class 'bool'>I make the statement True with assertIsInstance
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 # self.assertIsNone(True) 14 self.assertIsNotNone(True) 15 # self.assertNotIsInstance(False, bool) 16 self.assertIsInstance(False, bool) 17 # self.assertNotIsInstance(True, bool) 18 self.assertIsInstance(True, bool) 19 20 21# NOTESthe test passes.
I add assertIsInstance to test if None is an instance (a copy) of the bool class
9 def test_is_none_a_boolean(self): 10 # self.assertIsNone(False) 11 self.assertIsNotNone(False) 12 # self.assertIsNone(True) 13 self.assertIsNotNone(True) 14 # self.assertNotIsInstance(False, bool) 15 self.assertIsInstance(False, bool) 16 # self.assertNotIsInstance(True, bool) 17 self.assertIsInstance(True, bool) 18 self.assertIsInstance(None, bool) 19 20 21# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'bool'>because None is NOT a boolean.
I make the line True with assertNotIsInstance
10 def test_is_none_a_boolean(self): 11 # self.assertIsNone(False) 12 self.assertIsNotNone(False) 13 # self.assertIsNone(True) 14 self.assertIsNotNone(True) 15 # self.assertNotIsInstance(False, bool) 16 self.assertIsInstance(False, bool) 17 # self.assertNotIsInstance(True, bool) 18 self.assertIsInstance(True, bool) 19 # self.assertIsInstance(None, bool) 20 self.assertNotIsInstance(None, bool) 21 22 23# NOTESthe test passes.
I remove the commented lines
10 def test_is_none_a_boolean(self): 11 self.assertIsNotNone(False) 12 self.assertIsNotNone(True) 13 self.assertIsInstance(False, bool) 14 self.assertIsInstance(True, bool) 15 self.assertNotIsInstance(None, bool) 16 17 18# NOTESI change the last 2 comments I added (
True is NOT NoneandFalse is NOT None), since this is about None18# NOTES 19# None is NOT a boolean 20# None is None 21 22 23# Exceptions seen 24# AssertionErrorOkay, this is new, not something from the assertion_error project.
I add a git commit message in the other terminal
git commit -am 'add test_is_none_a_boolean'
The names of the assert methods can be confusing,
there is
assertIsNotNonewhereNotcomes afterIsthere is
assertNotIsInstancewhereIscomes afterNot
Would assertIsNotInstance be better than assertNotIsInstance, since assertNotIsNone does not sound better than assertIsNotNone?
Not in this case, because the names of the assert methods match the assert statements they replace
assertIsNotNone(x)forassert x is not NoneassertNotIsInstance(x, y)forassert not isinstance(x, y)
test_is_none_an_integer
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is an integer (a whole number without decimals)
15 self.assertNotIsInstance(None, bool) 16 17 def test_is_none_an_integer(self): 18 self.assertIsNone(-1) 19 20 21# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: -1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
17 def test_is_none_an_integer(self):
18 # self.assertIsNone(-1)
19 self.assertIsNotNone(-1)
20
21
22# NOTES
the test passes.
REFACTOR: make it better
I add a new failing line with assertIsNotNone
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 self.assertIsNone(0) 21 22 23# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0 is not NoneI make the statement True with assertIsNotNone
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 23 24# NOTESthe test passes.
I add an assertion for
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 self.assertIsNone(1) 23 24 25# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 1 is not NoneI change the line to make it True
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 25 26# NOTESthe test passes.
I add a new failing line with assertNotIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 self.assertNotIsInstance(-1, int) 25 26 27# NOTESI make the line True with assertIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 27 28# NOTESthe test passes.
I add a failing assertion with assertNotIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 self.assertNotIsInstance(0, int) 27 28 29# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0 is an instance of <class 'int'>because
0is an integer.I change the statement to make it True
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 # self.assertNotIsInstance(0, int) 27 self.assertIsInstance(0, int) 28 29 30# NOTESthe test passes.
I add a failing assertion with assertNotIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 # self.assertNotIsInstance(0, int) 27 self.assertIsInstance(0, int) 28 self.assertNotIsInstance(1, int) 29 30 31# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 1 is an instance of <class 'int'>I make the failing line True with assertIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 # self.assertNotIsInstance(0, int) 27 self.assertIsInstance(0, int) 28 # self.assertNotIsInstance(1, int) 29 self.assertIsInstance(1, int) 30 31 32# NOTESthe test passes.
I add one more failing line to test if None is an integer with assertIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 # self.assertNotIsInstance(0, int) 27 self.assertIsInstance(0, int) 28 # self.assertNotIsInstance(1, int) 29 self.assertIsInstance(1, int) 30 self.assertIsInstance(None, int) 31 32 33# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'int'>because None is NOT an integer.
I make the line True with assertNotIsInstance
17 def test_is_none_an_integer(self): 18 # self.assertIsNone(-1) 19 self.assertIsNotNone(-1) 20 # self.assertIsNone(0) 21 self.assertIsNotNone(0) 22 # self.assertIsNone(1) 23 self.assertIsNotNone(1) 24 # self.assertNotIsInstance(-1, int) 25 self.assertIsInstance(-1, int) 26 # self.assertNotIsInstance(0, int) 27 self.assertIsInstance(0, int) 28 # self.assertNotIsInstance(1, int) 29 self.assertIsInstance(1, int) 30 # self.assertIsInstance(None, int) 31 self.assertNotIsInstance(None, int) 32 33 34# NOTESthe test passes.
I remove the commented lines
17 def test_is_none_an_integer(self): 18 self.assertIsNotNone(-1) 19 self.assertIsNotNone(0) 20 self.assertIsNotNone(1) 21 self.assertIsInstance(-1, int) 22 self.assertIsInstance(0, int) 23 self.assertIsInstance(1, int) 24 self.assertNotIsInstance(None, int) 25 26 27# NOTESI add a new comment
27# NOTES 28# None is NOT an integer 29# None is NOT a boolean 30# None is None 31 32 33# Exceptions seen 34# AssertionErrorI add a git commit message in the other terminal
git commit -am 'add test_is_none_an_integer'
test_is_none_a_float
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a float (binary floating point decimal number)
24 self.assertNotIsInstance(None, int)
25
26 def test_is_none_a_float(self):
27 self.assertIsNone(-0.1)
28
29
30# NOTES
the terminal is my friend, and shows AssertionError
AssertionError: -0.1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
26 def test_is_none_a_float(self):
27 # self.assertIsNone(-0.1)
28 self.assertIsNotNone(-0.1)
29
30
31# NOTES
the test passes.
REFACTOR: make it better
I add a call to assertIsNone for
0.026 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 self.assertIsNone(0.0) 30 31 32# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0.0 is not NoneI make the statement True with assertIsNotNone
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 32 33# NOTESthe test passes.
I add a call to assertIsNone for
0.126 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 self.assertIsNone(0.1) 32 33 34# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0.1 is not NoneI make the statement True with assertIsNotNone
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 34 35# NOTESthe test passes. Time for instance tests.
I add a failing line with assertNotIsInstance for
-0.126 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 self.assertNotIsInstance(-0.1, float) 34 35 36# NOTESfloat is the class for binary floating point numbers. the terminal is my friend, and shows AssertionError
AssertionError: -0.1 is an instance of <class 'float'>because
-0.1is a float.I use
-0.1for all the binary floating point numbers that are smaller than0.0.
I make the statement True with assertIsInstance
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 36 37# NOTESthe test passes.
I add the next instance test with assertNotIsInstance for
0.026 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 self.assertNotIsInstance(0.0, float) 36 37 38# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0.0 is an instance of <class 'float'>because
0.0is a binary floating point number.I make the statement True with assertIsInstance
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 # self.assertNotIsInstance(0.0, float) 36 self.assertIsInstance(0.0, float) 37 38 39# NOTESthe test passes.
I add a failing line with assertNotIsInstance for
0.126 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 # self.assertNotIsInstance(0.0, float) 36 self.assertIsInstance(0.0, float) 37 self.assertNotIsInstance(0.1, float) 38 39 40# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0.1 is an instance of <class 'float'>because
0.1is a float.I use
0.1for all the binary floating point numbers that are bigger than0.0.
I make the statement True with assertIsInstance
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 # self.assertNotIsInstance(0.0, float) 36 self.assertIsInstance(0.0, float) 37 # self.assertNotIsInstance(0.1, float) 38 self.assertIsInstance(0.1, float) 39 40 41# NOTESthe test passes.
I add one more failing line with the assertIsInstance method
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 # self.assertNotIsInstance(0.0, float) 36 self.assertIsInstance(0.0, float) 37 # self.assertNotIsInstance(0.1, float) 38 self.assertIsInstance(0.1, float) 39 self.assertIsInstance(None, float) 40 41 42# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'float'>because None is NOT a float.
I make the statement True with the assertNotIsInstance method
26 def test_is_none_a_float(self): 27 # self.assertIsNone(-0.1) 28 self.assertIsNotNone(-0.1) 29 # self.assertIsNone(0.0) 30 self.assertIsNotNone(0.0) 31 # self.assertIsNone(0.1) 32 self.assertIsNotNone(0.1) 33 # self.assertNotIsInstance(-0.1, float) 34 self.assertIsInstance(-0.1, float) 35 # self.assertNotIsInstance(0.0, float) 36 self.assertIsInstance(0.0, float) 37 # self.assertNotIsInstance(0.1, float) 38 self.assertIsInstance(0.1, float) 39 # self.assertIsInstance(None, float) 40 self.assertNotIsInstance(None, float) 41 42 43# NOTESthe test passes.
I remove the commented lines
26 def test_is_none_a_float(self): 27 self.assertIsNotNone(-0.1) 28 self.assertIsNotNone(0.0) 29 self.assertIsNotNone(0.1) 30 self.assertIsInstance(-0.1, float) 31 self.assertIsInstance(0.0, float) 32 self.assertIsInstance(0.1, float) 33 self.assertNotIsInstance(None, float) 34 35 36# NOTESI add a new comment
36# NOTES 37# None is NOT a float 38# None is NOT an integer 39# None is NOT a boolean 40# None is None 41 42 43# Exceptions seen 44# AssertionErrorI add a git commit message in the other terminal
git commit -am 'add test_is_none_a_float'
test_is_none_a_string
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a string (anything inside quotes)
33 self.assertNotIsInstance(None, float) 34 35 def test_is_none_a_string(self): 36 self.assertIsNone('') 37 38 39# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: '' is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
35 def test_is_none_a_string(self):
36 # self.assertIsNone('')
37 self.assertIsNotNone('')
38
39
40# NOTES
the test passes.
REFACTOR: make it better
I add a call to assertIsNone for a string that is not empty
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 self.assertIsNone("characters")the terminal is my friend, and shows AssertionError
AssertionError: 'characters' is not NoneI change the assert method to make the statement True
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 41 42# NOTESthe test passes.
I add a failing line with assertNotIsInstance for the empty string (
"")35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 self.assertNotIsInstance("", str) 41 42 43# NOTESI change the assert method
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 # self.assertNotIsInstance("", str) 41 self.assertIsInstance("", str) 42 43 44# NOTESthe test passes.
I add an assertion for
'characters'35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 # self.assertNotIsInstance("", str) 41 self.assertIsInstance("", str) 42 self.assertNotIsInstance('characters', str) 43 44 45# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 'characters' is an instance of <class 'str'>I change the assert method
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 # self.assertNotIsInstance("", str) 41 self.assertIsInstance("", str) 42 # self.assertNotIsInstance('characters', str) 43 self.assertIsInstance('characters', str) 44 45 46# NOTESthe test passes.
I add a failing assertion with assertIsInstance
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 # self.assertNotIsInstance("", str) 41 self.assertIsInstance("", str) 42 # self.assertNotIsInstance('characters', str) 43 self.assertIsInstance('characters', str) 44 self.assertIsInstance(None, str) 45 46 47# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'str'>because None is NOT a string.
I change the assert method
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("characters") 39 self.assertIsNotNone("characters") 40 # self.assertNotIsInstance("", str) 41 self.assertIsInstance("", str) 42 # self.assertNotIsInstance('characters', str) 43 self.assertIsInstance('characters', str) 44 # self.assertIsInstance(None, str) 45 self.assertNotIsInstance(None, str) 46 47 48# NOTESI remove the commented lines
35 def test_is_none_a_string(self): 36 self.assertIsNotNone('') 37 self.assertIsNotNone("characters") 38 self.assertIsInstance("", str) 39 self.assertIsInstance('characters', str) 40 self.assertNotIsInstance(None, str) 41 42 43# NOTESI add a comment
43# NOTES 44# None is NOT a string 45# None is NOT a float 46# None is NOT an integer 47# None is NOT a boolean 48# None is None 49 50 51# Exceptions seen 52# AssertionErrorI add a git commit message in the other terminal
git commit -am 'add test_is_none_a_string'
test_is_none_a_tuple
RED: make it fail
I go back to the terminal where the tests are running
I add a test to see if None is a tuple (anything in parentheses (
( )) separated by commas)40 self.assertNotIsInstance(None, str) 41 42 def test_is_none_a_tuple(self): 43 self.assertIsNone(()) 44 45 46# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: () is not None
GREEN: make it pass
I make the statement True
42 def test_is_none_a_tuple(self):
43 # self.assertIsNone(())
44 self.assertIsNotNone(())
45
46
47# NOTES
the test passes.
REFACTOR: make it better
I add a failing assertion for
(0, 1, 2, 'n')42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 self.assertIsNone((0, 1, 2, 'n')) 46 47 48# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: (0, 1, 2, 'n') is not NoneI make the statement True
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 48 49# NOTESthe test passes.
I add a failing line with assertNotIsInstance for the empty tuple
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 self.assertNotIsInstance((), tuple) 48 49 50# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: () is an instance of <class 'tuple'>I make the statement True
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 50 51# NOTESthe test passes.
I add a failing assertion for
(0, 1, 2, 'n')42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 self.assertNotIsInstance((0, 1, 2, 'n'), tuple) 50 51 52# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: (0, 1, 2, 'n') is an instance of <class 'tuple'>because in Python anything in parentheses (
( )) separated by commas is a tuple.I change the assert method to make the statement True
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 # self.assertNotIsInstance((0, 1, 2, 'n'), tuple) 50 self.assertIsInstance((0, 1, 2, 'n'), tuple) 51 52 53# NOTESthe test passes.
I add one more instance test
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 # self.assertNotIsInstance((0, 1, 2, 'n'), tuple) 50 self.assertIsInstance((0, 1, 2, 'n'), tuple) 51 self.assertIsInstance(None, tuple) 52 53 54# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'tuple'>because None is NOT a tuple.
I change the statement to make it True
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((0, 1, 2, 'n')) 46 self.assertIsNotNone((0, 1, 2, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 # self.assertNotIsInstance((0, 1, 2, 'n'), tuple) 50 self.assertIsInstance((0, 1, 2, 'n'), tuple) 51 # self.assertIsInstance(None, tuple) 52 self.assertNotIsInstance(None, tuple) 53 54 55# NOTESthe test passes.
I remove the commented lines
42 def test_is_none_a_tuple(self): 43 self.assertIsNotNone(()) 44 self.assertIsNotNone((0, 1, 2, 'n')) 45 self.assertIsInstance((), tuple) 46 self.assertIsInstance((0, 1, 2, 'n'), tuple) 47 self.assertNotIsInstance(None, tuple) 48 49 50# NOTESI add a comment
50# NOTES 51# None is NOT a tuple 52# None is NOT a string 53# None is NOT a float 54# None is NOT an integer 55# None is NOT a boolean 56# None is None 57 58 59# Exceptions seen 60# AssertionErrorit looks like None is None and not anything else.
I add a git commit message in the other terminal
git commit -am 'add test_is_none_a_tuple'
test_is_none_a_list
RED: make it fail
I go back to the terminal where the tests are running
I add a new test to see if None is a list (anything in square brackets (
[ ]))47 self.assertNotIsInstance(None, tuple) 48 49 def test_is_none_a_list(self): 50 self.assertIsNone([]) 51 52 53# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: [] is not None
GREEN: make it pass
I change the assertion to make the statement True
49 def test_is_none_a_list(self):
50 # self.assertIsNone([])
51 self.assertIsNotNone([])
52
53
54# NOTES
the test passes.
REFACTOR: make it better
I add a failing assertion for
[0, 1, 2, 'n']49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 self.assertIsNone([0, 1, 2, 'n'])the terminal is my friend, and shows AssertionError
AssertionError: [0, 1, 2, 'n'] is not NoneI make the statement True
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 55 56# NOTESthe test passes.
I add a failing instance test for the empty list
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 self.assertNotIsInstance([], list) 55 56 57# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: [] is an instance of <class 'list'>because in Python anything in square brackets (
[ ]) is a list.I make the statement True
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 57 58# NOTESthe test passes.
I add a failing assertion with assertNotIsInstance
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 self.assertNotIsInstance([0, 1, 2, 'n'], list) 57 58 59# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: [0, 1, 2, 'n'] is an instance of <class 'list'>because in Python anything in square brackets (
[ ]) is a list.I change the assert method
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 # self.assertNotIsInstance([0, 1, 2, 'n'], list) 57 self.assertIsInstance([0, 1, 2, 'n'], list) 58 59 60# NOTESthe test passes.
I add one more failing line with the assertIsInstance method
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 # self.assertNotIsInstance([0, 1, 2, 'n'], list) 57 self.assertIsInstance([0, 1, 2, 'n'], list) 58 self.assertIsInstance(None, list) 59 60 61# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'list'>because None is NOT a list.
I make the statement True
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([0, 1, 2, 'n']) 53 self.assertIsNotNone([0, 1, 2, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 # self.assertNotIsInstance([0, 1, 2, 'n'], list) 57 self.assertIsInstance([0, 1, 2, 'n'], list) 58 # self.assertIsInstance(None, list) 59 self.assertNotIsInstance(None, list) 60 61 62# NOTESthe test passes.
I remove the commented lines
49 def test_is_none_a_list(self): 50 self.assertIsNotNone([]) 51 self.assertIsNotNone([0, 1, 2, 'n']) 52 self.assertIsInstance([], list) 53 self.assertIsInstance([0, 1, 2, 'n'], list) 54 self.assertNotIsInstance(None, list) 55 56 57# NOTESI add a new comment
57# NOTES 58# None is NOT a list 59# None is NOT a tuple 60# None is NOT a string 61# None is NOT a float 62# None is NOT an integer 63# None is NOT a boolean 64# None is None 65 66 67# Exceptions seen 68# AssertionErrorI add a git commit message in the other terminal
git commit -am 'add test_is_none_a_list'
test_is_none_a_set
RED: make it fail
I go back to the terminal where the tests are running
I want to test if None is a set (anything in curly braces
{ }, not key-value pairs)54 self.assertNotIsInstance(None, list) 55 56 def test_is_none_a_set(self): 57 self.assertIsNone(set()) 58 59 60# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: set() is not None
GREEN: make it pass
I make the statement True
56 def test_is_none_a_set(self):
57 # self.assertIsNone(set())
58 self.assertIsNotNone(set())
59
60
61# NOTES
the test passes.
REFACTOR: make it better
I add an assertion for
{0, 1, 2, 'n'}56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 self.assertIsNone({0, 1, 2, 'n'}) 60 61 62# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {0, 1, 2, 'n'} is not NoneI make the statement True
56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 # self.assertIsNone({0, 1, 2, 'n'}) 60 self.assertIsNotNone({0, 1, 2, 'n'}) 61 62 63# NOTESthe test passes.
I add an instance test
56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 # self.assertIsNone({0, 1, 2, 'n'}) 60 self.assertIsNotNone({0, 1, 2, 'n'}) 61 self.assertNotIsInstance({0, 1, 2, 'n'}, set) 62 63 64# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {0, 1, 2, 'n'} is an instance of <class 'set'>I make the statement True
56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 # self.assertIsNone({0, 1, 2, 'n'}) 60 self.assertIsNotNone({0, 1, 2, 'n'}) 61 # self.assertNotIsInstance({0, 1, 2, 'n'}, set) 62 self.assertIsInstance({0, 1, 2, 'n'}, set) 63 64 65# NOTESthe test passes.
I add another instance test
56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 # self.assertIsNone({0, 1, 2, 'n'}) 60 self.assertIsNotNone({0, 1, 2, 'n'}) 61 # self.assertNotIsInstance({0, 1, 2, 'n'}, set) 62 self.assertIsInstance({0, 1, 2, 'n'}, set) 63 self.assertIsInstance(None, set) 64 65 66# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'set'>because None is NOT a set.
I make the statement True
56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 # self.assertIsNone({0, 1, 2, 'n'}) 60 self.assertIsNotNone({0, 1, 2, 'n'}) 61 # self.assertNotIsInstance({0, 1, 2, 'n'}, set) 62 self.assertIsInstance({0, 1, 2, 'n'}, set) 63 # self.assertIsInstance(None, set) 64 self.assertNotIsInstance(None, set) 65 66 67# NOTESthe test passes.
I remove the commented lines
56 def test_is_none_a_set(self): 57 self.assertIsNotNone(set()) 58 self.assertIsNotNone({0, 1, 2, 'n'}) 59 self.assertIsInstance({0, 1, 2, 'n'}, set) 60 self.assertNotIsInstance(None, set) 61 62 63# NOTESI add a comment
63# NOTES 64# None is NOT a set 65# None is NOT a list 66# None is NOT a tuple 67# None is NOT a string 68# None is NOT a float 69# None is NOT an integer 70# None is NOT a boolean 71# None is NoneI add a git commit message in the other terminal
git commit -am 'add test_is_none_a_set'
test_is_none_a_dictionary
Is None a dictionary?
RED: make it fail
I go back to the terminal where the tests are running
One last test, this one for if None is a dictionary (key-value pairs in curly braces ‘{ }’ separated by commas)
56 def test_is_none_a_set(self): 57 self.assertIsNotNone(set()) 58 self.assertIsNotNone({0, 1, 2, 'n'}) 59 self.assertIsInstance({0, 1, 2, 'n'}, set) 60 self.assertNotIsInstance(None, set) 61 62 def test_is_none_a_dictionary(self): 63 self.assertIsNone(dict()) 64 65 66# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {} is not Nonewait a minute! Python uses
{}for sets. It also uses them for dictionaries with a difference.
GREEN: make it pass
I make the statement True
62 def test_is_none_a_dictionary(self):
63 # self.assertIsNone(dict())
64 self.assertIsNotNone(dict())
65
66
67# NOTES
the test passes.
REFACTOR: make it better
I add an assertion for
{'key': 'value'}62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 self.assertIsNone({'key': 'value'}) 66 67 68# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is not NoneI make the statement True
62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 68 69# NOTESthe test passes.
I add a failing instance test for
{}62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 self.assertNotIsInstance({}, dict) 68 69 70# NOTESdict is the class for dictionaries
the terminal is my friend, and shows AssertionError
AssertionError: {} is an instance of <class 'dict'>{}is the empty dictionaryset()is the empty set
I change the assert method
62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 # self.assertNotIsInstance({}, dict) 68 self.assertIsInstance({}, dict) 69 70 71# NOTESthe test passes.
I add an instance test for
{'key': 'value'}62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 # self.assertNotIsInstance({}, dict) 68 self.assertIsInstance({}, dict) 69 self.assertNotIsInstance( 70 {'key': 'value'}, dict 71 ) 72 73 74# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {'key': 'value'} is an instance of <class 'dict'>I make the statement True
62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 # self.assertNotIsInstance({}, dict) 68 self.assertIsInstance({}, dict) 69 # self.assertNotIsInstance( 70 self.assertIsInstance( 71 {'key': 'value'}, dict 72 ) 73 74 75# NOTESthe test passes.
I add the last failing instance test with assertIsInstance
62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 # self.assertNotIsInstance({}, dict) 68 self.assertIsInstance({}, dict) 69 # self.assertNotIsInstance( 70 self.assertIsInstance( 71 {'key': 'value'}, dict 72 ) 73 self.assertIsInstance(None, dict) 74 75 76# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: None is not an instance of <class 'dict'>because None is NOT a dictionary.
I make the statement True with assertNotIsInstance
62 def test_is_none_a_dictionary(self): 63 # self.assertIsNone(dict()) 64 self.assertIsNotNone(dict()) 65 # self.assertIsNone({'key': 'value'}) 66 self.assertIsNotNone({'key': 'value'}) 67 # self.assertNotIsInstance({}, dict) 68 self.assertIsInstance({}, dict) 69 # self.assertNotIsInstance( 70 self.assertIsInstance( 71 {'key': 'value'}, dict 72 ) 73 # self.assertIsInstance(None, dict) 74 self.assertNotIsInstance(None, dict) 75 76 77# NOTESthe test passes.
I remove the commented lines
62 def test_is_none_a_dictionary(self): 63 self.assertIsNotNone(dict()) 64 self.assertIsNotNone({'key': 'value'}) 65 self.assertIsInstance({}, dict) 66 self.assertIsInstance( 67 {'key': 'value'}, dict 68 ) 69 self.assertNotIsInstance(None, dict) 70 71 72# NOTESI add the last comment
72# NOTES 73# None is NOT a dictionary 74# None is NOT a set 75# None is NOT a list 76# None is NOT a tuple 77# None is NOT a string 78# None is NOT a float 79# None is NOT an integer 80# None is NOT a boolean 81# None is None 82 83 84# Exceptions seen 85# AssertionErrorI add a git commit message in the other terminal
git commit -am \ 'add test_is_none_a_dictionary'
sets vs dictionaries
{'key': 'value'} is a dictionary with : separating the key on the left from the value on the right.
I can add more key-value pairs separating them with commas, for example
{
'key': 'value',
'another_key': 'another value',
'one_more_key': 'one more value',
'magic_key': 'magic value',
'keyN': [0, 1, 2, 'n'],
}
sets do NOT have key-value pairs.
{0, 1, 2, 'n'}
close the project
I close
test_none.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
nonecd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I used assertIsInstance and assertNotIsInstance to show that None is not an instance of the other basic types.
I used two new assert methods for None:
assertIsNotNone which raises AssertionError if the thing in parentheses is None.
assertIsNone which raises AssertionError if the thing in parentheses is NOT None.
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
so far I have seen
Would you like to use assertIsNotNone and assertIsNone to test these projects?
Would you like to use the assertIsNotNone and assertIsNone methods with the assertion_error project?
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.