test AssertionError with None


I want to use the assertIsNotNone and assertIsNone methods to test None in test_assertion_error_w_none.


preview

test_assertion_error_w_none looks like this by the end of the chapter

assertion_error/tests/test_assertion_error.py
54    def test_assertion_error_w_none(self):
55        assert None is None
56        self.assertIs(None, None)
57        self.assertIsNone(None)
58
59        assert_is_not_none(False)
60        self.assertIsNot(False, None)
61        self.assertIsNotNone(False)
62
63        assert_is_not_none(True)
64        self.assertIsNot(True, None)
65        self.assertIsNotNone(True)
66
67        assert_is_not_none(self.an_integer)
68        self.assertIsNot(self.an_integer, None)
69        self.assertIsNotNone(self.an_integer)
70
71        assert_is_not_none(self.a_float)
72        self.assertIsNot(self.a_float, None)
73        self.assertIsNotNone(self.a_float)
74
75        assert_is_not_none(self.a_string)
76        self.assertIsNot(self.a_string, None)
77        self.assertIsNotNone(self.a_string)
78
79        assert_is_not_none(self.a_tuple)
80        self.assertIsNot(self.a_tuple, None)
81        self.assertIsNotNone(self.a_tuple)
82
83        assert_is_not_none(self.a_list)
84        self.assertIsNot(self.a_list, None)
85        self.assertIsNotNone(self.a_list)
86
87        assert_is_not_none(self.a_set)
88        self.assertIsNot(self.a_set, None)
89        self.assertIsNotNone(self.a_set)
90
91        assert_is_not_none(self.a_dictionary)
92        self.assertIsNot(self.a_dictionary, None)
93        self.assertIsNotNone(self.a_dictionary)

open the project

  • I open a terminal

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

    terminal
    cd assertion_error
    
  • I open test_assertion_error.py

  • I use pytest-watcher to run the tests automatically

    terminal
    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows

    tests/test_assertion_error.py .......             [100%]
    
    ================== 7 passed in D.EFs ===================
    

use assertIsNotNone and assertIsNone

I can use assertIsNotNone and assertIsNone for assertIsNot(x, None) and assertIs(x, None).


close the project

  • I close test_assertion_error.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 assertion_error

    terminal
    cd ..
    

    the terminal is my friend, and shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I can use assertIsNone methods and assertIsNotNone for assertions that test if something is None or not - assertIs(x, None) and assertIsNot(x, None).


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 the assertIsNotNone and assertIsNone methods with the functions 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.