what is None?


None is used when there is no value. In Mathematics we use 0 for 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

questions about None


start the project

  • I name this project none

  • I open a terminal

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

    terminal
    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

    terminal
    uv init none
    

    the terminal shows

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

    terminal
    cd none
    
  • I make a directory for the tests

    terminal
    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 make an empty file for the tests in the tests folder

    touch tests/test_none.py
    
    New-Item tests/test_none.py
    
  • I open test_none.py from the tests folder

  • 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.assertEqual(False, True)
    
  • I go back to the terminal

  • I use echo to add pytest to requirements.txt

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

    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use Out-File to add pytest to requirements.txt

    "pytest" | Out-File requirements.txt -Encoding UTF8
    
  • I add pytest-watcher to the requirements file

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

    terminal
    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

    terminal
    git add .
    
  • I add a git commit message

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

    terminal
    uv run pytest-watcher . --now --patterns "**/*.py"
    

    the terminal is my friend, and shows AssertionError

    terminal
    ======================== 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

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

    and try uv run pytest-watcher . --now --patterns "**/*.py" again

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

  • I change True to False in the assertion

    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.


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

  • I add comments

  • I open a new terminal then change directories to none

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

    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



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


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)

    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


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


None is NOT a float.


test_is_none_a_string

Is None a string?


RED: make it fail



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


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)

    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


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 ([ ]))

    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


None is NOT a list.


test_is_none_a_set

Is None a set?


RED: make it fail



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


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


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',
    'value',
    'another value',
    'one more value',
    'magic value',
}

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

    terminal
    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?


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.