None


what is None?

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

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

preview

In AssertionError, I used assertIsNone and assertIsNotNone to test_assertion_error_w_none, that experiment showed that

Here 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

  • then I make a directory for the project

    mkdir none
    

    the terminal goes back to the command line

    .../pumping_python
    
  • I change directory to the project

    cd none
    

    the terminal shows I am now in the none folder

    .../pumping_python/none
    
  • I make a folder for the source code

    mkdir src
    

    the terminal goes back to the command line

    .../pumping_python/none
    
  • I use touch to make an empty file for the program in the src folder

    touch src/none.py
    

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

    New-Item src/none.py
    

    the terminal goes back to the command line

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

    mkdir tests
    

    the terminal goes back to the command line

  • I use touch to make an empty file in the tests folder to tell Python that it is a Python package

    Attention

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

    touch tests/__init__.py
    

    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 an empty file for the actual test

    touch tests/test_none.py
    

    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 open a file from the terminal in Visual Studio Code by typing code and the name of the file with

    code tests/test_none.py
    

    test_none.py opens up 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 virtual environment in the terminal

    python3 -m venv .venv
    

    on Windows without Windows Subsystem for Linux use python3 -m venv .venv instead of python3 -m venv .venv

    python -m venv .venv
    

    the terminal takes some time then goes back to the command line

  • I activate the virtual environment

    source .venv/bin/activate
    

    on Windows without Windows Subsystem for Linux use .venv/bin/activate.ps1 instead of source .venv/bin/activate

    .venv/scripts/activate.ps1
    

    the terminal shows

    (.venv) .../pumping_python/none
    
  • I upgrade the Python package manager (pip) to the latest version

    python3 -m pip install --upgrade pip
    

    the terminal shows pip being uninstalled then installs the latest version or shows that it is already the latest version

  • I make a requirements.txt file for the Python programs my project needs

    echo "pytest-watch" > requirements.txt
    

    the terminal goes back to the command line

  • I use pip to use the requirements file to install pytest-watch

    python3 -m pip install --requirement requirements.txt
    

    on Windows without Windows Subsystem for Linux use python -m pip install --requirement requirements.txt instead of python3 -m pip install --requirement requirements.txt

    python -m pip install --requirement requirements.txt
    

    the terminal shows pip downloads and installs the Python programs that pytest-watch needs to run

  • I use pytest-watch to run the test

    pytest-watch
    

    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 hold ctrl (Windows/Linux) or option or command (MacOS) on the keyboard and use the mouse to click on tests/test_none.py:7 to open it in the editor

  • I add AssertionError to the list of Exceptions seen in test_none.py

     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

We need a way to represent nothing or the absence of a 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

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

7        self.assertIsNone(None)

the test passes

REFACTOR: make it better

I add a note

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

so far this is a repetition of 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 make the assertion True with the assertIsNotNone method

10        self.assertIsNotNone(False)

the test passes

REFACTOR: make it better

  • I add a note

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

    Still a repetition of what I learned in AssertionError

  • 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 make the assertion True with assertIsNotNone

    11        self.assertIsNotNone(True)
    

    the test passes

  • I add a note

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

    more repetition

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

    11        self.assertIsNotNone(True)
    12        self.assertNotIsInstance(False, bool)
    
    • assertNotIsInstance checks if the first item it is given is NOT a child/instance of the second item. It is like asking the question is False NOT a child of the bool class?. Okay, this is new

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

    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

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

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

I know two new assert methods

Caution

the naming 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)

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

    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 (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 with to test if None is an integer 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 the assertIsInstance method

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

    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)
    

    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 (())), pronounced two-pull

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 to see if None is a list (anything in square brackets ([]))

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

    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
    

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({'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 note

    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
    

close the project

  • I close the file(s) I have open in the editor(s)

  • I click in the terminal and exit the tests with ctrl+c on the keyboard

  • I deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line, (.venv) is no longer on the left side

    .../pumping_python/none
    
  • 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 that were say_hellod in AssertionError

and 2 new assert methods

I also showed the basic Python data structures

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?


please leave a review