None (the simplest object)
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
I have these tests by the end of the chapter
questions about None
Questions to think about as I go through the chapter
requirements
I name this project
noneI open a terminal
I use uv to make a directory for the project and initialize it
uv init nonethe terminal shows
Initialized project `none` at `.../pumping_python/none`then 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__.pyNew-Item tests/__init__.pythe terminal goes back to the command line.
I 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.pythe terminal goes back to the command line.
I delete the text 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.assertFalse(True)I go back to the terminal to make a requirements file for the Python packages I need
echo "pytest-watcher" > requirements.txtthe terminal goes back to the command line.
I 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 .the terminal goes back to the command line.
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.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 ========================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
fix those errors and try to run
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.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.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.
REFACTOR: make it better
I can also use another assert method from the unittest.TestCase class to test if something is NOT the same object as None.
how I test if something is NOT None
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 seenthe terminal is my friend, and shows AssertionError
AssertionError: unexpectedly Nonebecause assertIsNotNone raises AssertionError if the object given in parentheses is None.
how I test if something is None
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 seenthe test passes because None is None and assertIsNotNone raises AssertionError if the object given in parentheses is NOT None.
I remove the commented lines
6 def test_what_is_none(self): 7 self.assertIs(None, None) 8 self.assertIsNone(None) 9 10 11# Exceptions seen 12# AssertionErrorI 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 chapter
I add a git commit message in the other terminal
git commit -am 'add test_what_is_none'
test_is_none_a_boolean
RED: make it fail
I go back to the terminal that is running the tests
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 make the assertion True with 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 chapter.
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 make the assertion True with assertIsNotNone
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 15 16# NOTESthe test passes.
I add a comment
17# NOTES 18# True is NOT None 19# False is NOT None 20# None is Nonealso a comment from the assertion_error chapter
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'>because False is an instance of the bool class.
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'>because True is an instance of the bool class.
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'>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 chapter.
I add a git commit message in the other terminal
git commit -am 'add test_is_none_a_boolean'
I know two new assert methods
assertIsNotNone to test if something is NOT None
assertIsNone to test if something is None
Caution
the names of the assert methods can be confusing,
there is
assertIsNotNonewhereNotcomes afterIsthen there is
assertNotIsInstancewhereIscomes afterNot
Would assertIsNotInstance be better than assertNotIsInstance, since assertNotIsNone does not sound better than assertIsNotNone? In this case no, 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 that is running the tests
I add a test to see if None is an integer (a whole number)
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.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 another assertion
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 another 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 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 another 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 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'>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 that is running the tests
I add a test to see if None is a float (binary floating point decimal number)
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.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
26 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 another call to assertIsNone
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 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
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 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 floatI 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
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 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 numberI 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
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 39 40# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 0.1 is an instance of <class 'float'>because
0.1is a floatI 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'>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 that is running the tests
I add a test to see if None is a string (anything inside quotes)
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.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
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 self.assertIsNone("text")the terminal is my friend, and shows AssertionError
AssertionError: 'text' 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("text") 39 self.assertIsNotNone("text") 40 41 42# NOTESthe test passes.
I add a failing line with assertNotIsInstance
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("text") 39 self.assertIsNotNone("text") 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("text") 39 self.assertIsNotNone("text") 40 # self.assertNotIsInstance('', str) 41 self.assertIsInstance('', str) 42 43 44# NOTESthe test passes.
I add another assertion
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("text") 39 self.assertIsNotNone("text") 40 # self.assertNotIsInstance('', str) 41 self.assertIsInstance('', str) 42 self.assertNotIsInstance("text", str) 43 44 45# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: 'text' 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("text") 39 self.assertIsNotNone("text") 40 # self.assertNotIsInstance('', str) 41 self.assertIsInstance('', str) 42 # self.assertNotIsInstance("text", str) 43 self.assertIsInstance("text", str) 44 45 46# NOTESthe test passes.
I add another failing line with assertIsInstance
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("text") 39 self.assertIsNotNone("text") 40 # self.assertNotIsInstance('', str) 41 self.assertIsInstance('', str) 42 # self.assertNotIsInstance("text", str) 43 self.assertIsInstance("text", 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'>I change the assert method
35 def test_is_none_a_string(self): 36 # self.assertIsNone('') 37 self.assertIsNotNone('') 38 # self.assertIsNone("text") 39 self.assertIsNotNone("text") 40 # self.assertNotIsInstance('', str) 41 self.assertIsInstance('', str) 42 # self.assertNotIsInstance("text", str) 43 self.assertIsInstance("text", 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("text") 38 self.assertIsInstance('', str) 39 self.assertIsInstance("text", 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 that is running the tests
I add a test to see if None is a tuple (anything in parentheses (
( )) separated by commas), pronouncedtwo-pull35 def test_is_none_a_string(self): 36 self.assertIsNotNone('') 37 self.assertIsNotNone("text") 38 self.assertIsInstance('', str) 39 self.assertIsInstance("text", str) 40 self.assertNotIsInstance(None, str) 41 42 def test_is_none_a_tuple(self): 43 self.assertIsNone(()) 44 45 46# NOTES
the 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
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 self.assertIsNone((1, 2, 3, 'n')) 46 47 48# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: (1, 2, 3, '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((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 48 49# NOTESthe test passes.
I add a failing line with assertNotIsInstance
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 self.assertNotIsInstance((), tuple) 48 49 50# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: () is an instance of <class 'tuple'>because in Python anything in parentheses (
( )) separated by commas is a tuple.I make the statement True
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 50 51# NOTESthe test passes.
I add another failing line
42 def test_is_none_a_tuple(self): 43 # self.assertIsNone(()) 44 self.assertIsNotNone(()) 45 # self.assertIsNone((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 self.assertNotIsInstance((1, 2, 3, 'n'), tuple) 50 51 52# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: (1, 2, 3, '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((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 # self.assertNotIsInstance((1, 2, 3, 'n'), tuple) 50 self.assertIsInstance((1, 2, 3, '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((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 # self.assertNotIsInstance((1, 2, 3, 'n'), tuple) 50 self.assertIsInstance((1, 2, 3, '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'>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((1, 2, 3, 'n')) 46 self.assertIsNotNone((1, 2, 3, 'n')) 47 # self.assertNotIsInstance((), tuple) 48 self.assertIsInstance((), tuple) 49 # self.assertNotIsInstance((1, 2, 3, 'n'), tuple) 50 self.assertIsInstance((1, 2, 3, '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((1, 2, 3, 'n')) 45 self.assertIsInstance((), tuple) 46 self.assertIsInstance((1, 2, 3, '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 that is running the tests
I add a new test to see if None is a list (anything in square brackets (
[ ]))42 def test_is_none_a_tuple(self): 43 self.assertIsNotNone(()) 44 self.assertIsNotNone((1, 2, 3, 'n')) 45 self.assertIsInstance((), tuple) 46 self.assertIsInstance((1, 2, 3, 'n'), tuple) 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 another failing line
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 self.assertIsNone([1, 2, 3, 'n'])the terminal is my friend, and shows AssertionError
AssertionError: [1, 2, 3, '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([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, 'n']) 54 55 56# NOTESthe test passes.
I add a failing instance test
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, '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([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 57 58# NOTESthe test passes.
I add another failing line with assertNotIsInstance
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 self.assertNotIsInstance([1, 2, 3, 'n'], list) 57 58 59# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: [1, 2, 3, '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([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 # self.assertNotIsInstance([1, 2, 3, 'n'], list) 57 self.assertIsInstance([1, 2, 3, '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([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 # self.assertNotIsInstance([1, 2, 3, 'n'], list) 57 self.assertIsInstance([1, 2, 3, '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'>I make the statement True
49 def test_is_none_a_list(self): 50 # self.assertIsNone([]) 51 self.assertIsNotNone([]) 52 # self.assertIsNone([1, 2, 3, 'n']) 53 self.assertIsNotNone([1, 2, 3, 'n']) 54 # self.assertNotIsInstance([], list) 55 self.assertIsInstance([], list) 56 # self.assertNotIsInstance([1, 2, 3, 'n'], list) 57 self.assertIsInstance([1, 2, 3, '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([1, 2, 3, 'n']) 52 self.assertIsInstance([], list) 53 self.assertIsInstance([1, 2, 3, '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 that is running the tests
I want to test if None is a set (anything in curly braces
{ }, not key-value pairs)49 def test_is_none_a_list(self): 50 self.assertIsNotNone([]) 51 self.assertIsNotNone([1, 2, 3, 'n']) 52 self.assertIsInstance([], list) 53 self.assertIsInstance([1, 2, 3, 'n'], list) 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 another assertion
56 def test_is_none_a_set(self): 57 # self.assertIsNone(set()) 58 self.assertIsNotNone(set()) 59 self.assertIsNone({1, 2, 3, 'n'}) 60 61 62# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {1, 2, 3, '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({1, 2, 3, 'n'}) 60 self.assertIsNotNone({1, 2, 3, '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({1, 2, 3, 'n'}) 60 self.assertIsNotNone({1, 2, 3, 'n'}) 61 self.assertNotIsInstance({1, 2, 3, 'n'}, set) 62 63 64# NOTESthe terminal is my friend, and shows AssertionError
AssertionError: {1, 2, 3, '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({1, 2, 3, 'n'}) 60 self.assertIsNotNone({1, 2, 3, 'n'}) 61 # self.assertNotIsInstance({1, 2, 3, 'n'}, set) 62 self.assertIsInstance({1, 2, 3, '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({1, 2, 3, 'n'}) 60 self.assertIsNotNone({1, 2, 3, 'n'}) 61 # self.assertNotIsInstance({1, 2, 3, 'n'}, set) 62 self.assertIsInstance({1, 2, 3, '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'>I make the statement True
55 def test_is_none_a_set(self): 56 # self.assertIsNone(set()) 57 self.assertIsNotNone(set()) 58 # self.assertIsNone({1, 2, 3, 'n'}) 59 self.assertIsNotNone({1, 2, 3, 'n'}) 60 # self.assertNotIsInstance({1, 2, 3, 'n'}, set) 61 self.assertIsInstance({1, 2, 3, 'n'}, set) 62 # self.assertIsInstance(None, set) 63 self.assertNotIsInstance(None, set) 64 65 66# NOTESthe test passes.
I remove the commented lines
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# 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
RED: make it fail
I go back to the terminal that is running the tests
One last test, this one for if None is a dictionary (key-value pairs in curly braces ‘{ }’ separated by a comma)
56 def test_is_none_a_set(self): 57 self.assertIsNotNone(set()) 58 self.assertIsNotNone({1, 2, 3, 'n'}) 59 self.assertIsInstance({1, 2, 3, '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 another failing line
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
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 dictionary
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 another instance test
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': 'valueN',
}
sets do NOT have key-value pairs.
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 assert methods to test what None is and what it is NOT.
Two from the inheritance chapter:
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
to show that None is not an instance of the other basic types.
And two new assert methods for None:
assertIsNone which raises AssertionError if the thing in parentheses is NOT None. It replaced assertIs from the assertion_error project except in test_what_is_none
assertIsNotNone which raises AssertionError if the thing in parentheses is None. It replaced assertIsNot from the assertion_error project
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 commassets - anything in curly braces (
{ }) separated by commas and 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 seen
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.