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 can use None, it is the simplest object.

I used assertIs and assertIsNot in test_assertion_error_w_none in the assertion_error project, where I saw that

I want to use assert methods to compare None with the other Python basic objects to see what it is and what it is not.


preview

I have these tests by the end of the chapter

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

questions about None

Questions to think about as I go through the chapter


start the project

  • I name this project none

  • I open a terminal

  • I change directory to the none folder in the pumping_python folder

    cd none
    

    the terminal shows

    cd: no such file or directory: none
    
  • I use uv to make a directory for the project and initialize it

    uv init none
    

    the terminal shows

    Initialized project `none`
    at `.../pumping_python/none`
    
  • I change directory to none

    cd none
    
  • I make a directory for the tests

    mkdir tests
    
  • 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
    
    New-Item tests/__init__.py
    
  • I use the mv program to change the name of main.py to test_none.py and move it to the tests folder

    mv main.py tests/test_none.py
    
    Move-Item main.py tests/test_none.py
    
  • I open test_none.py

  • I delete the text in the file then 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.assertEqual(False, True)
    
  • I go back to the terminal to make a requirements file for the Python packages I need

    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    echo "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    uv add --requirement requirements.txt
    

    the terminal shows that it installed pytest-watcher and its dependencies.

  • I add the new files and folder to git for tracking

    git add .
    
  • I add a git commit message

    git commit -am 'setup project'
    
  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES ========================
    _________________ TestNone.test_failure __________________
    
    self = <tests.test_none.TestNone testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertEqual(False, True)
    E       AssertionError: False != True
    
    tests/test_none.py:7: AssertionError
    ================ short test summary info =================
    FAILED tests/test_none.py::TestNone::test_failure - AssertionError: False != True
    =================== 1 failed in X.YZs ====================
    

    because True is NOT equal to False.

    if the terminal does not show the same error, then check

    • if your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • if you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now again

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

     4class TestNone(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertEqual(False, True)
     8
     9
    10# Exceptions seen
    11# AssertionError
    
  • I change True to False in the assertion

     4class TestNone(unittest.TestCase):
     5
     6    def test_failure(self):
     7        # self.assertEqual(False, True)
     8        self.assertEqual(False, False)
     9
    10
    11# Exceptions seen
    12# AssertionError
    

    the test passes.


test_what_is_none

RED: make it fail



GREEN: make it pass


I change assertIsNot to assertIs

 6    def test_what_is_none(self):
 7        # self.assertIsNot(None, None)
 8        self.assertIs(None, None)
 9
10
11# Exceptions seen

the test passes. So far this is the same as test_assertion_error_w_none.


another way to test if something is NOT None

I can also use another assert method from the unittest.TestCase class to test if something is NOT the same object as None - the assertIsNotNone method


RED: make it fail


I add an assertion with the assertIsNotNone method

 6    def test_what_is_none(self):
 7        # self.assertIsNot(None, None)
 8        self.assertIs(None, None)
 9        self.assertIsNotNone(None)
10
11
12# Exceptions seen

the terminal is my friend, and shows AssertionError

AssertionError: unexpectedly None

another way to test if something is None

I can use the assertIsNone method from the unittest.TestCase class to test if something is the same object as None


GREEN: make it pass


I change assertIsNotNone to the assertIsNone method

 6    def test_what_is_none(self):
 7        # self.assertIsNot(None, None)
 8        self.assertIs(None, None)
 9        # self.assertIsNotNone(None)
10        self.assertIsNone(None)
11
12
13# Exceptions seen

REFACTOR: make it better


  • I remove the commented lines

     4class TestNone(unittest.TestCase):
     5
     6    def test_what_is_none(self):
     7        self.assertIs(None, None)
     8        self.assertIsNone(None)
     9
    10
    11# Exceptions seen
    
  • I add comments

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

    this is the same comment from the assertion_error project.

  • I open a new terminal then change directories to none

    cd none
    
  • I add a git commit message in the new terminal

    git commit -am 'add test_what_is_none'
    

None is None.

I know two new assert methods


test_is_none_a_boolean

Is None a boolean?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test to see if None is a boolean

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

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not None
    

GREEN: make it pass


I change assertIsNone to the assertIsNotNone method

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

the test passes.


REFACTOR: make it better


  • I add a comment

    15# NOTES
    16# False is NOT None
    17# None is None
    18
    19
    20# Exceptions seen
    21# AssertionError
    

    another comment from the assertion_error project.

  • I add an assertion for the other boolean

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

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not None
    
  • I change assertIsNone to assertIsNotNone

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

    the test passes.

  • I add a comment

    17# NOTES
    18# True is NOT None
    19# False is NOT None
    20# None is None
    

    also from the assertion_error project

  • I add a call to the assertNotIsInstance method to test if False is an instance of the bool class

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

    the terminal is my friend, and shows AssertionError

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

    because False is an instance of the bool class.

  • I change assertNotIsInstance to the assertIsInstance method

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

    the test passes.

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

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

    the terminal is my friend, and shows AssertionError

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

    because True is an instance of the bool class.

  • I make the statement True with assertIsInstance

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

    the test passes.

  • I add assertIsInstance to test if None is an instance (a copy) of the bool class

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

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a boolean.

  • I make the line True with assertNotIsInstance

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

    the test passes.

  • I remove the commented lines

    10    def test_is_none_a_boolean(self):
    11        self.assertIsNotNone(False)
    12        self.assertIsNotNone(True)
    13        self.assertIsInstance(False, bool)
    14        self.assertIsInstance(True, bool)
    15        self.assertNotIsInstance(None, bool)
    16
    17
    18# NOTES
    
  • I change the last 2 comments I added (True is NOT None and False is NOT None), since this is about None

    18# NOTES
    19# None is NOT a boolean
    20# None is None
    21
    22
    23# Exceptions seen
    24# AssertionError
    

    Okay, this is new, not something from the assertion_error project.

  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_boolean'
    

None is NOT a boolean.

The names of the assert methods can be confusing,

  • there is assertIsNotNone where Not comes after Is

  • there is assertNotIsInstance where Is comes after Not

Would assertIsNotInstance be better than assertNotIsInstance, since assertNotIsNone does not sound better than assertIsNotNone?

Not in this case, because the names of the assert methods match the assert statements they replace

  • assertIsNotNone(x) for assert x is not None

  • assertNotIsInstance(x, y) for assert not isinstance(x, y)


test_is_none_an_integer

Is None an integer?


RED: make it fail


  • I go back to the terminal where the tests are running

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

    15        self.assertNotIsInstance(None, bool)
    16
    17    def test_is_none_an_integer(self):
    18        self.assertIsNone(-1)
    19
    20
    21# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: -1 is not None
    

GREEN: make it pass


I make the statement True with assertIsNotNone

17    def test_is_none_an_integer(self):
18        # self.assertIsNone(-1)
19        self.assertIsNotNone(-1)
20
21
22# NOTES

the test passes.


REFACTOR: make it better


  • I add a new failing line with assertIsNotNone

    17    def test_is_none_an_integer(self):
    18        # self.assertIsNone(-1)
    19        self.assertIsNotNone(-1)
    20        self.assertIsNone(0)
    21
    22
    23# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    17    def test_is_none_an_integer(self):
    18        # self.assertIsNone(-1)
    19        self.assertIsNotNone(-1)
    20        # self.assertIsNone(0)
    21        self.assertIsNotNone(0)
    22
    23
    24# NOTES
    

    the test passes.

  • I add an assertion for

    17    def test_is_none_an_integer(self):
    18        # self.assertIsNone(-1)
    19        self.assertIsNotNone(-1)
    20        # self.assertIsNone(0)
    21        self.assertIsNotNone(0)
    22        self.assertIsNone(1)
    23
    24
    25# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    17    def test_is_none_an_integer(self):
    18        # self.assertIsNone(-1)
    19        self.assertIsNotNone(-1)
    20        # self.assertIsNone(0)
    21        self.assertIsNotNone(0)
    22        # self.assertIsNone(1)
    23        self.assertIsNotNone(1)
    24
    25
    26# NOTES
    

    the test passes.

  • I add a new failing line with assertNotIsInstance

    17    def test_is_none_an_integer(self):
    18        # self.assertIsNone(-1)
    19        self.assertIsNotNone(-1)
    20        # self.assertIsNone(0)
    21        self.assertIsNotNone(0)
    22        # self.assertIsNone(1)
    23        self.assertIsNotNone(1)
    24        self.assertNotIsInstance(-1, int)
    25
    26
    27# NOTES
    
  • I make the line True with assertIsInstance

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

    the test passes.

  • I add a failing assertion with assertNotIsInstance

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

    the terminal is my friend, and shows AssertionError

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

    because 0 is an integer.

  • I change the statement to make it True

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

    the test passes.

  • I add a failing assertion with assertNotIsInstance

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

    the terminal is my friend, and shows AssertionError

    AssertionError: 1 is an instance of <class 'int'>
    
    • because 1 is an integer.

    • I use 1 for all the integers (whole numbers) that are bigger than 0.

  • I make the failing line True with assertIsInstance

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

    the test passes.

  • I add one more failing line to test if None is an integer with assertIsInstance

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

    the terminal is my friend, and shows AssertionError

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

    because None is NOT an integer.

  • I make the line True with assertNotIsInstance

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

    the test passes.

  • I remove the commented lines

    17    def test_is_none_an_integer(self):
    18        self.assertIsNotNone(-1)
    19        self.assertIsNotNone(0)
    20        self.assertIsNotNone(1)
    21        self.assertIsInstance(-1, int)
    22        self.assertIsInstance(0, int)
    23        self.assertIsInstance(1, int)
    24        self.assertNotIsInstance(None, int)
    25
    26
    27# NOTES
    
  • I add a new comment

    27# NOTES
    28# None is NOT an integer
    29# None is NOT a boolean
    30# None is None
    31
    32
    33# Exceptions seen
    34# AssertionError
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_an_integer'
    

None is NOT an integer.


test_is_none_a_float

Is None a float?


RED: make it fail


  • I go back to the terminal where the tests are running

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

24        self.assertNotIsInstance(None, int)
25
26    def test_is_none_a_float(self):
27        self.assertIsNone(-0.1)
28
29
30# NOTES

the terminal is my friend, and shows AssertionError

AssertionError: -0.1 is not None

GREEN: make it pass


I make the statement True with assertIsNotNone

26    def test_is_none_a_float(self):
27        # self.assertIsNone(-0.1)
28        self.assertIsNotNone(-0.1)
29
30
31# NOTES

the test passes.


REFACTOR: make it better


  • I add a call to assertIsNone for 0.0

    26    def test_is_none_a_float(self):
    27        # self.assertIsNone(-0.1)
    28        self.assertIsNotNone(-0.1)
    29        self.assertIsNone(0.0)
    30
    31
    32# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    26    def test_is_none_a_float(self):
    27        # self.assertIsNone(-0.1)
    28        self.assertIsNotNone(-0.1)
    29        # self.assertIsNone(0.0)
    30        self.assertIsNotNone(0.0)
    31
    32
    33# NOTES
    

    the test passes.

  • I add a call to assertIsNone for 0.1

    26    def test_is_none_a_float(self):
    27        # self.assertIsNone(-0.1)
    28        self.assertIsNotNone(-0.1)
    29        # self.assertIsNone(0.0)
    30        self.assertIsNotNone(0.0)
    31        self.assertIsNone(0.1)
    32
    33
    34# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    26    def test_is_none_a_float(self):
    27        # self.assertIsNone(-0.1)
    28        self.assertIsNotNone(-0.1)
    29        # self.assertIsNone(0.0)
    30        self.assertIsNotNone(0.0)
    31        # self.assertIsNone(0.1)
    32        self.assertIsNotNone(0.1)
    33
    34
    35# NOTES
    

    the test passes. Time for instance tests.

  • I add a failing line with assertNotIsInstance for -0.1

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

    float is the class for binary floating point numbers. the terminal is my friend, and shows AssertionError

    AssertionError: -0.1 is an instance of <class 'float'>
    
    • because -0.1 is a 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

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

    the test passes.

  • I add the next instance test with assertNotIsInstance for 0.0

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

    the terminal is my friend, and shows AssertionError

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

    because 0.0 is a binary floating point number.

  • I make the statement True with assertIsInstance

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

    the test passes.

  • I add a failing line with assertNotIsInstance for 0.1

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

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.1 is an instance of <class 'float'>
    
    • because 0.1 is a 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

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

    the test passes.

  • I add one more failing line with the assertIsInstance method

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

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a float.

  • I make the statement True with the assertNotIsInstance method

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

    the test passes.

  • I remove the commented lines

    26    def test_is_none_a_float(self):
    27        self.assertIsNotNone(-0.1)
    28        self.assertIsNotNone(0.0)
    29        self.assertIsNotNone(0.1)
    30        self.assertIsInstance(-0.1, float)
    31        self.assertIsInstance(0.0, float)
    32        self.assertIsInstance(0.1, float)
    33        self.assertNotIsInstance(None, float)
    34
    35
    36# NOTES
    
  • I add a new comment

    36# NOTES
    37# None is NOT a float
    38# None is NOT an integer
    39# None is NOT a boolean
    40# None is None
    41
    42
    43# Exceptions seen
    44# AssertionError
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_float'
    

None is NOT a float.


test_is_none_a_string

Is None a string?


RED: make it fail


  • I go back to the terminal where the tests are running

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

    33        self.assertNotIsInstance(None, float)
    34
    35    def test_is_none_a_string(self):
    36        self.assertIsNone('')
    37
    38
    39# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: '' is not None
    

    because the empty string ('') is NOT None.


GREEN: make it pass


I make the statement True with assertIsNotNone

35    def test_is_none_a_string(self):
36        # self.assertIsNone('')
37        self.assertIsNotNone('')
38
39
40# NOTES

the test passes.


REFACTOR: make it better


  • I add a call to assertIsNone for a string that is not empty

    35    def test_is_none_a_string(self):
    36        # self.assertIsNone('')
    37        self.assertIsNotNone('')
    38        self.assertIsNone("characters")
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'characters' is not None
    
  • I change the assert method to make the statement True

    35    def test_is_none_a_string(self):
    36        # self.assertIsNone('')
    37        self.assertIsNotNone('')
    38        # self.assertIsNone("characters")
    39        self.assertIsNotNone("characters")
    40
    41
    42# NOTES
    

    the test passes.

  • I add a failing line with assertNotIsInstance for the empty string ("")

    35    def test_is_none_a_string(self):
    36        # self.assertIsNone('')
    37        self.assertIsNotNone('')
    38        # self.assertIsNone("characters")
    39        self.assertIsNotNone("characters")
    40        self.assertNotIsInstance("", str)
    41
    42
    43# NOTES
    
  • I change the assert method

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

    the test passes.

  • I add an assertion for 'characters'

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

    the terminal is my friend, and shows AssertionError

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

    because anything in quotes is a string.

  • I change the assert method

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

    the test passes.

  • I add a failing assertion with assertIsInstance

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

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a string.

  • I change the assert method

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

    the test passes because None is not a string.

  • I remove the commented lines

    35    def test_is_none_a_string(self):
    36        self.assertIsNotNone('')
    37        self.assertIsNotNone("characters")
    38        self.assertIsInstance("", str)
    39        self.assertIsInstance('characters', str)
    40        self.assertNotIsInstance(None, str)
    41
    42
    43# NOTES
    
  • I add a comment

    43# NOTES
    44# None is NOT a string
    45# None is NOT a float
    46# None is NOT an integer
    47# None is NOT a boolean
    48# None is None
    49
    50
    51# Exceptions seen
    52# AssertionError
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_string'
    

None is NOT a string.


test_is_none_a_tuple

Is None a tuple?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test to see if None is a tuple (anything in parentheses (( )) separated by commas)

    40        self.assertNotIsInstance(None, str)
    41
    42    def test_is_none_a_tuple(self):
    43        self.assertIsNone(())
    44
    45
    46# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: () is not None
    

GREEN: make it pass


I make the statement True

42    def test_is_none_a_tuple(self):
43        # self.assertIsNone(())
44        self.assertIsNotNone(())
45
46
47# NOTES

the test passes.


REFACTOR: make it better


  • I add a failing assertion for (0, 1, 2, 'n')

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        self.assertIsNone((0, 1, 2, 'n'))
    46
    47
    48# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        # self.assertIsNone((0, 1, 2, 'n'))
    46        self.assertIsNotNone((0, 1, 2, 'n'))
    47
    48
    49# NOTES
    

    the test passes.

  • I add a failing line with assertNotIsInstance for the empty tuple

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        # self.assertIsNone((0, 1, 2, 'n'))
    46        self.assertIsNotNone((0, 1, 2, 'n'))
    47        self.assertNotIsInstance((), tuple)
    48
    49
    50# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    because in Python empty parentheses (( )) is a tuple.

  • I make the statement True

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        # self.assertIsNone((0, 1, 2, 'n'))
    46        self.assertIsNotNone((0, 1, 2, 'n'))
    47        # self.assertNotIsInstance((), tuple)
    48        self.assertIsInstance((), tuple)
    49
    50
    51# NOTES
    

    the test passes.

  • I add a failing assertion for (0, 1, 2, 'n')

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

    the terminal is my friend, and shows AssertionError

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

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

  • I change the assert method to make the statement True

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        # self.assertIsNone((0, 1, 2, 'n'))
    46        self.assertIsNotNone((0, 1, 2, 'n'))
    47        # self.assertNotIsInstance((), tuple)
    48        self.assertIsInstance((), tuple)
    49        # self.assertNotIsInstance((0, 1, 2, 'n'), tuple)
    50        self.assertIsInstance((0, 1, 2, 'n'), tuple)
    51
    52
    53# NOTES
    

    the test passes.

  • I add one more instance test

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        # self.assertIsNone((0, 1, 2, 'n'))
    46        self.assertIsNotNone((0, 1, 2, 'n'))
    47        # self.assertNotIsInstance((), tuple)
    48        self.assertIsInstance((), tuple)
    49        # self.assertNotIsInstance((0, 1, 2, 'n'), tuple)
    50        self.assertIsInstance((0, 1, 2, 'n'), tuple)
    51        self.assertIsInstance(None, tuple)
    52
    53
    54# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a tuple.

  • I change the statement to make it True

    42    def test_is_none_a_tuple(self):
    43        # self.assertIsNone(())
    44        self.assertIsNotNone(())
    45        # self.assertIsNone((0, 1, 2, 'n'))
    46        self.assertIsNotNone((0, 1, 2, 'n'))
    47        # self.assertNotIsInstance((), tuple)
    48        self.assertIsInstance((), tuple)
    49        # self.assertNotIsInstance((0, 1, 2, 'n'), tuple)
    50        self.assertIsInstance((0, 1, 2, 'n'), tuple)
    51        # self.assertIsInstance(None, tuple)
    52        self.assertNotIsInstance(None, tuple)
    53
    54
    55# NOTES
    

    the test passes.

  • I remove the commented lines

    42    def test_is_none_a_tuple(self):
    43        self.assertIsNotNone(())
    44        self.assertIsNotNone((0, 1, 2, 'n'))
    45        self.assertIsInstance((), tuple)
    46        self.assertIsInstance((0, 1, 2, 'n'), tuple)
    47        self.assertNotIsInstance(None, tuple)
    48
    49
    50# NOTES
    
  • I add a comment

    50# NOTES
    51# None is NOT a tuple
    52# None is NOT a string
    53# None is NOT a float
    54# None is NOT an integer
    55# None is NOT a boolean
    56# None is None
    57
    58
    59# Exceptions seen
    60# AssertionError
    

    it looks like None is None and not anything else.

  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_tuple'
    

None is NOT a tuple.


test_is_none_a_list

Is None a list?


RED: make it fail


  • I go back to the terminal where the tests are running

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

    47        self.assertNotIsInstance(None, tuple)
    48
    49    def test_is_none_a_list(self):
    50        self.assertIsNone([])
    51
    52
    53# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [] is not None
    

GREEN: make it pass


I change the assertion to make the statement True

49    def test_is_none_a_list(self):
50        # self.assertIsNone([])
51        self.assertIsNotNone([])
52
53
54# NOTES

the test passes.


REFACTOR: make it better


  • I add a failing assertion for [0, 1, 2, 'n']

    49    def test_is_none_a_list(self):
    50        # self.assertIsNone([])
    51        self.assertIsNotNone([])
    52        self.assertIsNone([0, 1, 2, 'n'])
    

    the terminal is my friend, and shows AssertionError

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

    49    def test_is_none_a_list(self):
    50        # self.assertIsNone([])
    51        self.assertIsNotNone([])
    52        # self.assertIsNone([0, 1, 2, 'n'])
    53        self.assertIsNotNone([0, 1, 2, 'n'])
    54
    55
    56# NOTES
    

    the test passes.

  • I add a failing instance test for the empty list

    49    def test_is_none_a_list(self):
    50        # self.assertIsNone([])
    51        self.assertIsNotNone([])
    52        # self.assertIsNone([0, 1, 2, 'n'])
    53        self.assertIsNotNone([0, 1, 2, 'n'])
    54        self.assertNotIsInstance([], list)
    55
    56
    57# NOTES
    

    the terminal is my friend, and shows AssertionError

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

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

  • I make the statement True

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

    the test passes.

  • I add a failing assertion with assertNotIsInstance

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

    the terminal is my friend, and shows AssertionError

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

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

  • I change the assert method

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

    the test passes.

  • I add one more failing line with the assertIsInstance method

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

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a list.

  • I make the statement True

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

    the test passes.

  • I remove the commented lines

    49    def test_is_none_a_list(self):
    50        self.assertIsNotNone([])
    51        self.assertIsNotNone([0, 1, 2, 'n'])
    52        self.assertIsInstance([], list)
    53        self.assertIsInstance([0, 1, 2, 'n'], list)
    54        self.assertNotIsInstance(None, list)
    55
    56
    57# NOTES
    
  • I add a new comment

    57# NOTES
    58# None is NOT a list
    59# None is NOT a tuple
    60# None is NOT a string
    61# None is NOT a float
    62# None is NOT an integer
    63# None is NOT a boolean
    64# None is None
    65
    66
    67# Exceptions seen
    68# AssertionError
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_list'
    

None is NOT a list.


test_is_none_a_set

Is None a set?


RED: make it fail


  • I go back to the terminal where the tests are running

  • I want to test if None is a set (anything in curly braces { }, not key-value pairs)

    54        self.assertNotIsInstance(None, list)
    55
    56    def test_is_none_a_set(self):
    57        self.assertIsNone(set())
    58
    59
    60# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError: set() is not None
    

GREEN: make it pass


I make the statement True

56    def test_is_none_a_set(self):
57        # self.assertIsNone(set())
58        self.assertIsNotNone(set())
59
60
61# NOTES

the test passes.


REFACTOR: make it better


  • I add an assertion for {0, 1, 2, 'n'}

    56    def test_is_none_a_set(self):
    57        # self.assertIsNone(set())
    58        self.assertIsNotNone(set())
    59        self.assertIsNone({0, 1, 2, 'n'})
    60
    61
    62# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    56    def test_is_none_a_set(self):
    57        # self.assertIsNone(set())
    58        self.assertIsNotNone(set())
    59        # self.assertIsNone({0, 1, 2, 'n'})
    60        self.assertIsNotNone({0, 1, 2, 'n'})
    61
    62
    63# NOTES
    

    the test passes.

  • I add an instance test

    56    def test_is_none_a_set(self):
    57        # self.assertIsNone(set())
    58        self.assertIsNotNone(set())
    59        # self.assertIsNone({0, 1, 2, 'n'})
    60        self.assertIsNotNone({0, 1, 2, 'n'})
    61        self.assertNotIsInstance({0, 1, 2, 'n'}, set)
    62
    63
    64# NOTES
    

    the terminal is my friend, and shows AssertionError

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

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

    the test passes.

  • I add another instance test

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

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a set.

  • I make the statement True

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

    the test passes.

  • I remove the commented lines

    56    def test_is_none_a_set(self):
    57        self.assertIsNotNone(set())
    58        self.assertIsNotNone({0, 1, 2, 'n'})
    59        self.assertIsInstance({0, 1, 2, 'n'}, set)
    60        self.assertNotIsInstance(None, set)
    61
    62
    63# NOTES
    
  • I add a comment

    63# NOTES
    64# None is NOT a set
    65# None is NOT a list
    66# None is NOT a tuple
    67# None is NOT a string
    68# None is NOT a float
    69# None is NOT an integer
    70# None is NOT a boolean
    71# None is None
    
  • I add a git commit message in the other terminal

    git commit -am 'add test_is_none_a_set'
    

None is NOT a set


test_is_none_a_dictionary

Is None a dictionary?


RED: make it fail



GREEN: make it pass


I make the statement True

62    def test_is_none_a_dictionary(self):
63        # self.assertIsNone(dict())
64        self.assertIsNotNone(dict())
65
66
67# NOTES

the test passes.


REFACTOR: make it better


  • I add an assertion for {'key': 'value'}

    62    def test_is_none_a_dictionary(self):
    63        # self.assertIsNone(dict())
    64        self.assertIsNotNone(dict())
    65        self.assertIsNone({'key': 'value'})
    66
    67
    68# NOTES
    

    the terminal is my friend, and shows AssertionError

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

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

    the test passes.

  • I add a failing instance test for {}

    62    def test_is_none_a_dictionary(self):
    63        # self.assertIsNone(dict())
    64        self.assertIsNotNone(dict())
    65        # self.assertIsNone({'key': 'value'})
    66        self.assertIsNotNone({'key': 'value'})
    67        self.assertNotIsInstance({}, dict)
    68
    69
    70# NOTES
    
  • I change the assert method

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

    the test passes.

  • I add an instance test for {'key': 'value'}

    62    def test_is_none_a_dictionary(self):
    63        # self.assertIsNone(dict())
    64        self.assertIsNotNone(dict())
    65        # self.assertIsNone({'key': 'value'})
    66        self.assertIsNotNone({'key': 'value'})
    67        # self.assertNotIsInstance({}, dict)
    68        self.assertIsInstance({}, dict)
    69        self.assertNotIsInstance(
    70            {'key': 'value'}, dict
    71        )
    72
    73
    74# NOTES
    

    the terminal is my friend, and shows AssertionError

    AssertionError:
        {'key': 'value'} is an instance of <class 'dict'>
    
  • I make the statement True

    62    def test_is_none_a_dictionary(self):
    63        # self.assertIsNone(dict())
    64        self.assertIsNotNone(dict())
    65        # self.assertIsNone({'key': 'value'})
    66        self.assertIsNotNone({'key': 'value'})
    67        # self.assertNotIsInstance({}, dict)
    68        self.assertIsInstance({}, dict)
    69        # self.assertNotIsInstance(
    70        self.assertIsInstance(
    71            {'key': 'value'}, dict
    72        )
    73
    74
    75# NOTES
    

    the test passes.

  • I add the last failing instance test with assertIsInstance

    62    def test_is_none_a_dictionary(self):
    63        # self.assertIsNone(dict())
    64        self.assertIsNotNone(dict())
    65        # self.assertIsNone({'key': 'value'})
    66        self.assertIsNotNone({'key': 'value'})
    67        # self.assertNotIsInstance({}, dict)
    68        self.assertIsInstance({}, dict)
    69        # self.assertNotIsInstance(
    70        self.assertIsInstance(
    71            {'key': 'value'}, dict
    72        )
    73        self.assertIsInstance(None, dict)
    74
    75
    76# NOTES
    

    the terminal is my friend, and shows AssertionError

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

    because None is NOT a dictionary.

  • I make the statement True with assertNotIsInstance

    62    def test_is_none_a_dictionary(self):
    63        # self.assertIsNone(dict())
    64        self.assertIsNotNone(dict())
    65        # self.assertIsNone({'key': 'value'})
    66        self.assertIsNotNone({'key': 'value'})
    67        # self.assertNotIsInstance({}, dict)
    68        self.assertIsInstance({}, dict)
    69        # self.assertNotIsInstance(
    70        self.assertIsInstance(
    71            {'key': 'value'}, dict
    72        )
    73        # self.assertIsInstance(None, dict)
    74        self.assertNotIsInstance(None, dict)
    75
    76
    77# NOTES
    

    the test passes.

  • I remove the commented lines

    62    def test_is_none_a_dictionary(self):
    63        self.assertIsNotNone(dict())
    64        self.assertIsNotNone({'key': 'value'})
    65        self.assertIsInstance({}, dict)
    66        self.assertIsInstance(
    67            {'key': 'value'}, dict
    68        )
    69        self.assertNotIsInstance(None, dict)
    70
    71
    72# NOTES
    
  • I add the last comment

    72# NOTES
    73# None is NOT a dictionary
    74# None is NOT a set
    75# None is NOT a list
    76# None is NOT a tuple
    77# None is NOT a string
    78# None is NOT a float
    79# None is NOT an integer
    80# None is NOT a boolean
    81# None is None
    82
    83
    84# Exceptions seen
    85# AssertionError
    
  • I add a git commit message in the other terminal

    git commit -am \
    'add test_is_none_a_dictionary'
    

None is NOT a dictionary.


sets vs dictionaries

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

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

{
    'key': 'value',
    'another_key': 'another value',
    'one_more_key': 'one more value',
    'magic_key': 'magic value',
    'keyN': [0, 1, 2, 'n'],
}

sets do NOT have key-value pairs.

{0, 1, 2, 'n'}

close the project

  • I close test_none.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of none

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

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 I have seen


Would you like to use assertIsNotNone and assertIsNone to test these projects?

Would you like to use the assertIsNotNone and assertIsNone methods with the assertion_error project?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.