test AssertionError with assertIsNotNone and assertIsNone


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


preview

I have these tests by the end of the chapter

  1import unittest
  2
  3
  4class TestAssertionError(unittest.TestCase):
  5
  6    an_integer = 0
  7    a_float = 0.0
  8    a_string = ''
  9    a_tuple = ()
 10    a_list = []
 11    a_set = set()
 12    a_dictionary = {}
 13
 14    def test_assert_keyword(self):
 15        reality = 1 + 1
 16        my_expectation = 2
 17        assert reality == my_expectation
 18        self.assertEqual(reality, my_expectation)
 19
 20        reality = '1' + '1'
 21        my_expectation = '11'
 22        assert reality == my_expectation
 23        self.assertEqual(reality, my_expectation)
 24
 25        reality = 'I am' + ' alive'
 26        my_expectation = 'I am alive'
 27        assert reality == my_expectation
 28        self.assertEqual(reality, my_expectation)
 29
 30    def test_assertion_error_w_none(self):
 31        assert None is None
 32        self.assertIs(None, None)
 33        self.assertIsNone(None)
 34
 35        assert False is not None
 36        self.assertIsNot(False, None)
 37        self.assertIsNotNone(False)
 38
 39        assert True is not None
 40        self.assertIsNot(True, None)
 41        self.assertIsNotNone(True)
 42
 43        assert self.an_integer is not None
 44        self.assertIsNot(self.an_integer, None)
 45        self.assertIsNotNone(self.an_integer)
 46
 47        assert self.a_float is not None
 48        self.assertIsNot(self.a_float, None)
 49        self.assertIsNotNone(self.a_float)
 50
 51        assert self.a_string is not None
 52        self.assertIsNot(self.a_string, None)
 53        self.assertIsNotNone(self.a_string)
 54
 55        assert self.a_tuple is not None
 56        self.assertIsNot(self.a_tuple, None)
 57        self.assertIsNotNone(self.a_tuple)
 58
 59        assert self.a_list is not None
 60        self.assertIsNot(self.a_list, None)
 61        self.assertIsNotNone(self.a_list)
 62
 63        assert self.a_set is not None
 64        self.assertIsNot(self.a_set, None)
 65        self.assertIsNotNone(self.a_set)
 66
 67        assert self.a_dictionary is not None
 68        self.assertIsNot(self.a_dictionary, None)
 69        self.assertIsNotNone(self.a_dictionary)
 70
 71    def test_assertion_error_w_false(self):
 72        assert None is not False
 73        self.assertIsNot(None, False)
 74
 75        assert False is False
 76        self.assertIs(False, False)
 77
 78        assert True is not False
 79        self.assertIsNot(True, False)
 80
 81        assert self.an_integer is not False
 82        self.assertIsNot(self.an_integer, False)
 83
 84        assert self.a_float is not False
 85        self.assertIsNot(self.a_float, False)
 86
 87        assert self.a_string is not False
 88        self.assertIsNot(self.a_string, False)
 89
 90        assert self.a_tuple is not False
 91        self.assertIsNot(self.a_tuple, False)
 92
 93        assert self.a_list is not False
 94        self.assertIsNot(self.a_list, False)
 95
 96        assert self.a_set is not False
 97        self.assertIsNot(self.a_set, False)
 98
 99        assert self.a_dictionary is not False
100        self.assertIsNot(self.a_dictionary, False)
101
102    def test_assertion_error_w_true(self):
103        assert None is not True
104        self.assertIsNot(None, True)
105
106        assert False is not True
107        self.assertIsNot(False, True)
108
109        assert True is True
110        self.assertIs(True, True)
111
112        assert self.an_integer is not True
113        self.assertIsNot(self.an_integer, True)
114
115        assert self.a_float is not True
116        self.assertIsNot(self.a_float, True)
117
118        assert self.a_string is not True
119        self.assertIsNot(self.a_string, True)
120
121        assert self.a_tuple is not True
122        self.assertIsNot(self.a_tuple, True)
123
124        assert self.a_list is not True
125        self.assertIsNot(self.a_list, True)
126
127        assert self.a_set is not True
128        self.assertIsNot(self.a_set, True)
129
130        assert self.a_dictionary is not True
131        self.assertIsNot(self.a_dictionary, True)
132
133    def test_assertion_error_w_equality(self):
134        assert None == None
135        self.assertEqual(None, None)
136
137        assert False != None
138        self.assertNotEqual(False, None)
139
140        assert False != True
141        self.assertNotEqual(False, True)
142
143        assert False == False
144        self.assertEqual(False, False)
145
146        assert True != None
147        self.assertNotEqual(True, None)
148
149        assert True == True
150        self.assertEqual(True, True)
151
152    def test_assertion_error_w_is_vs_equal(self):
153        assert 0 is not 0.0
154        self.assertIsNot(0, 0.0)
155
156        assert 0 == 0.0
157        self.assertEqual(0, 0.0)
158
159    def will_not_run():
160        assert False == True
161        self.assertEqual(False, True)
162
163    def test_failure(self):
164        assert False == False
165        self.assertEqual(False, False)
166
167
168# NOTES
169# a dictionary is not True
170# a dictionary is not False
171# a dictionary is not None
172# a set is not True
173# a set is not False
174# a set is not None
175# a list is not True
176# a list is not False
177# a list is not None
178# a tuple is not True
179# a tuple is not False
180# a tuple is not None
181# a string is not True
182# a string is not False
183# a string is not None
184# a float is not True
185# a float is not False
186# a float is not None
187# an integer is not True
188# an integer is not False
189# an integer is not None
190# True is True and equal to True
191# True is not False and NOT equal to False
192# True is not None and NOT equal to None
193# False is not True and NOT equal to True
194# False is False and equal to False
195# False is not None and NOT equal to None
196# None is not True and NOT equal to True
197# None is not False and NOT equal to False
198# None is None and equal to None
199
200
201# Exceptions seen
202# AssertionError
203# AttributeError
204# NameError
205# TypeError

open the project


use assertIsNotNone and assertIsNone

  • I add a call to assertIsNotNone in test_assertion_error_w_none

    30    def test_assertion_error_w_none(self):
    31        assert None is None
    32        self.assertIs(None, None)
    33        self.assertIsNotNone(None)
    34
    35        assert False is not None
    36        self.assertIsNot(False, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly None
    

    because None is None

  • I change assertIsNotNone to a call to the assertIsNone method

    30    def test_assertion_error_w_none(self):
    31        assert None is None
    32        self.assertIs(None, None)
    33        # self.assertIsNotNone(None)
    34        self.assertIsNone(None)
    35
    36        assert False is not None
    37        self.assertIsNot(False, None)
    

    the test passes

  • I add assertIsNone for False

    36        assert False is not None
    37        self.assertIsNot(False, None)
    38        self.assertIsNone(False)
    39
    40        assert True is not None
    41        self.assertIsNot(True, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    36        assert False is not None
    37        self.assertIsNot(False, None)
    38        # self.assertIsNone(False)
    39        self.assertIsNotNone(False)
    40
    41        assert True is not None
    42        self.assertIsNot(True, None)
    

    the test passes because False is not None.

  • I add assertIsNone for True

    41        assert True is not None
    42        self.assertIsNot(True, None)
    43        self.assertIsNone(True)
    44
    45        assert self.an_integer is not None
    46        self.assertIsNot(self.an_integer, None)
    

    the terminal is my friend, and shows AssertionError

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

    41        assert True is not None
    42        self.assertIsNot(True, None)
    43        # self.assertIsNone(True)
    44        self.assertIsNotNone(True)
    45
    46        assert self.an_integer is not None
    47        self.assertIsNot(self.an_integer, None)
    

    the test passes because True is not None.

  • I add assertIsNone for an integer (a whole number without decimals)

    46        assert self.an_integer is not None
    47        self.assertIsNot(self.an_integer, None)
    48        self.assertIsNone(self.an_integer)
    49
    50        assert self.a_float is not None
    51        self.assertIsNot(self.a_float, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0 is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    46        assert self.an_integer is not None
    47        self.assertIsNot(self.an_integer, None)
    48        # self.assertIsNone(self.an_integer)
    49        self.assertIsNotNone(self.an_integer)
    50
    51        assert self.a_float is not None
    52        self.assertIsNot(self.a_float, None)
    

    the test passes because an integer is not None.

  • I add assertIsNone for a float (binary floating point decimal number)

    51        assert self.a_float is not None
    52        self.assertIsNot(self.a_float, None)
    53        self.assertIsNone(self.a_float)
    54
    55        assert self.a_string is not None
    56        self.assertIsNot(self.a_string, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 0.0 is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    51        assert self.a_float is not None
    52        self.assertIsNot(self.a_float, None)
    53        # self.assertIsNone(self.a_float)
    54        self.assertIsNotNone(self.a_float)
    55
    56        assert self.a_string is not None
    57        self.assertIsNot(self.a_string, None)
    

    the test passes because a float is not None.

  • I add assertIsNone for a string (anything in quotes)

    56        assert self.a_string is not None
    57        self.assertIsNot(self.a_string, None)
    58        self.assertIsNone(self.a_string)
    59
    60        assert self.a_tuple is not None
    61        self.assertIsNot(self.a_tuple, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: '' is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    56        assert self.a_string is not None
    57        self.assertIsNot(self.a_string, None)
    58        # self.assertIsNone(self.a_string)
    59        self.assertIsNotNone(self.a_string)
    60
    61        assert self.a_tuple is not None
    62        self.assertIsNot(self.a_tuple, None)
    

    the test passes because a string (anything in quotes) is not None.

  • I add assertIsNone for a tuple (anything in parentheses ( ) separated by a comma)

    61        assert self.a_tuple is not None
    62        self.assertIsNot(self.a_tuple, None)
    63        self.assertIsNone(self.a_tuple)
    64
    65        assert self.a_list is not None
    66        self.assertIsNot(self.a_list, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: () is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    61        assert self.a_tuple is not None
    62        self.assertIsNot(self.a_tuple, None)
    63        # self.assertIsNone(self.a_tuple)
    64        self.assertIsNotNone(self.a_tuple)
    65
    66        assert self.a_list is not None
    67        self.assertIsNot(self.a_list, None)
    

    the test passes because a tuple is not None.

  • I add assertIsNone for a list (anything in square brackets ‘[ ]’)

    66        assert self.a_list is not None
    67        self.assertIsNot(self.a_list, None)
    68        self.assertIsNone(self.a_list)
    69
    70        assert self.a_set is not None
    71        self.assertIsNot(self.a_set, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: [] is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    66        assert self.a_list is not None
    67        self.assertIsNot(self.a_list, None)
    68        # self.assertIsNone(self.a_list)
    69        self.assertIsNotNone(self.a_list)
    70
    71        assert self.a_set is not None
    72        self.assertIsNot(self.a_set, None)
    

    the test passes because a list is not None.

  • I add assertIsNone for a set (anything in curly braces { }, not key-value pairs)

    71        assert self.a_set is not None
    72        self.assertIsNot(self.a_set, None)
    73        self.assertIsNone(self.a_set)
    74
    75        assert self.a_dictionary is not None
    76        self.assertIsNot(self.a_dictionary, None)
    

    the terminal is my friend, and shows AssertionError

    AssertionError: set() is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    71        assert self.a_set is not None
    72        self.assertIsNot(self.a_set, None)
    73        # self.assertIsNone(self.a_set)
    74        self.assertIsNotNone(self.a_set)
    75
    76        assert self.a_dictionary is not None
    77        self.assertIsNot(self.a_dictionary, None)
    

    the test passes because a set is not None.

  • I add assertIsNone for a dictionary (key-value pairs in curly braces ‘{ }’ separated by commas)

    76        assert self.a_dictionary is not None
    77        self.assertIsNot(self.a_dictionary, None)
    78        self.assertIsNone(self.a_dictionary)
    79
    80    def test_assertion_error_w_false(self):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: {} is not None
    
  • I change the call from assertIsNone to assertIsNotNone

    76        assert self.a_dictionary is not None
    77        self.assertIsNot(self.a_dictionary, None)
    78        # self.assertIsNone(self.a_dictionary)
    79        self.assertIsNotNone(self.a_dictionary)
    80
    81    def test_assertion_error_w_false(self):
    

    the test passes because a dictionary is not None.

  • I remove the commented lines from test_assertion_error_w_none

    30    def test_assertion_error_w_none(self):
    31        assert None is None
    32        self.assertIs(None, None)
    33        self.assertIsNone(None)
    
    35        assert False is not None
    36        self.assertIsNot(False, None)
    37        self.assertIsNotNone(False)
    
    39        assert True is not None
    40        self.assertIsNot(True, None)
    41        self.assertIsNotNone(True)
    
    43        assert self.an_integer is not None
    44        self.assertIsNot(self.an_integer, None)
    45        self.assertIsNotNone(self.an_integer)
    
    47        assert self.a_float is not None
    48        self.assertIsNot(self.a_float, None)
    49        self.assertIsNotNone(self.a_float)
    
    51        assert self.a_string is not None
    52        self.assertIsNot(self.a_string, None)
    53        self.assertIsNotNone(self.a_string)
    
    55        assert self.a_tuple is not None
    56        self.assertIsNot(self.a_tuple, None)
    57        self.assertIsNotNone(self.a_tuple)
    
    59        assert self.a_list is not None
    60        self.assertIsNot(self.a_list, None)
    61        self.assertIsNotNone(self.a_list)
    
    63        assert self.a_set is not None
    64        self.assertIsNot(self.a_set, None)
    65        self.assertIsNotNone(self.a_set)
    
    67        assert self.a_dictionary is not None
    68        self.assertIsNot(self.a_dictionary, None)
    69        self.assertIsNotNone(self.a_dictionary)
    70
    71    def test_assertion_error_w_false(self):
    
  • I open a new terminal then change directories to assertion_error

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

    git commit -am \
    'use assertIsNotNone and assertIsNone'
    

    the terminal shows a summary of the changes then goes back to the command line.

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

    cd ..
    

    the terminal is my friend, and shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I can use assertIsNotNone and assertIsNone methods 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?

I know

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.