what is None?


None is used when there is no value. In Mathematics we use 0 to represent no quantity. In some languages or domains we use NULL, in forms we use N/A when the options do not apply. In Python we use None, It is the simplest data structure.

In AssertionError, I used assertIsNone and assertIsNotNone in test_assertion_error_w_none, where I saw that

I want to use assert methods to compare None with the other Python data structures to see what it is and what it is not


preview

These are the tests I have by the end of the chapter

 1import unittest
 2
 3
 4class TestNone(unittest.TestCase):
 5
 6    def test_what_is_none(self):
 7        self.assertIsNone(None)
 8
 9    def test_is_none_a_boolean(self):
10        self.assertIsNotNone(False)
11        self.assertIsNotNone(True)
12        self.assertIsInstance(False, bool)
13        self.assertIsInstance(True, bool)
14        self.assertNotIsInstance(None, bool)
15
16    def test_is_none_an_integer(self):
17        self.assertIsNotNone(-1)
18        self.assertIsNotNone(0)
19        self.assertIsNotNone(1)
20        self.assertIsInstance(-1, int)
21        self.assertIsInstance(0, int)
22        self.assertIsInstance(1, int)
23        self.assertNotIsInstance(None, int)
24
25    def test_is_none_a_float(self):
26        self.assertIsNotNone(-0.1)
27        self.assertIsNotNone(0.0)
28        self.assertIsNotNone(0.1)
29        self.assertIsInstance(-0.1, float)
30        self.assertIsInstance(0.0, float)
31        self.assertIsInstance(0.1, float)
32        self.assertNotIsInstance(None, float)
33
34    def test_is_none_a_string(self):
35        self.assertIsNotNone('')
36        self.assertIsNotNone("text")
37        self.assertIsInstance('', str)
38        self.assertIsInstance("text", str)
39        self.assertNotIsInstance(None, str)
40
41    def test_is_none_a_tuple(self):
42        self.assertIsNotNone(())
43        self.assertIsNotNone((1, 2, 3, 'n'))
44        self.assertIsInstance((), tuple)
45        self.assertIsInstance((1, 2, 3, 'n'), tuple)
46        self.assertNotIsInstance(None, tuple)
47
48    def test_is_none_a_list(self):
49        self.assertIsNotNone([])
50        self.assertIsNotNone([1, 2, 3, 'n'])
51        self.assertIsInstance([], list)
52        self.assertIsInstance([1, 2, 3, 'n'], list)
53        self.assertNotIsInstance(None, list)
54
55    def test_is_none_a_set(self):
56        self.assertIsNotNone(set())
57        self.assertIsNotNone({1, 2, 3, 'n'})
58        self.assertIsInstance({1, 2, 3, 'n'}, set)
59        self.assertNotIsInstance(None, set)
60
61    def test_is_none_a_dictionary(self):
62        self.assertIsNotNone(dict())
63        self.assertIsNotNone({'key': 'value'})
64        self.assertIsInstance({}, dict)
65        self.assertIsInstance({'key': 'value'}, dict)
66        self.assertNotIsInstance(None, dict)
67
68
69# NOTES
70# None is NOT a dictionary
71# None is NOT a set
72# None is NOT a list
73# None is NOT a tuple
74# None is NOT a string
75# None is NOT a float
76# None is NOT an integer
77# None is NOT a boolean
78# None is None
79
80
81# Exceptions seen
82# AssertionError

questions about None

Here are questions you can answer after going through this chapter


requirements

  • I name this project none

  • I open a terminal

  • I make a directory for the project

    mkdir none
    

    the terminal goes back to the command line

  • I change directory to the project

    cd none
    

    the terminal shows I am in the none folder

    .../pumping_python/none
    
  • I make a directory for the tests

    mkdir tests
    

    the terminal goes back to the command line

  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    touch tests/__init__.py
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item tests/__init__.py instead of touch tests/__init__.py

    New-Item tests/__init__.py
    

    the terminal goes back to the command line

  • I make a Python file for the tests in the tests directory

    touch tests/test_none.py
    

    Note

    on Windows without Windows Subsystem for Linux use New-Item tests/test_none.py instead of touch tests/test_none.py

    New-Item tests/test_none.py
    

    the terminal goes back to the command line

  • I open test_none.py in the editor of the Integrated Development Environment (IDE)

    Tip

    I can use the terminal to open a file in the Integrated Development Environment (IDE) by typing the name of the program and the name of the file. That means when I type this in the terminal

    code tests/test_none.py
    

    Visual Studio Code opens test_none.py in the editor

  • I add the first failing test to test_none.py

    1import unittest
    2
    3
    4class TestNone(unittest.TestCase):
    5
    6    def test_failure(self):
    7        self.assertFalse(True)
    
  • I make a requirements file for the Python packages I need, in the terminal

    echo "pytest" > requirements.txt
    

    the terminal goes back to the command line

  • I add pytest-watcher to the file

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line

  • I setup the project with uv

    uv init
    

    the terminal shows

    Initialized project `none`
    

    then goes back to the command line

  • I remove main.py from the project because I do not use it

    rm main.py
    

    the terminal goes back to the command line

  • I install the Python packages I gave in the requirements file

    uv add --requirement requirements.txt
    

    the terminal shows it installed the Python packages

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal shows

    ================================ FAILURES ================================
    _________________________ TestNone.test_failure __________________________
    
    self = <tests.test_none.TestNone testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_none.py:7: AssertionError
    ======================== short test summary info =========================
    FAILED tests/test_none.py::TestNone::test_failure - AssertionError: True is not false
    =========================== 1 failed in X.YZs ============================
    
  • I add AssertionError to the list of Exceptions seen in test_none.py in the editor

     4class TestNone(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(True)
     8
     9
    10# Exceptions seen
    11# AssertionError
    
  • then 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_failure to test_what_is_none

     4class TestNone(unittest.TestCase):
     5
     6    def test_what_is_none(self):
     7        self.assertIsNotNone(None)
     8
     9
    10# Exceptions seen
    

    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 assertIsNotNone to assertIsNone, which checks if what it gets in parentheses is None

7        self.assertIsNone(None)

the test passes because None is None


REFACTOR: make it better


I add a comment

 4class TestNone(unittest.TestCase):
 5
 6    def test_what_is_none(self):
 7        self.assertIsNone(None)
 8
 9
10# NOTES
11# None is None
12
13
14# Exceptions seen
15# AssertionError

this is the same comment from the AssertionError chapter

None is None


test_is_none_a_boolean


RED: make it fail


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

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

the terminal shows AssertionError

AssertionError: False is not None

GREEN: make it pass


I make the assertion True with the assertIsNotNone method

 9    def test_is_none_a_boolean(self):
10        self.assertIsNotNone(False)
11
12
13# NOTES

the test passes


REFACTOR: make it better


  • I add a comment

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

    another comment from the AssertionError chapter

  • I add an assertion for the other boolean

     9    def test_is_none_a_boolean(self):
    10        self.assertIsNotNone(False)
    11        self.assertIsNone(True)
    12
    13
    14# NOTES
    

    the terminal shows AssertionError

    AssertionError: True is not None
    
  • I make the assertion True with assertIsNotNone

     9    def test_is_none_a_boolean(self):
    10        self.assertIsNotNone(False)
    11        self.assertIsNotNone(True)
    12
    13
    14# NOTES
    

    the test passes

  • I add a comment

    14# NOTES
    15# True is NOT None
    16# False is NOT None
    17# None is None
    

    another comment from the AssertionError chapter


how to test if something is an instance of a class

The unittest.TestCase class has 2 methods I can use to test if an object is a child (instance) of a class or not - assertIsInstance and assertNotIsInstance

  • I add the assertNotIsInstance method to test if False is a boolean

     9    def test_is_none_a_boolean(self):
    10        self.assertIsNotNone(False)
    11        self.assertIsNotNone(True)
    12        self.assertNotIsInstance(False, bool)
    13
    14
    15# NOTES
    

    the terminal shows AssertionError

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

    False is a boolean

  • I make the assertion True with the assertIsInstance method which checks if the first item it is given is a child (instance) of the second item. It is like asking the question "is False a child of the bool class?"

    12        self.assertIsInstance(False, bool)
    

    the test passes

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

     9    def test_is_none_a_boolean(self):
    10        self.assertIsNotNone(False)
    11        self.assertIsNotNone(True)
    12        self.assertIsInstance(False, bool)
    13        self.assertNotIsInstance(True, bool)
    14
    15
    16# NOTES
    

    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

     9    def test_is_none_a_boolean(self):
    10        self.assertIsNotNone(False)
    11        self.assertIsNotNone(True)
    12        self.assertIsInstance(False, bool)
    13        self.assertIsInstance(True, bool)
    14        self.assertIsInstance(None, bool)
    15
    16
    17# NOTES
    

    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

  • Since this is about None, I change the last 2 comments I added

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

None is NOT a boolean

I know two new assert methods

Caution

the names of the assert methods can be confusing, there is

where Not comes after Is and then there is

where Is comes after Not

Maybe assertIsNotInstance would have been better, since assertNotIsNone does not sound better than assertIsNotNone.

Naming things is its own challenge


test_is_none_an_integer


RED: make it fail


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

 9    def test_is_none_a_boolean(self):
10        self.assertIsNotNone(False)
11        self.assertIsNotNone(True)
12        self.assertIsInstance(False, bool)
13        self.assertIsInstance(True, bool)
14        self.assertNotIsInstance(None, bool)
15
16    def test_is_none_an_integer(self):
17        self.assertIsNone(-1)
18
19
20# NOTES

the terminal shows AssertionError

AssertionError: -1 is not None

GREEN: make it pass


I make the statement True with assertIsNotNone

17        self.assertIsNotNone(-1)

the test passes


REFACTOR: make it better


  • I add a new failing line with assertIsNone

    16    def test_is_none_an_integer(self):
    17        self.assertIsNotNone(-1)
    18        self.assertIsNone(0)
    

    the terminal shows AssertionError

    AssertionError: 0 is not None
    
  • I make the statement True with assertIsNotNone

    18        self.assertIsNotNone(0)
    

    the test passes

  • I add another assertion

    16    def test_is_none_an_integer(self):
    17        self.assertIsNotNone(-1)
    18        self.assertIsNotNone(0)
    19        self.assertIsNone(1)
    

    the terminal shows AssertionError

    AssertionError: 1 is not None
    
  • I change the line to make it True

    19        self.assertIsNotNone(1)
    

    the test passes

  • I add a new failing line with assertNotIsInstance

    16    def test_is_none_an_integer(self):
    17        self.assertIsNotNone(-1)
    18        self.assertIsNotNone(0)
    19        self.assertIsNotNone(1)
    20        self.assertNotIsInstance(-1, int)
    

    int is the class for integers, the terminal shows AssertionError

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

    I use -1 for all the integers (whole numbers) 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

    16    def test_is_none_an_integer(self):
    17        self.assertIsNotNone(-1)
    18        self.assertIsNotNone(0)
    19        self.assertIsNotNone(1)
    20        self.assertIsInstance(-1, int)
    21        self.assertNotIsInstance(0, int)
    

    the terminal shows AssertionError

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

    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

    20        self.assertIsInstance(-1, int)
    21        self.assertIsInstance(0, int)
    22        self.assertNotIsInstance(1, int)
    23
    24
    25# NOTES
    26# None is NOT a boolean
    27# None is None
    

    the terminal shows AssertionError

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

    I use 1 for all the integers (whole numbers) 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 to test if None is an integer with assertIsInstance

    20        self.assertIsInstance(-1, int)
    21        self.assertIsInstance(0, int)
    22        self.assertIsInstance(1, int)
    23        self.assertIsInstance(None, int)
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'int'>
    
  • I make the line True with assertNotIsInstance

    16    def test_is_none_an_integer(self):
    17        self.assertIsNotNone(-1)
    18        self.assertIsNotNone(0)
    19        self.assertIsNotNone(1)
    20        self.assertIsInstance(-1, int)
    21        self.assertIsInstance(0, int)
    22        self.assertIsInstance(1, int)
    23        self.assertNotIsInstance(None, int)
    24
    25
    26# NOTES
    

    the test passes

  • I add a new comment

    26# NOTES
    27# None is NOT an integer
    28# None is NOT a boolean
    29# None is None
    30
    31
    32# Exceptions seen
    33# AssertionError
    

None is NOT an integer


test_is_none_a_float


RED: make it fail


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

16    def test_is_none_an_integer(self):
17        self.assertIsNotNone(-1)
18        self.assertIsNotNone(0)
19        self.assertIsNotNone(1)
20        self.assertIsInstance(-1, int)
21        self.assertIsInstance(0, int)
22        self.assertIsInstance(1, int)
23        self.assertNotIsInstance(None, int)
24
25    def test_is_none_a_float(self):
26        self.assertIsNone(-0.1)
27
28
29# NOTES

the terminal shows AssertionError

AssertionError: -0.1 is not None

GREEN: make it pass


I make the statement True with assertIsNotNone

26        self.assertIsNotNone(-0.1)

the test passes


REFACTOR: make it better


  • I add another failing line with assertIsNone

    25    def test_is_none_a_float(self):
    26        self.assertIsNotNone(-0.1)
    27        self.assertIsNone(0.0)
    

    the terminal shows AssertionError

    AssertionError: 0.0 is not None
    
  • I make the statement True with assertIsNotNone

    27        self.assertIsNotNone(0.0)
    

    the test passes

  • I add a failing line with assertIsNone

    25    def test_is_none_a_float(self):
    26        self.assertIsNotNone(-0.1)
    27        self.assertIsNotNone(0.0)
    28        self.assertIsNone(0.1)
    

    the terminal shows AssertionError

    AssertionError: 0.1 is not 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

    25    def test_is_none_a_float(self):
    26        self.assertIsNotNone(-0.1)
    27        self.assertIsNotNone(0.0)
    28        self.assertIsNotNone(0.1)
    29        self.assertNotIsInstance(-0.1, float)
    

    float is the class for binary floating point numbers. The terminal shows AssertionError

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

    I use -0.1 for all the binary 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

    25    def test_is_none_a_float(self):
    26        self.assertIsNotNone(-0.1)
    27        self.assertIsNotNone(0.0)
    28        self.assertIsNotNone(0.1)
    29        self.assertIsInstance(-0.1, float)
    30        self.assertNotIsInstance(0.0, float)
    

    the terminal shows AssertionError

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

    0.0 is a binary 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

    29        self.assertIsInstance(-0.1, float)
    30        self.assertIsInstance(0.0, float)
    31        self.assertNotIsInstance(0.1, float)
    32
    33
    34# NOTES
    

    the terminal shows AssertionError

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

    I use 0.1 for all the binary 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 the assertIsInstance method

    29        self.assertIsInstance(-0.1, float)
    30        self.assertIsInstance(0.0, float)
    31        self.assertIsInstance(0.1, float)
    32        self.assertIsInstance(None, float)
    33
    34
    35# NOTES
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'float'>
    
  • I make the statement True with the assertNotIsInstance method

    25    def test_is_none_a_float(self):
    26        self.assertIsNotNone(-0.1)
    27        self.assertIsNotNone(0.0)
    28        self.assertIsNotNone(0.1)
    29        self.assertIsInstance(-0.1, float)
    30        self.assertIsInstance(0.0, float)
    31        self.assertIsInstance(0.1, float)
    32        self.assertNotIsInstance(None, float)
    33
    34
    35# NOTES
    

    the test passes

  • I add a new comment

    35# NOTES
    36# None is NOT a float
    37# None is NOT an integer
    38# None is NOT a boolean
    39# None is None
    40
    41
    42# Exceptions seen
    43# AssertionError
    

None is NOT a float


test_is_none_a_string


RED: make it fail


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

25    def test_is_none_a_float(self):
26        self.assertIsNotNone(-0.1)
27        self.assertIsNotNone(0.0)
28        self.assertIsNotNone(0.1)
29        self.assertIsInstance(-0.1, float)
30        self.assertIsInstance(0.0, float)
31        self.assertIsInstance(0.1, float)
32        self.assertNotIsInstance(None, float)
33
34    def test_is_none_a_string(self):
35        self.assertIsNone('')
36
37
38# NOTES

the terminal shows AssertionError

AssertionError: '' is not None

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

    34    def test_is_none_a_string(self):
    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

    34    def test_is_none_a_string(self):
    35        self.assertIsNotNone('')
    36        self.assertIsNotNone("text")
    37        self.assertNotIsInstance('', str)
    

    str is the class for strings. The terminal shows AssertionError

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

    because anything in quotes is a string

  • I change the assert method

    37        self.assertIsInstance('', str)
    

    the test passes

  • I add another assertion

    34    def test_is_none_a_string(self):
    35        self.assertIsNotNone('')
    36        self.assertIsNotNone("text")
    37        self.assertIsInstance('', str)
    38        self.assertNotIsInstance("text", str)
    

    the terminal shows AssertionError

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

    because anything in quotes is a string

  • I change the assert method

    38        self.assertIsInstance("text", str)
    

    the test passes

  • I add another failing line with assertIsInstance

    37        self.assertIsInstance('', str)
    38        self.assertIsInstance("text", str)
    39        self.assertIsInstance(None, str)
    40
    41
    42# NOTES
    

    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 comment

    42# NOTES
    43# None is NOT a string
    44# None is NOT a float
    45# None is NOT an integer
    46# None is NOT a boolean
    47# None is None
    48
    49
    50# Exceptions seen
    51# AssertionError
    

None is NOT a string


test_is_none_a_tuple


RED: make it fail


I add a test to see if None is a tuple (anything in parentheses (()) separated by commas), pronounced too-pull

34    def test_is_none_a_string(self):
35        self.assertIsNotNone('')
36        self.assertIsNotNone("text")
37        self.assertIsInstance('', str)
38        self.assertIsInstance("text", str)
39        self.assertNotIsInstance(None, str)
40
41    def test_is_none_a_tuple(self):
42        self.assertIsNone(())
43
44
45# NOTES

the terminal shows AssertionError

AssertionError: () is not None

GREEN: make it pass


I make the statement True

42        self.assertIsNotNone(())

the test passes


REFACTOR: make it better


  • I add a failing assertion

    42    def test_is_none_a_tuple(self):
    43        self.assertIsNotNone(())
    44        self.assertIsNone((1, 2, 3, 'n'))
    

    the terminal shows AssertionError

    AssertionError: (1, 2, 3, 'n') is not None
    
  • I make the statement True

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

    the test passes

  • I add a failing line with assertNotIsInstance

    41    def test_is_none_a_tuple(self):
    42        self.assertIsNotNone(())
    43        self.assertIsNotNone((1, 2, 3, 'n'))
    44        self.assertNotIsInstance((), tuple)
    

    the terminal shows AssertionError

    AssertionError: () is an instance of <class 'tuple'>
    

    because anything in parentheses (()) separated by commas in Python is a tuple

  • I make the statement True

    44        self.assertIsInstance((), tuple)
    

    the test passes

  • I add another failing line

    41    def test_is_none_a_tuple(self):
    42        self.assertIsNotNone(())
    43        self.assertIsNotNone((1, 2, 3, 'n'))
    44        self.assertIsInstance((), tuple)
    45        self.assertNotIsInstance((1, 2, 3, 'n'), tuple)
    

    the terminal shows AssertionError

    AssertionError: (1, 2, 3, 'n') is an instance of <class 'tuple'>
    

    because anything in parentheses (()) separated by commas in Python is a 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

    41    def test_is_none_a_tuple(self):
    42        self.assertIsNotNone(())
    43        self.assertIsNotNone((1, 2, 3, 'n'))
    44        self.assertIsInstance((), tuple)
    45        self.assertIsInstance((1, 2, 3, 'n'), tuple)
    46        self.assertIsInstance(None, tuple)
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'tuple'>
    
  • I change the statement to make it True

    41    def test_is_none_a_tuple(self):
    42        self.assertIsNotNone(())
    43        self.assertIsNotNone((1, 2, 3, 'n'))
    44        self.assertIsInstance((), tuple)
    45        self.assertIsInstance((1, 2, 3, 'n'), tuple)
    46        self.assertNotIsInstance(None, tuple)
    47
    48
    49# NOTES
    

    the test passes

  • I add a comment

    49# NOTES
    50# None is NOT a tuple
    51# None is NOT a string
    52# None is NOT a float
    53# None is NOT an integer
    54# None is NOT a boolean
    55# None is None
    

    it looks like None is None and not anything else

None is NOT a tuple


test_is_none_a_list


RED: make it fail


I add a new test to see if None is a list (anything in square brackets ([]))

41    def test_is_none_a_tuple(self):
42        self.assertIsNotNone(())
43        self.assertIsNotNone((1, 2, 3, 'n'))
44        self.assertIsInstance((), tuple)
45        self.assertIsInstance((1, 2, 3, 'n'), tuple)
46        self.assertNotIsInstance(None, tuple)
47
48    def test_is_none_a_list(self):
49        self.assertIsNone([])
50
51
52# NOTES

the terminal shows AssertionError

AssertionError: [] is not None

GREEN: make it pass


I change the assertion to make the statement True

49        self.assertIsNotNone([])

the test passes


REFACTOR: make it better


  • I add another failing line

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

    the terminal shows AssertionError

    AssertionError: [1, 2, 3, 'n'] is not None
    
  • I make the statement True

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

    the test passes

  • I add a failing instance test

    48    def test_is_none_a_list(self):
    49        self.assertIsNotNone([])
    50        self.assertIsNotNone([1, 2, 3, 'n'])
    51        self.assertNotIsInstance([], list)
    

    the terminal shows AssertionError

    AssertionError: [] is an instance of <class 'list'>
    

    because anything in square brackets ([]) in Python is a list

  • I make the statement True

    51        self.assertIsInstance([], list)
    

    the test passes

  • I add another failing line with assertNotIsInstance

    48    def test_is_none_a_list(self):
    49        self.assertIsNotNone([])
    50        self.assertIsNotNone([1, 2, 3, 'n'])
    51        self.assertIsInstance([], list)
    52        self.assertNotIsInstance([1, 2, 3, 'n'], list)
    

    the terminal shows AssertionError

    AssertionError: [1, 2, 3, 'n'] is an instance of <class 'list'>
    

    because anything in square brackets ([]) in Python is a 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

    51        self.assertIsInstance([], list)
    52        self.assertIsInstance([1, 2, 3, 'n'], list)
    53        self.assertIsInstance(None, list)
    54
    55
    56# NOTES
    

    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 comment

    56# NOTES
    57# None is NOT a list
    58# None is NOT a tuple
    59# None is NOT a string
    60# None is NOT a float
    61# None is NOT an integer
    62# None is NOT a boolean
    63# None is None
    

None is NOT a list


test_is_none_a_set


RED: make it fail


I want to see if None is a set

48      def test_is_none_a_list(self):
49          self.assertIsNotNone([])
50          self.assertIsNotNone([1, 2, 3, 'n'])
51          self.assertIsInstance([], list)
52          self.assertIsInstance([1, 2, 3, 'n'], list)
53          self.assertNotIsInstance(None, list)
54
55      def test_is_none_a_set(self):
56          self.assertIsNone(set())
57
58
59  # NOTES

the terminal shows AssertionError

AssertionError: set() is not None

GREEN: make it pass


I make the statement True

56            self.assertIsNotNone(set())

the test passes


REFACTOR: make it better


  • I add another assertion

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

    the terminal shows AssertionError

    AssertionError: {1, 2, 3, 'n'} is not None
    
  • I make the statement True

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

    the test passes

  • I add an instance test

    55    def test_is_none_a_set(self):
    56        self.assertIsNotNone(set())
    57        self.assertIsNotNone({1, 2, 3, 'n'})
    58        self.assertNotIsInstance({1, 2, 3, 'n'}, set)
    

    the terminal shows AssertionError

    AssertionError: {1, 2, 3, 'n'} is an instance of <class 'set'>
    
  • I make the statement True

    58        self.assertIsInstance({1, 2, 3, 'n'}, set)
    

    the test passes

  • I add another instance test

    55    def test_is_none_a_set(self):
    56        self.assertIsNotNone(set())
    57        self.assertIsNotNone({1, 2, 3, 'n'})
    58        self.assertIsInstance({1, 2, 3, 'n'}, set)
    59        self.assertIsInstance(None, set)
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'set'>
    
  • I make the statement True

    55    def test_is_none_a_set(self):
    56        self.assertIsNotNone(set())
    57        self.assertIsNotNone({1, 2, 3, 'n'})
    58        self.assertIsInstance({1, 2, 3, 'n'}, set)
    59        self.assertNotIsInstance(None, set)
    60
    61
    62# NOTES
    

    the test passes

  • I add a comment

    62# NOTES
    63# None is NOT a set
    64# None is NOT a list
    65# None is NOT a tuple
    66# None is NOT a string
    67# None is NOT a float
    68# None is NOT an integer
    69# None is NOT a boolean
    70# None is None
    

None is NOT a set


test_is_none_a_dictionary


RED: make it fail


One last test, this one is to see if None is a dictionary

55    def test_is_none_a_set(self):
56        self.assertIsNotNone(set())
57        self.assertIsNotNone({1, 2, 3, 'n'})
58        self.assertIsInstance({1, 2, 3, 'n'}, set)
59        self.assertNotIsInstance(None, set)
60
61    def test_is_none_a_dictionary(self):
62        self.assertIsNone(dict())
63
64
65# NOTES

the terminal shows AssertionError

AssertionError: {} is not None

wait a minute! Python uses {} for sets. It also uses them for dictionaries with a difference.


GREEN: make it pass


I make the statement True

62        self.assertIsNotNone(dict())

the test passes


REFACTOR: make it better


  • I add another failing line

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

    the terminal shows AssertionError

    AssertionError: {'key': 'value'} is not None
    
  • I make the statement True

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

    the test passes

  • I add a failing instance test

    61    def test_is_none_a_dictionary(self):
    62        self.assertIsNotNone(dict())
    63        self.assertIsNotNone({'key': 'value'})
    64        self.assertNotIsInstance({}, dict)
    

    dict is the class for dictionaries, the terminal shows AssertionError

    AssertionError: {} is an instance of <class 'dict'>
    

    {} is the empty dictionary

  • I change the assert method

    64        self.assertIsInstance({}, dict)
    

    the test passes

  • I add another instance test

    61    def test_is_none_a_dictionary(self):
    62        self.assertIsNotNone(dict())
    63        self.assertIsNotNone({'key': 'value'})
    64        self.assertIsInstance({}, dict)
    65        self.assertNotIsInstance({'key': 'value'}, dict)
    

    the terminal shows AssertionError

    AssertionError: {'key': 'value'} is an instance of <class 'dict'>
    

    Note

    {'key': 'value'} is a dictionary with : separating the key on the left from the value on the right.

    I can add more key-value pairs separating them with commas, for example

    {
        'key': 'value',
        'another_key': 'another value',
        'one_more_key': 'one more value',
        'magic_key': 'magic value',
        ...
        'keyN': 'valueN',
    }
    

    sets do NOT have key-value pairs.

  • I make the statement True

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

    the test passes

  • I add the last failing instance test with assertIsInstance

    61    def test_is_none_a_dictionary(self):
    62        self.assertIsNotNone(dict())
    63        self.assertIsNotNone({'key': 'value'})
    64        self.assertIsInstance({}, dict)
    65        self.assertIsInstance({'key': 'value'}, dict)
    66        self.assertIsInstance(None, dict)
    

    the terminal shows AssertionError

    AssertionError: None is not an instance of <class 'dict'>
    
  • I make the statement True with assertNotIsInstance

    61    def test_is_none_a_dictionary(self):
    62        self.assertIsNotNone(dict())
    63        self.assertIsNotNone({'key': 'value'})
    64        self.assertIsInstance({}, dict)
    65        self.assertIsInstance({'key': 'value'}, dict)
    66        self.assertNotIsInstance(None, dict)
    67
    68
    69# NOTES
    

    the test passes

  • I add the last comment

    69# NOTES
    70# None is NOT a dictionary
    71# None is NOT a set
    72# None is NOT a list
    73# None is NOT a tuple
    74# None is NOT a string
    75# None is NOT a float
    76# None is NOT an integer
    77# None is NOT a boolean
    78# None is None
    79
    80
    81# Exceptions seen
    82# AssertionError
    

None is NOT a dictionary


close the project

  • I close test_none.py in the editor

  • I click in the terminal and use q on the keyboard to leave the tests and the terminal goes back to the command line

  • I change directory to the parent of none

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory


review

I used assert methods to test what None is and what it is NOT. I used 2 from the AssertionError chapter

and 2 new assert methods

I also used Python’s basic data structures in the tests

How many questions can you answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

so far you have covered

Would you like to test what is True and False in Python?


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