None¶
test_what_is_none¶
None is used when there is no value
red: make it fail¶
I open a terminal to run makePythonTdd.sh with
none
as the name of the project./makePythonTdd.sh none
on Windows without Windows Subsystem Linux use makePythonTdd.ps1
./makePythonTdd.ps1 none
it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError
E AssertionError: True is not false tests/test_none.py:7: AssertionError
I hold
ctrl
(windows/linux) oroption
(mac) on the keyboard and use the mouse to click ontests/test_none.py:7
to open it in the editorthen change
True
toFalse
to make the test passand change
test_failure
totest_what_is_none
import unittest class TestNone(unittest.TestCase): def test_what_is_none(self): self.assertIsNotNone(None)
the terminal shows AssertionError
AssertionError: unexpectedly None
assertIsNotNone checks that its input is NOT None
green: make it pass¶
When I use the assertIsNone method to check if the input is None
def test_what_is_none(self):
self.assertIsNone(None)
the test passes, and I add a note
# NOTES
# None is None
# Exceptions Encountered
# AssertionError
test_is_none_a_boolean¶
red: make it fail¶
I add another failing test
def test_what_is_none(self):
self.assertIsNone(None)
def test_is_none_a_boolean(self):
self.assertIsNone(False)
the terminal shows AssertionError
AssertionError: False is not None
green: make it pass¶
I add a note
# NOTES
# False is NOT None
# None is None
then change the method
def test_is_none_a_boolean(self):
self.assertIsNotNone(False)
and the test passes
refactor: make it better¶
I add another failing line
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNone(True)
the terminal shows AssertionError
AssertionError: True is not None
I add a note
# NOTES # True is NOT None # False is NOT None # None is None
then I change the method
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True)
and the test passes
The unittest.TestCase class has 2 methods I can use to test if an object is an instance of a class or not, I use them to test if None is a boolean
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True) self.assertNotIsInstance(False, bool)
I use the assertNotIsInstance method to test if False is NOT an instance of the bool class which is for booleans. The terminal shows AssertionError
AssertionError: False is an instance of <class 'bool'>
False is a boolean. I change the method to assertIsInstance to show that False is an instance of the bool class
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True) self.assertIsInstance(False, bool)
the test passes. I do it again with True
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True) self.assertIsInstance(False, bool) self.assertNotIsInstance(True, bool)
the terminal shows AssertionError
AssertionError: True is an instance of <class 'bool'>
True is a boolean. I use the assertIsInstance method
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True) self.assertIsInstance(False, bool) self.assertIsInstance(True, bool)
and the test passes
I add another line
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True) self.assertIsInstance(False, bool) self.assertIsInstance(True, bool) self.assertIsInstance(None, bool)
the terminal shows AssertionError
AssertionError: None is not an instance of <class 'bool'>
None is NOT a boolean. I change the method
def test_is_none_a_boolean(self): self.assertIsNotNone(False) self.assertIsNotNone(True) self.assertIsInstance(False, bool) self.assertIsInstance(True, bool) self.assertNotIsInstance(None, bool)
the test passes, and I change the last 2 notes I added
# NOTES # None is NOT a boolean # None is None
test_is_none_an_integer¶
red: make it fail¶
I add a test to see if None is an integer
def test_is_none_a_boolean(self):
...
self.assertNotIsInstance(None, bool)
def test_is_none_an_integer(self):
self.assertIsNone(-1)
the terminal shows AssertionError
AssertionError: -1 is not None
green: make it pass¶
I change the method to match
def test_is_none_an_integer(self):
self.assertIsNotNone(-1)
and the test passes
refactor: make it better¶
I add a new line
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNone(0)
the terminal shows AssertionError
AssertionError: 0 is not None
I change the method
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0)
and the terminal shows green again
I add another line
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNone(1)
the terminal shows AssertionError
AssertionError: 1 is not None
I change the method
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1)
and the test passes
I add an instance test
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertNotIsInstance(-1, int)
int is the class for integers, the terminal shows AssertionError
AssertionError: -1 is an instance of <class 'int'>
-1
is an integer for the positive integers. I make the test passdef test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int)
I add another instance test
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int) self.assertNotIsInstance(0, int)
the terminal shows AssertionError
AssertionError: 0 is an instance of <class 'int'>
0
is an integer. I change the methoddef test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int) self.assertIsInstance(0, int)
and the test passes
I add another one
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int) self.assertIsInstance(0, int) self.assertNotIsInstance(1, int)
the terminal shows AssertionError
AssertionError: 1 is an instance of <class 'int'>
1
is for the positive integers. I change the methoddef test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int) self.assertIsInstance(0, int) self.assertIsInstance(1, int)
and the terminal shows passing tests
one more instance test
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int) self.assertIsInstance(0, int) self.assertIsInstance(1, int) self.assertIsInstance(None, int)
and the terminal shows AssertionError
AssertionError: None is not an instance of <class 'int'>
I make the test pass
def test_is_none_an_integer(self): self.assertIsNotNone(-1) self.assertIsNotNone(0) self.assertIsNotNone(1) self.assertIsInstance(-1, int) self.assertIsInstance(0, int) self.assertIsInstance(1, int) self.assertNotIsInstance(None, int)
then add a new note
# NOTES # None is NOT an integer # None is NOT a boolean # None is None
test_is_none_a_float¶
red: make it fail¶
I add a test to see if None is a float
def test_is_none_an_integer(self):
...
self.assertNotIsInstance(None, int)
def test_is_none_a_float(self):
self.assertIsNone(-0.1)
the terminal shows AssertionError
AssertionError: -0.1 is not None
green: make it pass¶
I change the method
def test_is_none_a_float(self):
self.assertIsNotNone(-0.1)
and the test passes
refactor: make it better¶
I add another line
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNone(0.0)
the terminal shows AssertionError
AssertionError: 0.0 is not None
when I change the method to match
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0)
the test passes
I add a failing line
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNone(0.1)
the terminal shows AssertionError
AssertionError: 0.1 is not None
I change the method
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1)
and the test passes
time for instance tests
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertNotIsInstance(-0.1, float)
float is the class for floating point numbers, the terminal shows AssertionError
AssertionError: -0.1 is an instance of <class 'float'>
-0.1
is for the negative floating point numbers. I change the methoddef test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float)
and the test passes
I add the next instance test
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float) self.assertNotIsInstance(0.0, float)
the terminal shows AssertionError
AssertionError: 0.0 is an instance of <class 'float'>
0.0
is a floating point number. I change the methoddef test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float) self.assertIsInstance(0.0, float)
and the test passes
I add another instance test
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float) self.assertIsInstance(0.0, float) self.assertNotIsInstance(0.1, float)
the terminal shows AssertionError
AssertionError: 0.1 is an instance of <class 'float'>
0.1
is for the positive floating point numbers. I make the test passdef test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float) self.assertIsInstance(0.0, float) self.assertIsInstance(0.1, float)
then I add one more line
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float) self.assertIsInstance(0.0, float) self.assertIsInstance(0.1, float) self.assertIsInstance(None, float)
and get AssertionError
AssertionError: None is not an instance of <class 'float'>
when I change the method
def test_is_none_a_float(self): self.assertIsNotNone(-0.1) self.assertIsNotNone(0.0) self.assertIsNotNone(0.1) self.assertIsInstance(-0.1, float) self.assertIsInstance(0.0, float) self.assertIsInstance(0.1, float) self.assertNotIsInstance(None, float)
the test passes, time for a new note
# NOTES # None is NOT a float # None is NOT an integer # None is NOT a boolean # None is None
test_is_none_a_string¶
red: make it fail¶
I add a test to see if None is a string
def test_is_none_a_float(self):
...
self.assertNotIsInstance(None, float)
def test_is_none_a_string(self):
self.assertIsNone('')
the terminal shows AssertionError
AssertionError: '' is not None
green: make it pass¶
I change the method
def test_is_none_a_string(self):
self.assertIsNotNone('')
and the test passes
refactor: make it better¶
then I add another line
def test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNone("text")
the terminal shows AssertionError
AssertionError: 'text' is not None
I change the method to match
def test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text")
and the test passes
then I add a failing line for an instance test
def test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text") self.assertNotIsInstance('', str)
str is the class for strings. The terminal shows AssertionError
AssertionError: '' is an instance of <class 'str'>
''
is the empty string. I change the methoddef test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text") self.assertIsInstance('', str)
and the test passes
then I add another line
def test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text") self.assertIsInstance('', str) self.assertNotIsInstance("text", str)
and the terminal shows AssertionError
AssertionError: 'text' is an instance of <class 'str'>
'text'
is a string. I change the method to make the test passdef test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text") self.assertIsInstance('', str) self.assertIsInstance("text", str)
A string is anything in single, double or triple quotes, for example
'single quotes'
'''triple single quotes'''
"double quotes"
"""triple double quotes"""
see quotes for more
I add one more instance test
def test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text") self.assertIsInstance('', str) self.assertIsInstance("text", str) self.assertIsInstance(None, str)
and the terminal shows AssertionError
AssertionError: None is not an instance of <class 'str'>
when I change the method
def test_is_none_a_string(self): self.assertIsNotNone('') self.assertIsNotNone("text") self.assertIsInstance('', str) self.assertIsInstance("text", str) self.assertNotIsInstance(None, str)
the test passes and I add a note
# NOTES # None is NOT a string # None is NOT a float # None is NOT an integer # None is NOT a boolean # None is None
test_is_none_a_tuple¶
red: make it fail¶
I add a test to see if None is a tuple
def test_is_none_a_string(self):
...
self.assertNotIsInstance(None, str)
def test_is_none_a_tuple(self):
self.assertIsNone(())
the terminal shows AssertionError
AssertionError: () is not None
green: make it pass¶
I change the method
def test_is_none_a_tuple(self):
self.assertIsNotNone(())
and the test passes
refactor: make it better¶
I add a failing line
def test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNone((1, 2, 3, 'n'))
the terminal shows AssertionError
AssertionError: (1, 2, 3, 'n') is not None
I change the method
def test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n'))
and the test passes
then I add an instance test
def test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n')) self.assertNotIsInstance((), tuple)
and the terminal shows AssertionError
AssertionError: () is an instance of <class 'tuple'>
()
is the empty tuple. I change the methoddef test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n')) self.assertIsInstance((), tuple)
and the test passes
I add another line
def test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n')) self.assertIsInstance((), tuple) self.assertNotIsInstance((1, 2, 3, 'n'), tuple)
the terminal shows AssertionError
AssertionError: (1, 2, 3, 'n') is an instance of <class 'tuple'>
(1, 2, 3, 'n')
is a tuple. I change the methoddef test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n')) self.assertIsInstance((), tuple) self.assertIsInstance((1, 2, 3, 'n'), tuple)
and the test passes
I add one more instance test
def test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n')) self.assertIsInstance((), tuple) self.assertIsInstance((1, 2, 3, 'n'), tuple) self.assertIsInstance(None, tuple)
and the terminal shows AssertionError
AssertionError: None is not an instance of <class 'tuple'>
I change the method
def test_is_none_a_tuple(self): self.assertIsNotNone(()) self.assertIsNotNone((1, 2, 3, 'n')) self.assertIsInstance((), tuple) self.assertIsInstance((1, 2, 3, 'n'), tuple) self.assertNotIsInstance(None, tuple)
the test passes and I add a note
# NOTES # None is NOT a tuple # None is NOT a string # None is NOT a float # None is NOT an integer # None is NOT a boolean # None is None
test_is_none_a_list¶
red: make it fail¶
I add a new test for lists
def test_is_none_a_tuple(self):
...
self.assertNotIsInstance(None, tuple)
def test_is_none_a_list(self):
self.assertIsNone([])
and the terminal shows AssertionError
AssertionError: [] is not None
green: make it pass¶
when I change the method to match
def test_is_none_a_list(self):
self.assertIsNotNone([])
the test passes
refactor: make it better¶
I add another failing line
def test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNone([1, 2, 3, 'n'])
the terminal shows AssertionError
AssertionError: [1, 2, 3, 'n'] is not None
I change the method
def test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n'])
and the test passes
I add an instance test
def test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n']) self.assertNotIsInstance([], list)
and the terminal shows AssertionError
AssertionError: [] is an instance of <class 'list'>
[]
is the empty list. I change the methoddef test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n']) self.assertIsInstance([], list)
and the test passes
then I add another instance test
def test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n']) self.assertIsInstance([], list) self.assertNotIsInstance([1, 2, 3, 'n'], list)
and the terminal shows AssertionError
AssertionError: [1, 2, 3, 'n'] is an instance of <class 'list'>
[1, 2, 3, 'n']
is a list. I change the methoddef test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n']) self.assertIsInstance([], list) self.assertIsInstance([1, 2, 3, 'n'], list)
and the test passes
I add one more line
def test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n']) self.assertIsInstance([], list) self.assertIsInstance([1, 2, 3, 'n'], list) self.assertIsInstance(None, list)
and the terminal shows AssertionError
AssertionError: None is not an instance of <class 'list'>
I change the line to make the test pass
def test_is_none_a_list(self): self.assertIsNotNone([]) self.assertIsNotNone([1, 2, 3, 'n']) self.assertIsInstance([], list) self.assertIsInstance([1, 2, 3, 'n'], list) self.assertNotIsInstance(None, list)
then add a new note
# NOTES # None is NOT a list # None is NOT a tuple # None is NOT a string # None is NOT a float # None is NOT an integer # None is NOT a boolean # None is None
test_is_none_a_set¶
red: make it fail¶
I want to see if None is a set
def test_is_none_a_list(self):
...
self.assertNotIsInstance(None, list)
def test_is_none_a_set(self):
self.assertIsNone(set())
the terminal shows AssertionError
AssertionError: set() is not None
green: make it pass¶
I change the method to match the message
def test_is_none_a_set(self):
self.assertIsNotNone(set())
and the test passes
refactor: make it better¶
I add another line
def test_is_none_a_set(self): self.assertIsNotNone(set()) self.assertIsNone({1, 2, 3, 'n'})
the terminal shows AssertionError
AssertionError: {1, 2, 3, 'n'} is not None
I change the method
def test_is_none_a_set(self): self.assertIsNotNone(set()) self.assertIsNotNone({1, 2, 3, 'n'})
and the test passes
then I add an instance test
def test_is_none_a_set(self): self.assertIsNotNone(set()) self.assertIsNotNone({1, 2, 3, 'n'}) self.assertNotIsInstance({1, 2, 3, 'n'}, set)
and the terminal shows AssertionError
AssertionError: {1, 2, 3, 'n'} is an instance of <class 'set'>
I make the test pass
def test_is_none_a_set(self): self.assertIsNotNone(set()) self.assertIsNotNone({1, 2, 3, 'n'}) self.assertIsInstance({1, 2, 3, 'n'}, set)
then add another instance test
def test_is_none_a_set(self): self.assertIsNotNone(set()) self.assertIsNotNone({1, 2, 3, 'n'}) self.assertIsInstance({1, 2, 3, 'n'}, set) self.assertIsInstance(None, set)
which gives me AssertionError
AssertionError: None is not an instance of <class 'set'>
I change the method to make it pass
def test_is_none_a_set(self): self.assertIsNotNone(set()) self.assertIsNotNone({1, 2, 3, 'n'}) self.assertIsInstance({1, 2, 3, 'n'}, set) self.assertNotIsInstance(None, set)
then add a note
# NOTES # None is NOT a set # None is NOT a list # None is NOT a tuple # None is NOT a string # None is NOT a float # None is NOT an integer # None is NOT a boolean # None is None
test_is_none_a_dictionary¶
red: make it fail¶
One last test to see if None is a dictionary
def test_is_none_a_set(self):
...
self.assertNotIsInstance(None, set)
def test_is_none_a_dictionary(self):
self.assertIsNone(dict())
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 change the method
def test_is_none_a_dictionary(self):
self.assertIsNotNone(dict())
and the test passes
refactor: make it better¶
then I add another line
def test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNone({'key': 'value'})
and get AssertionError
AssertionError: {'key': 'value'} is not None
when I change the method
def test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'})
the terminal shows passing tests
I add an instance test
def test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'}) self.assertNotIsInstance({}, dict)
dict is the class for dictionaries, the terminal shows AssertionError
AssertionError: {} is an instance of <class 'dict'>
{}
is the empty dictionary. I change the method to make it passdef test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'}) self.assertIsInstance({}, dict)
then I add another instance test
def test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'}) self.assertIsInstance({}, dict) self.assertNotIsInstance({'key': 'value'}, dict)
and the terminal shows AssertionError
AssertionError: {'key': 'value'} is an instance of <class 'dict'>
{'key': 'value'}
is a dictionary with:
separating the key on the left from the value on the right, you can add more key-value pairs separating them with commas, that is the difference between dictionaries and sets, there is more on this in the dictionaries chapter. I change the methoddef test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'}) self.assertIsInstance({}, dict) self.assertIsInstance({'key': 'value'}, dict)
and the test passes
time for the last instance test
def test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'}) self.assertIsInstance({}, dict) self.assertIsInstance({'key': 'value'}, dict) self.assertIsInstance(None, dict)
the terminal shows AssertionError
AssertionError: None is not an instance of <class 'dict'>
I make the test pass
def test_is_none_a_dictionary(self): self.assertIsNotNone(dict()) self.assertIsNotNone({'key': 'value'}) self.assertIsInstance({}, dict) self.assertIsInstance({'key': 'value'}, dict) self.assertNotIsInstance(None, dict)
then I add the last note
# NOTES # None is NOT a dictionary # None is NOt a set # None is NOT a list # None is NOT a tuple # None is NOT a string # None is NOT a float # None is NOT an integer # None is NOT a boolean # None is None
review¶
I ran tests to show what None is and what it is not
Would you like to test booleans?