test AssertionError with unittest


I want to use the unittest library in the assertion_error project.


preview

I have these tests by the end of the chapter

assertion_error/tests/test_assertion_error.py
28class TestAssertionError(unittest.TestCase):
29
30    an_integer = 0
31    a_float = 0.0
32    a_string = ''
33    a_tuple = ()
34    a_list = []
35    a_set = set()
36    a_dictionary = {}
assertion_error/tests/test_assertion_error.py
38    def test_assert_keyword(self):
39        reality = 1 + 1
40        my_expectation = 2
41        assert_equal(reality, my_expectation)
42        self.assertEqual(reality, my_expectation)
43
44        reality = '1' + '1'
45        my_expectation = '11'
46        assert_equal(reality, my_expectation)
47        self.assertEqual(reality, my_expectation)
48
49        reality = 'I am' + ' alive'
50        my_expectation = 'I am alive'
51        assert_equal(reality, my_expectation)
52        self.assertEqual(reality, my_expectation)
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
58        assert_is_not_none(False)
59        self.assertIsNot(False, None)
60
61        assert_is_not_none(True)
62        self.assertIsNot(True, None)
63
64        assert_is_not_none(self.an_integer)
65        self.assertIsNot(self.an_integer, None)
66
67        assert_is_not_none(self.a_float)
68        self.assertIsNot(self.a_float, None)
69
70        assert_is_not_none(self.a_string)
71        self.assertIsNot(self.a_string, None)
72
73        assert_is_not_none(self.a_tuple)
74        self.assertIsNot(self.a_tuple, None)
75
76        assert_is_not_none(self.a_list)
77        self.assertIsNot(self.a_list, None)
78
79        assert_is_not_none(self.a_set)
80        self.assertIsNot(self.a_set, None)
81
82        assert_is_not_none(self.a_dictionary)
83        self.assertIsNot(self.a_dictionary, None)
assertion_error/tests/test_assertion_error.py
 85    def test_assertion_error_w_false(self):
 86        assert False is False
 87        self.assertIs(False, False)
 88
 89        assert_is_not_false(None)
 90        self.assertIsNot(None, False)
 91
 92        assert_is_not_false(True)
 93        self.assertIsNot(True, False)
 94
 95        assert_is_not_false(self.an_integer)
 96        self.assertIsNot(self.an_integer, False)
 97
 98        assert_is_not_false(self.a_float)
 99        self.assertIsNot(self.a_float, False)
100
101        assert_is_not_false(self.a_string)
102        self.assertIsNot(self.a_string, False)
103
104        assert_is_not_false(self.a_tuple)
105        self.assertIsNot(self.a_tuple, False)
106
107        assert_is_not_false(self.a_list)
108        self.assertIsNot(self.a_list, False)
109
110        assert_is_not_false(self.a_set)
111        self.assertIsNot(self.a_set, False)
112
113        assert_is_not_false(self.a_dictionary)
114        self.assertIsNot(self.a_dictionary, False)
assertion_error/tests/test_assertion_error.py
116    def test_assertion_error_w_true(self):
117        assert True is True
118        self.assertIs(True, True)
119
120        assert_is_not_true(None)
121        self.assertIsNot(None, True)
122
123        assert_is_not_true(False)
124        self.assertIsNot(False, True)
125
126        assert_is_not_true(self.an_integer)
127        self.assertIsNot(self.an_integer, True)
128
129        assert_is_not_true(self.a_float)
130        self.assertIsNot(self.a_float, True)
131
132        assert_is_not_true(self.a_string)
133        self.assertIsNot(self.a_string, True)
134
135        assert_is_not_true(self.a_tuple)
136        self.assertIsNot(self.a_tuple, True)
137
138        assert_is_not_true(self.a_list)
139        self.assertIsNot(self.a_list, True)
140
141        assert_is_not_true(self.a_set)
142        self.assertIsNot(self.a_set, True)
143
144        assert_is_not_true(self.a_dictionary)
145        self.assertIsNot(self.a_dictionary, True)
assertion_error/tests/test_assertion_error.py
147    def test_assertion_error_w_equality(self):
148        assert_equal(None, None)
149        self.assertEqual(None, None)
150
151        assert_equal(False, False)
152        self.assertEqual(False, False)
153
154        assert_equal(True, True)
155        self.assertEqual(True, True)
156
157        assert_not_equal(False, None)
158        self.assertNotEqual(False, None)
159
160        assert_not_equal(False, True)
161        self.assertNotEqual(False, True)
162
163        assert_not_equal(True, None)
164        self.assertNotEqual(True, None)
assertion_error/tests/test_assertion_error.py
166    def test_assertion_error_w_is_vs_equal(self):
167        assert_is_not(0, 0.0)
168        self.assertIsNot(0, 0.0)
169
170        assert_equal(0, 0.0)
171        self.assertEqual(0, 0.0)
assertion_error/tests/test_assertion_error.py
173    def will_not_run():
174        # will not run because
175        # the name does not start with test
176        assert False == True
177        self.assertEqual(False, True)
178
179    def test_failure(self):
180        assert_not_equal(False, True)
181        self.assertEqual(False, False)
182
183
184# NOTES
assertion_error/tests/test_assertion_error.py
220# None is None and equal to None
221
222
223# Exceptions seen
224# AssertionError
225# IndentationError
226# AttributeError
227# NameError
228# TypeError

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 from the tests folder

  • I use pytest-watcher to run the tests automatically

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

    the terminal is my friend, and shows

    tests/test_assertion_error.py .......             [100%]
    
    ================== 7 passed in A.BCs ===================
    

add TestAssertionError object

RED: make it fail



GREEN: make it pass



REFACTOR: make it better


  • I remove the commented lines from TestAssertionError

  • I open a new terminal then make sure I am in the assertion_error folder

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

    terminal
    git commit -am \
    'add TestAssertionError object'
    

test_assert_keyword with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_assert_keyword

28class TestAssertionError(unittest.TestCase):
29
30    # def test_assert_keyword():
31    def test_assert_keyword(self):
32        assert_equal(1+1, 2)

the test is green again.


REFACTOR: make it better



  • I add variables for 'I am' + ' alive' and 'I am alive'

  • I use the variables to remove repetition of 'I am' + ' alive' and 'I am alive'

    the test is still green.

  • I add variables for '1' + '1' and '11'

  • I use the variables to remove repetition of '1' + '1' and '11'

    still green.

  • I add variables for 1 + 1 and 2

  • I use the variables to remove repetition of 1 + 1 and 2

    green.

  • I remove the commented lines from test_assert_keyword

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'move test_assert_keyword to TestAssertionError'
    

test_assertion_error_w_none with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_assertion_error_w_none

41        reality = 'I am' + ' alive'
42        my_expectation = 'I am alive'
43        assert_equal(reality, my_expectation)
44        self.assertEqual(reality, my_expectation)
45
46    # def test_assertion_error_w_none():
47    def test_assertion_error_w_none(self):
48        assert None is None

the test is green again.


REFACTOR: make it better



  • I add a variable for {}

  • I use the variable to remove repetition of {}

    the test is still green.

  • I add a variable for set()

  • I use the variable to remove repetition of set()

    still green.

  • I add a variable for []

  • I use the variable to remove repetition of []

    green.

  • I add a variable for ()

  • I use the variable to remove repetition of ()

    still green.

  • I add a variable for ''

  • I use the variable to remove repetition of ''

    the test is still green.

  • I add a variable for 0.0

  • I use the variable to remove repetition of 0.0

    still green.

  • I add a variable for 0

  • I use the variable to remove repetition of 0

    green.

  • I remove the commented lines from test_assertion_error_w_none

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'move test_assertion_error_w_none to TestAssertionError'
    

test_assertion_error_w_false with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_assertion_error_w_false

80        a_dictionary = {}
81        assert_is_not_none(a_dictionary)
82        self.assertIsNot(a_dictionary, None)
83
84    # def test_assertion_error_w_false():
85    def test_assertion_error_w_false(self):
86        assert False is False

the test is green again.


REFACTOR: make it better



  • I add a variable for {}

  • I use the variable to remove repetition of {}

    the test is still green.

  • I add a variable for set()

  • I use the variable to remove repetition of set()

    still green.

  • I add a variable for []

  • I use the variable to remove repetition of []

    green.

  • I add a variable for ()

  • I use the variable to remove repetition of ()

    still green.

  • I add a variable for ''

  • I use the variable to remove repetition of ''

    the test is still green.

  • I add a variable for 0.0

  • I use the variable to remove repetition of 0.0

    still green.

  • I add a variable for 0

  • I use the variable to remove repetition of 0

    green.

  • I remove the commented lines from test_assertion_error_w_false

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'move test_assertion_error_w_false to TestAssertionError'
    

test_assertion_error_w_true with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_assertion_error_w_true

118        a_dictionary = {}
119        assert_is_not_false(a_dictionary)
120        self.assertIsNot(a_dictionary, False)
121
122    # def test_assertion_error_w_true():
123    def test_assertion_error_w_true(self):
124        assert True is True

the test is green again.


REFACTOR: make it better



  • I add a variable for {}

  • I use the variable to remove repetition of {}

    the test is still green.

  • I add a variable for set()

  • I use the variable to remove repetition of set()

    still green.

  • I add a variable for []

  • I use the variable to remove repetition of []

    green.

  • I add a variable for ()

  • I use the variable to remove repetition of ()

    still green.

  • I add a variable for ''

  • I use the variable to remove repetition of ''

    the test is still green.

  • I add a variable for 0.0

  • I use the variable to remove repetition of 0.0

    still green.

  • I add a variable for 0

  • I use the variable to remove repetition of 0

    green.

  • I remove the commented lines from test_assertion_error_w_true

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'move test_assertion_error_w_true to TestAssertionError'
    

test_assertion_error_w_equality with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_assertion_error_w_equality

156        a_dictionary = {}
157        assert_is_not_true(a_dictionary)
158        self.assertIsNot(a_dictionary, True)
159
160    # def test_assertion_error_w_equality():
161    def test_assertion_error_w_equality(self):
162        assert_equal(None, None)
163        assert_equal(False, False)
164        assert_equal(True, True)

the test is green again.


REFACTOR: make it better



test_assertion_error_w_is_vs_equal with unittest

RED: make it fail



GREEN: make it pass


I add self to the definition of test_assertion_error_w_is_vs_equal

176        assert_not_equal(True, None)
177        self.assertNotEqual(True, None)
178
179    # def test_assertion_error_w_is_vs_equal():
180    def test_assertion_error_w_is_vs_equal(self):
181        assert_is_not(0, 0.0)
182        assert_equal(0, 0.0)

the test is green again.


REFACTOR: make it better



will_not_run with unittest

RED: make it fail



GREEN: make it pass


I undo the change

183        assert_equal(0, 0.0)
184        self.assertEqual(0, 0.0)
185
186    def will_not_run():
187    # def will_not_run(self):
188    # def test_will_not_run(self):
189        # will not run because
190        # the name does not start with test
191        assert False == True
192        self.assertEqual(False, True)
193
194
195def test_failure():

the test is green again.


REFACTOR: make it better


  • I remove the new commented lines from will not run

  • I add a git commit message in the other terminal

    terminal
    git commit -am \
    'move will_not_run to TestAssertionError'
    

test_failure with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_failure in TestAssertionError

186    def will_not_run():
187        # will not run because
188        # the name does not start with test
189        assert False == True
190        self.assertEqual(False, True)
191
192    # def test_failure():
193    def test_failure(self):
194        # assert False == True
195        assert_not_equal(False, True)

the test is green again.


REFACTOR: make it better



remove repetition with object attributes

Three of the tests have the exact same variables. I can make them object attributes to remove repetition of making the same variables in the tests.








I can use object attributes to remove repetition. I make them once and other things in the object can reference them.


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


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?


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.