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 use None, It is the simplest data structure.
In AssertionError, I used assertIsNone and assertIsNotNone in test_assertion_error_w_none, where I saw that
I want to use assert methods to compare None with the other Python data structures to see what it is and what it is not
preview
These are the tests I have by the end of the chapter
1import unittest
2
3
4class TestNone(unittest.TestCase):
5
6 def test_what_is_none(self):
7 self.assertIsNone(None)
8
9 def test_is_none_a_boolean(self):
10 self.assertIsNotNone(False)
11 self.assertIsNotNone(True)
12 self.assertIsInstance(False, bool)
13 self.assertIsInstance(True, bool)
14 self.assertNotIsInstance(None, bool)
15
16 def test_is_none_an_integer(self):
17 self.assertIsNotNone(-1)
18 self.assertIsNotNone(0)
19 self.assertIsNotNone(1)
20 self.assertIsInstance(-1, int)
21 self.assertIsInstance(0, int)
22 self.assertIsInstance(1, int)
23 self.assertNotIsInstance(None, int)
24
25 def test_is_none_a_float(self):
26 self.assertIsNotNone(-0.1)
27 self.assertIsNotNone(0.0)
28 self.assertIsNotNone(0.1)
29 self.assertIsInstance(-0.1, float)
30 self.assertIsInstance(0.0, float)
31 self.assertIsInstance(0.1, float)
32 self.assertNotIsInstance(None, float)
33
34 def test_is_none_a_string(self):
35 self.assertIsNotNone('')
36 self.assertIsNotNone("text")
37 self.assertIsInstance('', str)
38 self.assertIsInstance("text", str)
39 self.assertNotIsInstance(None, str)
40
41 def test_is_none_a_tuple(self):
42 self.assertIsNotNone(())
43 self.assertIsNotNone((1, 2, 3, 'n'))
44 self.assertIsInstance((), tuple)
45 self.assertIsInstance((1, 2, 3, 'n'), tuple)
46 self.assertNotIsInstance(None, tuple)
47
48 def test_is_none_a_list(self):
49 self.assertIsNotNone([])
50 self.assertIsNotNone([1, 2, 3, 'n'])
51 self.assertIsInstance([], list)
52 self.assertIsInstance([1, 2, 3, 'n'], list)
53 self.assertNotIsInstance(None, list)
54
55 def test_is_none_a_set(self):
56 self.assertIsNotNone(set())
57 self.assertIsNotNone({1, 2, 3, 'n'})
58 self.assertIsInstance({1, 2, 3, 'n'}, set)
59 self.assertNotIsInstance(None, set)
60
61 def test_is_none_a_dictionary(self):
62 self.assertIsNotNone(dict())
63 self.assertIsNotNone({'key': 'value'})
64 self.assertIsInstance({}, dict)
65 self.assertIsInstance({'key': 'value'}, dict)
66 self.assertNotIsInstance(None, dict)
67
68
69# NOTES
70# None is NOT a dictionary
71# None is NOT a set
72# None is NOT a list
73# None is NOT a tuple
74# None is NOT a string
75# None is NOT a float
76# None is NOT an integer
77# None is NOT a boolean
78# None is None
79
80
81# Exceptions seen
82# AssertionError
questions about None
Here are questions you can answer after going through this chapter
requirements
I name this project
noneI open a terminal
I make a directory for the project
mkdir nonethe terminal goes back to the command line
I change directory to the project
cd nonethe terminal shows I am in the
nonefolder.../pumping_python/noneI make a directory for the tests
mkdir teststhe terminal goes back to the command line
I make the
testsdirectory a Python packageDanger
use 2 underscores (__) before and after
initfor__init__.pynot_init_.pytouch tests/__init__.pyNote
on Windows without Windows Subsystem for Linux use
New-Item tests/__init__.pyinstead oftouch tests/__init__.pyNew-Item tests/__init__.pythe terminal goes back to the command line
I make a Python file for the tests in the
testsdirectorytouch tests/test_none.pyNote
on Windows without Windows Subsystem for Linux use
New-Item tests/test_none.pyinstead oftouch tests/test_none.pyNew-Item tests/test_none.pythe terminal goes back to the command line
I open
test_none.pyin the editor of the Integrated Development Environment (IDE)Tip
I can use the terminal to open a file in the Integrated Development Environment (IDE) by typing the name of the program and the name of the file. That means when I type this in the terminal
code tests/test_none.pyVisual Studio Code opens
test_none.pyin the editorI add the first failing test to
test_none.py1import unittest 2 3 4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True)I make a requirements file for the Python packages I need, in the terminal
echo "pytest" > requirements.txtthe terminal goes back to the command line
I add pytest-watcher to the file
echo "pytest-watcher" >> requirements.txtthe terminal goes back to the command line
I setup the project with uv
uv initthe terminal shows
Initialized project `none`then goes back to the command line
I remove
main.pyfrom the project because I do not use itrm main.pythe terminal goes back to the command line
I install the Python packages I gave in the requirements file
uv add --requirement requirements.txtthe terminal shows it installed the Python packages
I use pytest-watcher to run the tests automatically
uv run pytest-watcher . --nowthe terminal shows
================================ FAILURES ================================ _________________________ TestNone.test_failure __________________________ self = <tests.test_none.TestNone testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_none.py:7: AssertionError ======================== short test summary info ========================= FAILED tests/test_none.py::TestNone::test_failure - AssertionError: True is not false =========================== 1 failed in X.YZs ============================I add AssertionError to the list of Exceptions seen in
test_none.pyin the editor4class TestNone(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(True) 8 9 10# Exceptions seen 11# AssertionErrorthen I change True to False in the assertion
7 self.assertFalse(False)the test passes
test_what_is_none
RED: make it fail
I change
test_failuretotest_what_is_none4class TestNone(unittest.TestCase): 5 6 def test_what_is_none(self): 7 self.assertIsNotNone(None) 8 9 10# Exceptions seenthe terminal shows AssertionError
AssertionError: unexpectedly NoneassertIsNotNone checks that what is in the parentheses is NOT None
GREEN: make it pass
I change the assertIsNotNone to assertIsNone, which checks if what it gets in parentheses is None
7 self.assertIsNone(None)
REFACTOR: make it better
I add a comment
4class TestNone(unittest.TestCase):
5
6 def test_what_is_none(self):
7 self.assertIsNone(None)
8
9
10# NOTES
11# None is None
12
13
14# Exceptions seen
15# AssertionError
this is the same comment from the AssertionError chapter
test_is_none_a_boolean
RED: make it fail
I add another failing test to see if None is a boolean
6 def test_what_is_none(self):
7 self.assertIsNone(None)
8
9 def test_is_none_a_boolean(self):
10 self.assertIsNone(False)
11
12
13# NOTES
14# None is None
the terminal shows AssertionError
AssertionError: False is not None
GREEN: make it pass
I make the assertion True with the assertIsNotNone method
9 def test_is_none_a_boolean(self):
10 self.assertIsNotNone(False)
11
12
13# NOTES
the test passes
REFACTOR: make it better
I add a comment
13# NOTES 14# False is NOT None 15# None is Noneanother comment from the AssertionError chapter
I add an assertion for the other boolean
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNone(True) 12 13 14# NOTESthe terminal shows AssertionError
AssertionError: True is not NoneI make the assertion True with assertIsNotNone
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNotNone(True) 12 13 14# NOTESthe test passes
I add a comment
14# NOTES 15# True is NOT None 16# False is NOT None 17# None is Noneanother comment from the AssertionError chapter
how to test if something is an instance of a class
The unittest.TestCase class has 2 methods I can use to test if an object is a child (instance) of a class or not - assertIsInstance and assertNotIsInstance
I add the assertNotIsInstance method to test if False is a boolean
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNotNone(True) 12 self.assertNotIsInstance(False, bool) 13 14 15# NOTESassertNotIsInstance checks if the first item it is given is NOT a child (instance) of the second item. It is like asking the question,
"is False NOT a child of the bool class?"Okay, this is new, not something from the AssertionError chapter
the terminal shows AssertionError
AssertionError: False is an instance of <class 'bool'>I make the assertion True with the assertIsInstance method which checks if the first item it is given is a child (instance) of the second item. It is like asking the question
"is False a child of the bool class?"12 self.assertIsInstance(False, bool)the test passes
I add a failing line for the other boolean with assertNotIsInstance
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNotNone(True) 12 self.assertIsInstance(False, bool) 13 self.assertNotIsInstance(True, bool) 14 15 16# NOTESthe terminal shows AssertionError
AssertionError: True is an instance of <class 'bool'>I make the statement True with assertIsInstance
13 self.assertIsInstance(True, bool)the test passes
I add assertIsInstance to test if None is a child (instance) of the bool class
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNotNone(True) 12 self.assertIsInstance(False, bool) 13 self.assertIsInstance(True, bool) 14 self.assertIsInstance(None, bool) 15 16 17# NOTESthe terminal shows AssertionError
AssertionError: None is not an instance of <class 'bool'>I make the line True with assertNotIsInstance
9 def test_is_none_a_boolean(self): 10 self.assertIsNotNone(False) 11 self.assertIsNotNone(True) 12 self.assertIsInstance(False, bool) 13 self.assertIsInstance(True, bool) 14 self.assertNotIsInstance(None, bool) 15 16 17# NOTESthe test passes
Since this is about None, I change the last 2 comments I added
17# NOTES 18# None is NOT a boolean 19# None is None
I know two new assert methods
assertIsInstance to see if something is an instance of a class
assertNotIsInstance to see if something is NOT an instance of a class
Caution
the names of the assert methods can be confusing, there is
where Not comes after Is and then there is
where Is comes after Not
Maybe assertIsNotInstance would have been better, since assertNotIsNone does not sound better than assertIsNotNone.
Naming things is its own challenge
test_is_none_an_integer
RED: make it fail
I add a test to see if None is an integer (a whole number)
9 def test_is_none_a_boolean(self):
10 self.assertIsNotNone(False)
11 self.assertIsNotNone(True)
12 self.assertIsInstance(False, bool)
13 self.assertIsInstance(True, bool)
14 self.assertNotIsInstance(None, bool)
15
16 def test_is_none_an_integer(self):
17 self.assertIsNone(-1)
18
19
20# NOTES
the terminal shows AssertionError
AssertionError: -1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
17 self.assertIsNotNone(-1)
the test passes
REFACTOR: make it better
I add a new failing line with assertIsNone
16 def test_is_none_an_integer(self): 17 self.assertIsNotNone(-1) 18 self.assertIsNone(0)the terminal shows AssertionError
AssertionError: 0 is not NoneI make the statement True with assertIsNotNone
18 self.assertIsNotNone(0)the test passes
I add another assertion
16 def test_is_none_an_integer(self): 17 self.assertIsNotNone(-1) 18 self.assertIsNotNone(0) 19 self.assertIsNone(1)the terminal shows AssertionError
AssertionError: 1 is not NoneI change the line to make it True
19 self.assertIsNotNone(1)the test passes
I add a new failing line with assertNotIsInstance
16 def test_is_none_an_integer(self): 17 self.assertIsNotNone(-1) 18 self.assertIsNotNone(0) 19 self.assertIsNotNone(1) 20 self.assertNotIsInstance(-1, int)int is the class for integers, the terminal shows AssertionError
AssertionError: -1 is an instance of <class 'int'>I use
-1for all the integers (whole numbers) that are smaller than0I make the line True with assertIsInstance
20 self.assertIsInstance(-1, int)the test passes
I add another failing line with assertNotIsInstance
16 def test_is_none_an_integer(self): 17 self.assertIsNotNone(-1) 18 self.assertIsNotNone(0) 19 self.assertIsNotNone(1) 20 self.assertIsInstance(-1, int) 21 self.assertNotIsInstance(0, int)the terminal shows AssertionError
AssertionError: 0 is an instance of <class 'int'>0is an integerI change the statement to make it True
21 self.assertIsInstance(0, int)the test passes
I add another failing line with assertNotIsInstance
20 self.assertIsInstance(-1, int) 21 self.assertIsInstance(0, int) 22 self.assertNotIsInstance(1, int) 23 24 25# NOTES 26# None is NOT a boolean 27# None is Nonethe terminal shows AssertionError
AssertionError: 1 is an instance of <class 'int'>I use
1for all the integers (whole numbers) that are bigger than0I make the failing line True with assertIsInstance
22 self.assertIsInstance(1, int)the test passes
I add one more failing line to test if None is an integer with assertIsInstance
20 self.assertIsInstance(-1, int) 21 self.assertIsInstance(0, int) 22 self.assertIsInstance(1, int) 23 self.assertIsInstance(None, int)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'int'>I make the line True with assertNotIsInstance
16 def test_is_none_an_integer(self): 17 self.assertIsNotNone(-1) 18 self.assertIsNotNone(0) 19 self.assertIsNotNone(1) 20 self.assertIsInstance(-1, int) 21 self.assertIsInstance(0, int) 22 self.assertIsInstance(1, int) 23 self.assertNotIsInstance(None, int) 24 25 26# NOTESthe test passes
I add a new comment
26# NOTES 27# None is NOT an integer 28# None is NOT a boolean 29# None is None 30 31 32# Exceptions seen 33# AssertionError
test_is_none_a_float
RED: make it fail
I add a test to see if None is a float (binary floating point decimal number)
16 def test_is_none_an_integer(self):
17 self.assertIsNotNone(-1)
18 self.assertIsNotNone(0)
19 self.assertIsNotNone(1)
20 self.assertIsInstance(-1, int)
21 self.assertIsInstance(0, int)
22 self.assertIsInstance(1, int)
23 self.assertNotIsInstance(None, int)
24
25 def test_is_none_a_float(self):
26 self.assertIsNone(-0.1)
27
28
29# NOTES
the terminal shows AssertionError
AssertionError: -0.1 is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
26 self.assertIsNotNone(-0.1)
the test passes
REFACTOR: make it better
I add another failing line with assertIsNone
25 def test_is_none_a_float(self): 26 self.assertIsNotNone(-0.1) 27 self.assertIsNone(0.0)the terminal shows AssertionError
AssertionError: 0.0 is not NoneI make the statement True with assertIsNotNone
27 self.assertIsNotNone(0.0)the test passes
I add a failing line with assertIsNone
25 def test_is_none_a_float(self): 26 self.assertIsNotNone(-0.1) 27 self.assertIsNotNone(0.0) 28 self.assertIsNone(0.1)the terminal shows AssertionError
AssertionError: 0.1 is not NoneI make the statement True with assertIsNotNone
28 self.assertIsNotNone(0.1)the test passes
time for instance tests. I add a failing line with assertNotIsInstance
25 def test_is_none_a_float(self): 26 self.assertIsNotNone(-0.1) 27 self.assertIsNotNone(0.0) 28 self.assertIsNotNone(0.1) 29 self.assertNotIsInstance(-0.1, float)float is the class for binary floating point numbers. The terminal shows AssertionError
AssertionError: -0.1 is an instance of <class 'float'>I use
-0.1for all the binary floating point numbers that are smaller than0.0I make the statement True with assertIsInstance
29 self.assertIsInstance(-0.1, float)the test passes
I add the next instance test with assertNotIsInstance
25 def test_is_none_a_float(self): 26 self.assertIsNotNone(-0.1) 27 self.assertIsNotNone(0.0) 28 self.assertIsNotNone(0.1) 29 self.assertIsInstance(-0.1, float) 30 self.assertNotIsInstance(0.0, float)the terminal shows AssertionError
AssertionError: 0.0 is an instance of <class 'float'>0.0is a binary floating point numberI make the statement True with assertIsInstance
30 self.assertIsInstance(0.0, float)the test passes
I add a failing line with assertNotIsInstance
29 self.assertIsInstance(-0.1, float) 30 self.assertIsInstance(0.0, float) 31 self.assertNotIsInstance(0.1, float) 32 33 34# NOTESthe terminal shows AssertionError
AssertionError: 0.1 is an instance of <class 'float'>I use
0.1for all the binary floating point numbers that are bigger than0.0I make the statement True with assertIsInstance
31 self.assertIsInstance(0.1, float)the test passes
I add one more failing line with the assertIsInstance method
29 self.assertIsInstance(-0.1, float) 30 self.assertIsInstance(0.0, float) 31 self.assertIsInstance(0.1, float) 32 self.assertIsInstance(None, float) 33 34 35# NOTESthe terminal shows AssertionError
AssertionError: None is not an instance of <class 'float'>I make the statement True with the assertNotIsInstance method
25 def test_is_none_a_float(self): 26 self.assertIsNotNone(-0.1) 27 self.assertIsNotNone(0.0) 28 self.assertIsNotNone(0.1) 29 self.assertIsInstance(-0.1, float) 30 self.assertIsInstance(0.0, float) 31 self.assertIsInstance(0.1, float) 32 self.assertNotIsInstance(None, float) 33 34 35# NOTESthe test passes
I add a new comment
35# NOTES 36# None is NOT a float 37# None is NOT an integer 38# None is NOT a boolean 39# None is None 40 41 42# Exceptions seen 43# AssertionError
test_is_none_a_string
RED: make it fail
I add a test to see if None is a string (anything inside quotes)
25 def test_is_none_a_float(self):
26 self.assertIsNotNone(-0.1)
27 self.assertIsNotNone(0.0)
28 self.assertIsNotNone(0.1)
29 self.assertIsInstance(-0.1, float)
30 self.assertIsInstance(0.0, float)
31 self.assertIsInstance(0.1, float)
32 self.assertNotIsInstance(None, float)
33
34 def test_is_none_a_string(self):
35 self.assertIsNone('')
36
37
38# NOTES
the terminal shows AssertionError
AssertionError: '' is not None
GREEN: make it pass
I make the statement True with assertIsNotNone
35 self.assertIsNotNone('')
the test passes
REFACTOR: make it better
I add another failing line with assertIsNone
34 def test_is_none_a_string(self): 35 self.assertIsNotNone('') 36 self.assertIsNone("text")the terminal shows AssertionError
AssertionError: 'text' is not NoneI change the assert method to make the statement True
36 self.assertIsNotNone("text")the test passes
I add a failing line with assertNotIsInstance
34 def test_is_none_a_string(self): 35 self.assertIsNotNone('') 36 self.assertIsNotNone("text") 37 self.assertNotIsInstance('', str)str is the class for strings. The terminal shows AssertionError
AssertionError: '' is an instance of <class 'str'>I change the assert method
37 self.assertIsInstance('', str)the test passes
I add another assertion
34 def test_is_none_a_string(self): 35 self.assertIsNotNone('') 36 self.assertIsNotNone("text") 37 self.assertIsInstance('', str) 38 self.assertNotIsInstance("text", str)the terminal shows AssertionError
AssertionError: 'text' is an instance of <class 'str'>I change the assert method
38 self.assertIsInstance("text", str)the test passes
I add another failing line with assertIsInstance
37 self.assertIsInstance('', str) 38 self.assertIsInstance("text", str) 39 self.assertIsInstance(None, str) 40 41 42# NOTESthe terminal shows AssertionError
AssertionError: None is not an instance of <class 'str'>I change the assert method
34 def test_is_none_a_string(self): 35 self.assertIsNotNone('') 36 self.assertIsNotNone("text") 37 self.assertIsInstance('', str) 38 self.assertIsInstance("text", str) 39 self.assertNotIsInstance(None, str) 40 41 42# NOTESthe test passes
I add a comment
42# NOTES 43# None is NOT a string 44# None is NOT a float 45# None is NOT an integer 46# None is NOT a boolean 47# None is None 48 49 50# Exceptions seen 51# AssertionError
test_is_none_a_tuple
RED: make it fail
I add a test to see if None is a tuple (anything in parentheses (()) separated by commas), pronounced too-pull
34 def test_is_none_a_string(self):
35 self.assertIsNotNone('')
36 self.assertIsNotNone("text")
37 self.assertIsInstance('', str)
38 self.assertIsInstance("text", str)
39 self.assertNotIsInstance(None, str)
40
41 def test_is_none_a_tuple(self):
42 self.assertIsNone(())
43
44
45# NOTES
the terminal shows AssertionError
AssertionError: () is not None
GREEN: make it pass
I make the statement True
42 self.assertIsNotNone(())
the test passes
REFACTOR: make it better
I add a failing assertion
42 def test_is_none_a_tuple(self): 43 self.assertIsNotNone(()) 44 self.assertIsNone((1, 2, 3, 'n'))the terminal shows AssertionError
AssertionError: (1, 2, 3, 'n') is not NoneI make the statement True
43 self.assertIsNotNone((1, 2, 3, 'n'))the test passes
I add a failing line with assertNotIsInstance
41 def test_is_none_a_tuple(self): 42 self.assertIsNotNone(()) 43 self.assertIsNotNone((1, 2, 3, 'n')) 44 self.assertNotIsInstance((), tuple)the terminal shows AssertionError
AssertionError: () is an instance of <class 'tuple'>because anything in parentheses (
()) separated by commas in Python is a tupleI make the statement True
44 self.assertIsInstance((), tuple)the test passes
I add another failing line
41 def test_is_none_a_tuple(self): 42 self.assertIsNotNone(()) 43 self.assertIsNotNone((1, 2, 3, 'n')) 44 self.assertIsInstance((), tuple) 45 self.assertNotIsInstance((1, 2, 3, 'n'), tuple)the terminal shows AssertionError
AssertionError: (1, 2, 3, 'n') is an instance of <class 'tuple'>because anything in parentheses (
()) separated by commas in Python is a tupleI change the assert method to make the statement True
45 self.assertIsInstance((1, 2, 3, 'n'), tuple)the test passes
I add one more instance test
41 def test_is_none_a_tuple(self): 42 self.assertIsNotNone(()) 43 self.assertIsNotNone((1, 2, 3, 'n')) 44 self.assertIsInstance((), tuple) 45 self.assertIsInstance((1, 2, 3, 'n'), tuple) 46 self.assertIsInstance(None, tuple)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'tuple'>I change the statement to make it True
41 def test_is_none_a_tuple(self): 42 self.assertIsNotNone(()) 43 self.assertIsNotNone((1, 2, 3, 'n')) 44 self.assertIsInstance((), tuple) 45 self.assertIsInstance((1, 2, 3, 'n'), tuple) 46 self.assertNotIsInstance(None, tuple) 47 48 49# NOTESthe test passes
I add a comment
49# NOTES 50# None is NOT a tuple 51# None is NOT a string 52# None is NOT a float 53# None is NOT an integer 54# None is NOT a boolean 55# None is None
test_is_none_a_list
RED: make it fail
I add a new test to see if None is a list (anything in square brackets ([]))
41 def test_is_none_a_tuple(self):
42 self.assertIsNotNone(())
43 self.assertIsNotNone((1, 2, 3, 'n'))
44 self.assertIsInstance((), tuple)
45 self.assertIsInstance((1, 2, 3, 'n'), tuple)
46 self.assertNotIsInstance(None, tuple)
47
48 def test_is_none_a_list(self):
49 self.assertIsNone([])
50
51
52# NOTES
the terminal shows AssertionError
AssertionError: [] is not None
GREEN: make it pass
I change the assertion to make the statement True
49 self.assertIsNotNone([])
the test passes
REFACTOR: make it better
I add another failing line
48 def test_is_none_a_list(self): 49 self.assertIsNotNone([]) 50 self.assertIsNone([1, 2, 3, 'n'])the terminal shows AssertionError
AssertionError: [1, 2, 3, 'n'] is not NoneI make the statement True
50 self.assertIsNotNone([1, 2, 3, 'n'])the test passes
I add a failing instance test
48 def test_is_none_a_list(self): 49 self.assertIsNotNone([]) 50 self.assertIsNotNone([1, 2, 3, 'n']) 51 self.assertNotIsInstance([], list)the terminal shows AssertionError
AssertionError: [] is an instance of <class 'list'>because anything in square brackets (
[]) in Python is a listI make the statement True
51 self.assertIsInstance([], list)the test passes
I add another failing line with assertNotIsInstance
48 def test_is_none_a_list(self): 49 self.assertIsNotNone([]) 50 self.assertIsNotNone([1, 2, 3, 'n']) 51 self.assertIsInstance([], list) 52 self.assertNotIsInstance([1, 2, 3, 'n'], list)the terminal shows AssertionError
AssertionError: [1, 2, 3, 'n'] is an instance of <class 'list'>because anything in square brackets (
[]) in Python is a listI change the assert method
52 self.assertIsInstance([1, 2, 3, 'n'], list)the test passes
I add one more failing line with the assertIsInstance method
51 self.assertIsInstance([], list) 52 self.assertIsInstance([1, 2, 3, 'n'], list) 53 self.assertIsInstance(None, list) 54 55 56# NOTESthe terminal shows AssertionError
AssertionError: None is not an instance of <class 'list'>I make the statement True
48 def test_is_none_a_list(self): 49 self.assertIsNotNone([]) 50 self.assertIsNotNone([1, 2, 3, 'n']) 51 self.assertIsInstance([], list) 52 self.assertIsInstance([1, 2, 3, 'n'], list) 53 self.assertNotIsInstance(None, list) 54 55 56# NOTESthe test passes
I add a new comment
56# NOTES 57# None is NOT a list 58# None is NOT a tuple 59# None is NOT a string 60# None is NOT a float 61# None is NOT an integer 62# None is NOT a boolean 63# None is None
test_is_none_a_set
RED: make it fail
I want to see if None is a set
48 def test_is_none_a_list(self):
49 self.assertIsNotNone([])
50 self.assertIsNotNone([1, 2, 3, 'n'])
51 self.assertIsInstance([], list)
52 self.assertIsInstance([1, 2, 3, 'n'], list)
53 self.assertNotIsInstance(None, list)
54
55 def test_is_none_a_set(self):
56 self.assertIsNone(set())
57
58
59 # NOTES
the terminal shows AssertionError
AssertionError: set() is not None
GREEN: make it pass
I make the statement True
56 self.assertIsNotNone(set())
the test passes
REFACTOR: make it better
I add another assertion
55 def test_is_none_a_set(self): 56 self.assertIsNotNone(set()) 57 self.assertIsNone({1, 2, 3, 'n'})the terminal shows AssertionError
AssertionError: {1, 2, 3, 'n'} is not NoneI make the statement True
57 self.assertIsNotNone({1, 2, 3, 'n'})the test passes
I add an instance test
55 def test_is_none_a_set(self): 56 self.assertIsNotNone(set()) 57 self.assertIsNotNone({1, 2, 3, 'n'}) 58 self.assertNotIsInstance({1, 2, 3, 'n'}, set)the terminal shows AssertionError
AssertionError: {1, 2, 3, 'n'} is an instance of <class 'set'>I make the statement True
58 self.assertIsInstance({1, 2, 3, 'n'}, set)the test passes
I add another instance test
55 def test_is_none_a_set(self): 56 self.assertIsNotNone(set()) 57 self.assertIsNotNone({1, 2, 3, 'n'}) 58 self.assertIsInstance({1, 2, 3, 'n'}, set) 59 self.assertIsInstance(None, set)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'set'>I make the statement True
55 def test_is_none_a_set(self): 56 self.assertIsNotNone(set()) 57 self.assertIsNotNone({1, 2, 3, 'n'}) 58 self.assertIsInstance({1, 2, 3, 'n'}, set) 59 self.assertNotIsInstance(None, set) 60 61 62# NOTESthe test passes
I add a comment
62# NOTES 63# None is NOT a set 64# None is NOT a list 65# None is NOT a tuple 66# None is NOT a string 67# None is NOT a float 68# None is NOT an integer 69# None is NOT a boolean 70# None is None
test_is_none_a_dictionary
RED: make it fail
One last test, this one is to see if None is a dictionary
55 def test_is_none_a_set(self):
56 self.assertIsNotNone(set())
57 self.assertIsNotNone({1, 2, 3, 'n'})
58 self.assertIsInstance({1, 2, 3, 'n'}, set)
59 self.assertNotIsInstance(None, set)
60
61 def test_is_none_a_dictionary(self):
62 self.assertIsNone(dict())
63
64
65# NOTES
the terminal shows AssertionError
AssertionError: {} is not None
wait 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 self.assertIsNotNone(dict())
the test passes
REFACTOR: make it better
I add another failing line
61 def test_is_none_a_dictionary(self): 62 self.assertIsNotNone(dict()) 63 self.assertIsNone({'key': 'value'})the terminal shows AssertionError
AssertionError: {'key': 'value'} is not NoneI make the statement True
63 self.assertIsNotNone({'key': 'value'})the test passes
I add a failing instance test
61 def test_is_none_a_dictionary(self): 62 self.assertIsNotNone(dict()) 63 self.assertIsNotNone({'key': 'value'}) 64 self.assertNotIsInstance({}, dict)dict is the class for dictionaries, the terminal shows AssertionError
AssertionError: {} is an instance of <class 'dict'>{}is the empty dictionaryI change the assert method
64 self.assertIsInstance({}, dict)the test passes
I add another instance test
61 def test_is_none_a_dictionary(self): 62 self.assertIsNotNone(dict()) 63 self.assertIsNotNone({'key': 'value'}) 64 self.assertIsInstance({}, dict) 65 self.assertNotIsInstance({'key': 'value'}, dict)the terminal shows AssertionError
AssertionError: {'key': 'value'} is an instance of <class 'dict'>Note
{'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': 'valueN', }sets do NOT have key-value pairs.
I make the statement True
65 self.assertIsInstance({'key': 'value'}, dict)the test passes
I add the last failing instance test with assertIsInstance
61 def test_is_none_a_dictionary(self): 62 self.assertIsNotNone(dict()) 63 self.assertIsNotNone({'key': 'value'}) 64 self.assertIsInstance({}, dict) 65 self.assertIsInstance({'key': 'value'}, dict) 66 self.assertIsInstance(None, dict)the terminal shows AssertionError
AssertionError: None is not an instance of <class 'dict'>I make the statement True with assertNotIsInstance
61 def test_is_none_a_dictionary(self): 62 self.assertIsNotNone(dict()) 63 self.assertIsNotNone({'key': 'value'}) 64 self.assertIsInstance({}, dict) 65 self.assertIsInstance({'key': 'value'}, dict) 66 self.assertNotIsInstance(None, dict) 67 68 69# NOTESthe test passes
I add the last comment
69# NOTES 70# None is NOT a dictionary 71# None is NOT a set 72# None is NOT a list 73# None is NOT a tuple 74# None is NOT a string 75# None is NOT a float 76# None is NOT an integer 77# None is NOT a boolean 78# None is None 79 80 81# Exceptions seen 82# AssertionError
close the project
review
I used assert methods to test what None is and what it is NOT. I used 2 from the AssertionError chapter
assertIsNone - which tests if the thing in parentheses is None
assertIsNotNone - which tests if the thing in parentheses is NOT None
and 2 new assert methods
assertIsInstance which checks if something is an instance of a given class
assertNotIsInstance which checks if something is NOT an instance of a given class
I also used Python’s basic data structures in the tests
None - the simplest
integers - whole numbers, negative and positive, including
0floats - binary floating point decimal numbers, negative and positive including
0.0tuples - anything in parentheses (
()) separated by commas separated by commassets - anything in curly braces (
{}) separated by commas but NOT key-value pairs
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
so far you have covered
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