None


None is used when there is no value. It is the simplest data structure in Python

requirements

  • 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/directories 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) or option (mac) on the keyboard and use the mouse to click on tests/test_none.py:7 to open it in the editor

  • then I change True to False to make the test pass

    7        self.assertFalse(False)
    
  • I change the name of the class to match the CapWords format

    4class TestNone(unittest.TestCase):
    

test_what_is_none

red: make it fail

  • I change test_failure to test_what_is_none

    4class TestNone(unittest.TestCase):
    5
    6    def test_what_is_none(self):
    7        self.assertIsNotNone(None)
    

    the terminal shows AssertionError

    AssertionError: unexpectedly None
    

    assertIsNotNone checks that what is in the parentheses is NOT None

green: make it pass

  • I change the assert method to assertIsNone, which checks if what it gets in parentheses is None

     7        self.assertIsNone(None)
     8
     9
    10# NOTES
    

    the test passes

  • I add a note

    10# NOTES
    11# None is None
    12
    13
    14# Exceptions Encountered
    15# AssertionError
    

test_is_none_a_boolean

red: make it fail

I add another failing test to see if None is a boolean

 7        self.assertIsNone(None)
 8
 9    def test_is_none_a_boolean(self):
10        self.assertIsNone(False)
11
12
13# NOTES

the terminal shows AssertionError

AssertionError: False is not None

green: make it pass

I add a note

13# NOTES
14# False is NOT None
15# None is None

I make the line True with assertIsNotNone

10        self.assertIsNotNone(False)

the test passes

refactor: make it better

  • I add another failing assertion for the other boolean

    10        self.assertIsNotNone(False)
    11        self.assertIsNone(True)
    

    the terminal shows AssertionError

    AssertionError: True is not None
    
  • I add a note

    14# NOTES
    15# True is NOT None
    16# False is NOT None
    17# None is None
    
  • I make the line True with assertIsNotNone

    11        self.assertIsNotNone(True)
    

    the test passes

  • The unittest.TestCase class has 2 methods I can use to test if an object is a child/instance of a class or not. I add assertNotIsInstance

    11        self.assertIsNotNone(True)
    12        self.assertNotIsInstance(False, bool)
    

    assertNotIsInstance checks if the first item (False) it is given is NOT a child/instance of the second item (the bool class). It is like asking the question is False NOT a child of the bool class?. bool is the class for booleans. The terminal shows AssertionError

    AssertionError: False is an instance of <class 'bool'>
    

    False is a boolean

  • I make the statement True with the assertIsInstance method which checks if the first item (False) it is given is a child/instance of the second item (the bool class)

    12        self.assertIsInstance(False, bool)
    

    the test passes

  • I add a failing line for the other boolean with assertNotIsInstance

    12        self.assertIsInstance(False, bool)
    13        self.assertNotIsInstance(True, bool)
    

    the terminal shows AssertionError

    AssertionError: True is an instance of <class 'bool'>
    

    True is a boolean

  • 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

    13        self.assertIsInstance(True, bool)
    14        self.assertIsInstance(None, bool)
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'bool'>
    

    None is NOT a boolean

  • 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# NOTES
    

    the test passes

  • I change the last 2 notes I added

    17# NOTES
    18# None is NOT a boolean
    19# None is None
    

test_is_none_an_integer

red: make it fail

I add a test to see if None is an integer (a whole number)

14        self.assertNotIsInstance(None, bool)
15
16    def test_is_none_an_integer(self):
17        self.assertIsNone(-1)

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

    17        self.assertIsNotNone(-1)
    18        self.assertIsNone(0)
    

    the terminal shows AssertionError

    AssertionError: 0 is not None
    

    I make the statement True with assertIsNotNone

    18        self.assertIsNotNone(0)
    

    the test passes

  • I add another assertion

    18        self.assertIsNotNone(0)
    19        self.assertIsNone(1)
    

    the terminal shows AssertionError

    AssertionError: 1 is not None
    

    I change the line to make it True

    19        self.assertIsNotNone(1)
    

    the test passes

  • I add a new failing line with assertNotIsInstance

    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 -1 for all the integers that are smaller than 0

  • I make the line True with assertIsInstance

    20        self.assertIsInstance(-1, int)
    

    the test passes

  • I add another failing line with assertNotIsInstance

    20        self.assertIsInstance(-1, int)
    21        self.assertNotIsInstance(0, int)
    

    the terminal shows AssertionError

    AssertionError: 0 is an instance of <class 'int'>
    

    0 is an integer

  • I change the statement to make it True

    21        self.assertIsInstance(0, int)
    

    the test passes

  • I add another failing line with assertNotIsInstance

    21        self.assertIsInstance(0, int)
    22        self.assertNotIsInstance(1, int)
    

    the terminal shows AssertionError

    AssertionError: 1 is an instance of <class 'int'>
    

    I use 1 for all the integers that are bigger than 0

  • I make the failing line True with assertIsInstance

    22        self.assertIsInstance(1, int)
    

    the test passes

  • I add one more failing line with assertIsInstance

    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# NOTES
    

    the test passes

  • I add a new note

    26# NOTES
    27# None is NOT an integer
    28# None is NOT a boolean
    29# None is None
    

test_is_none_a_float

red: make it fail

I add a test to see if None is a float (floating point decimal number)

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

    26        self.assertIsNotNone(-0.1)
    27        self.assertIsNone(0.0)
    

    the terminal shows AssertionError

    AssertionError: 0.0 is not None
    

    I make the statement True with assertIsNotNone

    27        self.assertIsNotNone(0.0)
    

    the test passes

  • I add a failing line with assertIsNone

    27        self.assertIsNotNone(0.0)
    28        self.assertIsNone(0.1)
    

    the terminal shows AssertionError

    AssertionError: 0.1 is not None
    

    I 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

    28        self.assertIsNotNone(0.1)
    29        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'>
    

    I use -0.1 for all the floating point numbers that are smaller than 0.0

  • I make the statement True with assertIsInstance

    29        self.assertIsInstance(-0.1, float)
    

    the test passes

  • I add the next instance test with assertNotIsInstance

    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.0 is a floating point number

  • I make the statement True with assertIsInstance

    30        self.assertIsInstance(0.0, float)
    

    the test passes

  • I add a failing line with assertNotIsInstance

    30        self.assertIsInstance(0.0, float)
    31        self.assertNotIsInstance(0.1, float)
    

    the terminal shows AssertionError

    AssertionError: 0.1 is an instance of <class 'float'>
    

    I use 0.1 for all the floating point numbers that are bigger than 0.0

  • I make the statement True with assertIsInstance

    31        self.assertIsInstance(0.1, float)
    

    the test passes

  • I add one more failing line with assertIsInstance

    31        self.assertIsInstance(0.1, float)
    32        self.assertIsInstance(None, float)
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'float'>
    

    I make the statement True 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.assertIsInstance(0.0, float)
    31        self.assertIsInstance(0.1, float)
    32        self.assertNotIsInstance(None, float)
    33
    34
    35# NOTES
    

    the test passes

  • I add a new note

    35# NOTES
    36# None is NOT a float
    37# None is NOT an integer
    38# None is NOT a boolean
    39# None is None
    

test_is_none_a_string

red: make it fail

I add a test to see if None is a string (anything in quotes)

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

the empty string ('') 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

    35        self.assertIsNotNone('')
    36        self.assertIsNone("text")
    

    the terminal shows AssertionError

    AssertionError: 'text' is not None
    

    I change the assert method to make the statement True

    36        self.assertIsNotNone("text")
    

    the test passes

  • I add a failing line with assertNotIsInstance

    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'>
    

    because anything in quotes is a string

  • I change the assert method

    37        self.assertIsInstance('', str)
    

    the test passes

  • I add another assertion

    36        self.assertIsInstance('', str)
    37        self.assertNotIsInstance("text", str)
    

    the terminal shows AssertionError

    AssertionError: 'text' is an instance of <class 'str'>
    

    because anything in quotes is a string

  • I change the assert method

    38        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 another failing line with assertIsInstance

    38        self.assertIsInstance("text", str)
    39        self.assertIsInstance(None, str)
    

    the 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# NOTES
    

    the test passes

  • I add a note

    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
    

test_is_none_a_tuple

red: make it fail

I add a test to see if None is a tuple (anything in parentheses ())

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        self.assertIsNotNone(())
    43        self.assertIsNone((1, 2, 3, 'n'))
    

    the terminal shows AssertionError

    AssertionError: (1, 2, 3, 'n') is not None
    

    I make the statement True

    43        self.assertIsNotNone((1, 2, 3, 'n'))
    

    the test passes

  • I add a failing line with assertNotIsInstance

    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 () in Python is a tuple

  • I make the statement True

    44        self.assertIsInstance((), tuple)
    

    the test passes

  • I add another failing line

    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 () in Python is a tuple

  • I 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

    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# NOTES
    

    the test passes

  • I add a note

    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
    

    it looks like None is None and not anything else


test_is_none_a_list

red: make it fail

I add a new test for lists

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

    49        self.assertIsNotNone([])
    50        self.assertIsNone([1, 2, 3, 'n'])
    

    the terminal shows AssertionError

    AssertionError: [1, 2, 3, 'n'] is not None
    

    I make the statement True

    50        self.assertIsNotNone([1, 2, 3, 'n'])
    

    the test passes

  • I add a failing instance test

    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 list

  • I make the statement True

    51        self.assertIsInstance([], list)
    

    the test passes

  • I add another failing line with assertNotIsInstance

    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 list

  • I 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

    52    self.assertIsInstance([1, 2, 3, 'n'], list)
    53    self.assertIsInstance(None, list)
    

    the 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# NOTES
    

    the test passes

  • I 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

53        self.assertNotIsInstance(None, list)
54
55    def test_is_none_a_set(self):
56        self.assertIsNone(set())

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

    56        self.assertIsNotNone(set())
    57        self.assertIsNone({1, 2, 3, 'n'})
    

    the terminal shows AssertionError

    AssertionError: {1, 2, 3, 'n'} is not None
    

    I make the statement True

    57        self.assertIsNotNone({1, 2, 3, 'n'})
    

    the test passes

  • I add an instance test

    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

    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# NOTES
    

    the test passes

  • I add a note

    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 to see if None is a dictionary

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

    62        self.assertIsNotNone(dict())
    63        self.assertIsNone({'key': 'value'})
    

    the terminal shows AssertionError

    AssertionError: {'key': 'value'} is not None
    

    I make the statement True

    63        self.assertIsNotNone({'key': 'value'})
    

    the test passes

  • I add a failing instance test

    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 dictionary

  • I change the assert method

    64        self.assertIsInstance({}, dict)
    

    the test passes

  • I add another instance test

    64        self.assertIsInstance({}, dict)
    65        self.assertNotIsInstance({'key': 'value'}, dict)
    

    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, I can add more key-value pairs separating them with commas. 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

    65        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# NOTES
    

    the test passes

  • 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 what is True and False?


Click Here for the code from this chapter