test functions with assertIsNotNone and assertIsNone

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


preview

I have these tests by the end of the chapter

  1import src.functions
  2import unittest
  3
  4
  5class TestFunctions(unittest.TestCase):
  6
  7    def test_making_a_function_w_pass(self):
  8        result = src.functions.w_pass()
  9
 10        assert result is None
 11        self.assertIs(result, None)
 12
 13    def test_making_a_function_w_return(self):
 14        result = src.functions.w_return()
 15
 16        assert result is None
 17        self.assertIs(result, None)
 18
 19    def test_making_a_function_w_return_none(self):
 20        result = src.functions.w_return_none()
 21
 22        assert result is None
 23        self.assertIs(result, None)
 24
 25    def test_what_happens_after_functions_return(self):
 26        result = src.functions.return_leaves_the_function()
 27
 28        assert result is None
 29        self.assertIs(result, None)
 30
 31    def test_constant_function(self):
 32        result = src.functions.constant()
 33        expectation = 'the same thing'
 34
 35        assert result == expectation
 36        self.assertEqual(result, expectation)
 37
 38    def test_identity_function(self):
 39        result = src.functions.identity(None)
 40
 41        assert result == None
 42        self.assertEqual(result, None)
 43
 44        result = src.functions.identity(object)
 45
 46        assert result == object
 47        self.assertEqual(result, object)
 48
 49    def test_why_use_a_function(self):
 50        def add_x(number):
 51            return 3 + number
 52
 53        result = add_x(0)
 54        expectation = 3
 55        assert result == expectation
 56        self.assertEqual(result, expectation)
 57
 58        result = add_x(1)
 59        expectation = 4
 60        assert result == expectation
 61        self.assertEqual(result, expectation)
 62
 63        result = add_x(2)
 64        expectation = 5
 65        assert result == expectation
 66        self.assertEqual(result, expectation)
 67
 68        result = add_x(3)
 69        expectation = 6
 70        assert result == expectation
 71        self.assertEqual(result, expectation)
 72
 73        result = add_x(4)
 74        expectation = 7
 75        assert result == expectation
 76        self.assertEqual(result, expectation)
 77
 78        result = add_x(5)
 79        expectation = 8
 80        assert result == expectation
 81        self.assertEqual(result, expectation)
 82
 83        result = add_x(6)
 84        expectation = 9
 85        assert result == expectation
 86        self.assertEqual(result, expectation)
 87
 88        result = add_x(7)
 89        expectation = 10
 90        assert result == expectation
 91        self.assertEqual(result, expectation)
 92
 93        result = add_x(8)
 94        expectation = 11
 95        assert result == expectation
 96        self.assertEqual(result, expectation)
 97
 98        result = add_x(9)
 99        expectation = 12
100        assert result == expectation
101        self.assertEqual(result, expectation)
102
103    def test_positional_arguments(self):
104        positional_arguments = (
105            src.functions.positional_arguments
106        )
107        first, last = 'first', 'last'
108
109        reality = positional_arguments(first, last)
110        my_expectation = (first, last)
111        assert reality == my_expectation
112        self.assertEqual(reality, my_expectation)
113
114        reality = positional_arguments(last, first)
115        my_expectation = (last, first)
116        assert reality == my_expectation
117        self.assertEqual(reality, my_expectation)
118
119        reality = positional_arguments(0, 1)
120        my_expectation = (0, 1)
121        assert reality == my_expectation
122        self.assertEqual(reality, my_expectation)
123
124        a_tuple = (1, 2, 3, 'n')
125        a_list = [1, 2, 3, 'n']
126
127        reality = positional_arguments(
128            a_tuple, a_list
129        )
130        my_expectation = (a_tuple, a_list)
131        assert reality == my_expectation
132        self.assertEqual(reality, my_expectation)
133
134        keyword_arguments = (
135            src.functions.keyword_arguments
136        )
137        a_set = {1, 2, 3, 'n'}
138        a_dictionary = {'key': 'value'}
139
140        reality = keyword_arguments(
141            a_set, a_dictionary,
142        )
143        my_expectation = (a_set, a_dictionary)
144        assert reality == my_expectation
145        self.assertEqual(reality, my_expectation)
146
147    def test_keyword_arguments(self):
148        keyword_arguments = (
149            src.functions.keyword_arguments
150        )
151        first, last = 'first', 'last'
152
153        reality = keyword_arguments(
154            first_input=first, last_input=last,
155        )
156        my_expectation = (first, last)
157        assert reality == my_expectation
158        self.assertEqual(reality, my_expectation)
159
160        reality = keyword_arguments(
161            last_input=last, first_input=first,
162        )
163        my_expectation = (first, last)
164        assert reality == my_expectation
165        self.assertEqual(reality, my_expectation)
166
167        reality = keyword_arguments(
168            last_input=0, first_input=1,
169        )
170        my_expectation = (1, 0)
171        assert reality == my_expectation
172        self.assertEqual(reality, my_expectation)
173
174        a_tuple = (1, 2, 3, 'n')
175        a_list = [1, 2, 3, 'n']
176
177        reality = keyword_arguments(
178            first_input=a_tuple,
179            last_input=a_list,
180        )
181        my_expectation = (a_tuple, a_list)
182        assert reality == my_expectation
183        self.assertEqual(reality, my_expectation)
184
185        positional_arguments = (
186            src.functions.positional_arguments
187        )
188        a_set = {1, 2, 3, 'n'}
189        a_dictionary = {'key': 'value'}
190
191        reality = positional_arguments(
192            last_input=a_dictionary,
193            first_input=a_set,
194        )
195        my_expectation = (a_set, a_dictionary)
196        assert reality == my_expectation
197        self.assertEqual(reality, my_expectation)
198
199    def test_args_and_kwargs(self):
200        first, last = 'first', 'last'
201
202        reality = src.functions.args_and_kwargs(
203            first, last_input=last,
204        )
205        my_expectation = (first, last)
206        assert reality == my_expectation
207        self.assertEqual(reality, my_expectation)
208
209    def test_optional_arguments(self):
210        optional_arguments = (
211            src.functions.optional_arguments
212        )
213
214        first_name, last_name = 'jane', 'doe'
215
216        reality = optional_arguments(first_name)
217        my_expectation = (first_name, last_name)
218        assert reality == my_expectation
219        self.assertEqual(reality, my_expectation)
220
221        first_name, blow = 'joe', 'blow'
222
223        reality = optional_arguments(
224            first_name, blow
225        )
226        my_expectation = (first_name, blow)
227        assert reality == my_expectation
228        self.assertEqual(reality, my_expectation)
229
230        first_name = 'john'
231
232        reality = optional_arguments(
233            first_input=first_name
234        )
235        my_expectation = (first_name, last_name)
236        assert reality == my_expectation
237        self.assertEqual(reality, my_expectation)
238
239        last_name = 'smith'
240
241        reality = optional_arguments(
242            last_input=last_name,
243            first_input=first_name,
244        )
245        my_expectation = (first_name, last_name)
246        assert reality == my_expectation
247        self.assertEqual(reality, my_expectation)
248
249    def test_unknown_number_of_arguments(self):
250        unknown_number_of_arguments = (
251            src.functions.unknown_number_of_arguments
252        )
253
254        a_tuple = (0, 1)
255        a_dictionary = {'a': 2, 'b': 3}
256
257        reality = unknown_number_of_arguments(
258            *a_tuple, **a_dictionary
259        )
260        my_expectation = (a_tuple, a_dictionary)
261        assert reality == my_expectation
262        self.assertEqual(reality, my_expectation)
263
264        a_tuple = (0, 1)
265        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
266
267        reality = unknown_number_of_arguments(
268            *a_tuple, **a_dictionary
269        )
270        my_expectation = (a_tuple, a_dictionary)
271        assert reality == my_expectation
272        self.assertEqual(reality, my_expectation)
273
274        a_tuple = (0, 1, 2)
275        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
276
277        reality = unknown_number_of_arguments(
278            *a_tuple, **a_dictionary
279        )
280        my_expectation = (a_tuple, a_dictionary)
281        assert reality == my_expectation
282        self.assertEqual(reality, my_expectation)
283
284        a_tuple = (1, 2, 3, 'n')
285
286        reality = unknown_number_of_arguments(*a_tuple)
287        my_expectation = (a_tuple, {})
288        assert reality == my_expectation
289        self.assertEqual(reality, my_expectation)
290
291        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
292
293        reality = unknown_number_of_arguments(
294            **a_dictionary
295        )
296        my_expectation = ((), a_dictionary)
297        assert reality == my_expectation
298        self.assertEqual(reality, my_expectation)
299
300        reality = unknown_number_of_arguments()
301        my_expectation = ((), {})
302        assert reality == my_expectation
303        self.assertEqual(reality, my_expectation)
304
305
306# Exceptions seen
307# AssertionError
308# NameError
309# TypeError
310# SyntaxError
311# ModuleNotFoundError
312# AttributeError

open the project

  • I open a terminal

  • I change directory to the project

    cd functions
    

    the terminal shows I am in the functions folder

    .../pumping_python/functions
    
  • I open test_functions.py

  • I use pytest-watcher to run the tests automatically

    uv run pytest-watcher . --now
    

    the terminal shows

    test_functions.py ............                      [100%]
    
    =================== 12 passed in G.HIs ===================
    

add TestFunctions class

RED: make it fail


  • I add a class named Functions to test_functions.py

     1import src.functions
     2
     3
     4class Functions(object):
     5
     6    def test_failure(self):
     7        self.assertEqual(True, False)
     8
     9
    10def test_making_a_function_w_pass():
    

    the test is still green.

  • I change the name of the class to TestFunctions

    1import src.functions
    2
    3
    4# class Functions(object):
    5class TestFunctions(object):
    

    the terminal is my friend, and shows AttributeError

    AttributeError: 'TestFunctions' object
                    has no attribute 'assertEqual'
    

GREEN: make it pass


  • I add unittest.TestCase as the parent class of TestFunctions

    1import src.functions
    2
    3
    4# class Functions(object):
    5# class TestFunctions(object):
    6class TestFunctions(unittest.TestCase):
    

    the terminal is my friend, and shows NameError

    NameError: name 'unittest' is not defined.
               Did you forget to import 'unittest'?
    
  • I add an import statement at the top of the file

    1import src.functions
    2import unittest
    3
    4
    5# class Functions(object):
    6# class TestFunctions(object):
    7class TestFunctions(unittest.TestCase):
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True != False
    
  • I change True to False in the assertion

     5# class Functions(object):
     6# class TestFunctions(object):
     7class TestFunctions(unittest.TestCase):
     8
     9    def test_failure(self):
    10        # self.assertEqual(True, False)
    11        self.assertEqual(False, False)
    12
    13
    14def test_making_a_function_w_pass():
    

    the test passes.


REFACTOR: make it better


  • I remove the commented lines

     1import src.functions
     2import unittest
     3
     4
     5class TestFunctions(unittest.TestCase):
     6
     7    def test_failure(self):
     8        self.assertEqual(False, False)
     9
    10
    11def test_making_a_function_w_pass():
    
  • I open a new terminal then make sure I am in the functions folder

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

    git commit -am \
    'add TestFunctions class'
    

test_making_a_function_w_pass with unittest

RED: make it fail


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

  • I move test_making_a_function_w_pass to make it a method of the TestFunctions class and replace test_failure

     5class TestFunctions(unittest.TestCase):
     6
     7    def test_making_a_function_w_pass():
     8        assert src.functions.w_pass() is None
     9
    10
    11def test_making_a_function_w_return():
    

    the terminal is my friend, and shows TypeError

    TypeError: TestFunctions.test_making_a_function_w_pass() takes
               0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_making_a_function_w_pass

7    # def test_making_a_function_w_pass():
8    def test_making_a_function_w_pass(self):
9        assert src.functions.w_pass() is None

the test is green again.


REFACTOR: make it better


  • I add a call to the assertIsNot method

     7    # def test_making_a_function_w_pass():
     8    def test_making_a_function_w_pass(self):
     9        assert src.functions.w_pass() is None
    10        self.assertIsNot(
    11            src.functions.w_pass(), None
    12        )
    13
    14
    15def test_making_a_function_w_return():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: None
    
  • I change assertIsNot to assertIs

     7    # def test_making_a_function_w_pass():
     8    def test_making_a_function_w_pass(self):
     9        assert src.functions.w_pass() is None
    10        # self.assertIsNot(
    11        self.assertIs(
    12            src.functions.w_pass(), None
    13        )
    14
    15
    16def test_making_a_function_w_return():
    

    the test passes.

  • I add a variable for src.functions.w_pass()

    7    # def test_making_a_function_w_pass():
    8    def test_making_a_function_w_pass(self):
    9        result = src.functions.w_pass()
    
  • I use the variable to remove repetition of src.functions.w_pass()

     7    # def test_making_a_function_w_pass():
     8    def test_making_a_function_w_pass(self):
     9        result = src.functions.w_pass()
    10        # assert src.functions.w_pass() is None
    11        assert result is None
    12        # self.assertIsNot(
    13        self.assertIs(
    14            # src.functions.w_pass(), None
    15            result, None
    16        )
    17
    18
    19def test_making_a_function_w_return():
    
  • I remove the commented lines from test_making_a_function_w_pass

     5class TestFunctions(unittest.TestCase):
     6
     7    def test_making_a_function_w_pass(self):
     8        result = src.functions.w_pass()
     9
    10        assert result is None
    11        self.assertIs(result, None)
    12
    13
    14def test_making_a_function_w_return():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_making_a_function_w_pass to TestFunctions'
    

test_making_a_function_w_return with unittest

RED: make it fail


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

  • I move test_making_a_function_w_return to make it a method of the TestFunctions class

    11        self.assertIs(result, None)
    12
    13    def test_making_a_function_w_return():
    14        assert src.functions.w_return() is None
    15
    16
    17def test_making_a_function_w_return_none():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_making_a_function_w_return()
        takes 0 positional arguments but 1 was given
    

    because …


GREEN: make it pass


I add self to the parentheses of test_making_a_function_w_return

13    # def test_making_a_function_w_return():
14    def test_making_a_function_w_return(self):

green again.


REFACTOR: make it better


  • I add a call to the assertIsNot method

    13    # def test_making_a_function_w_return():
    14    def test_making_a_function_w_return(self):
    15        assert src.functions.w_return() is None
    16        self.assertIsNot(
    17            src.functions.w_return(), None
    18        )
    19
    20
    21def test_making_a_function_w_return_none():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: None
    
  • I change assertIsNot to assertIs

    13    # def test_making_a_function_w_return():
    14    def test_making_a_function_w_return(self):
    15        assert src.functions.w_return() is None
    16        # self.assertIsNot(
    17        self.assertIs(
    18            src.functions.w_return(), None
    19        )
    20
    21
    22def test_making_a_function_w_return_none():
    

    the test passes.

  • I add a variable for src.functions.w_return()

    13    # def test_making_a_function_w_return():
    14    def test_making_a_function_w_return(self):
    15        result = src.functions.w_return()
    
  • I use the variable to remove repetition of src.functions.w_return()

    13    # def test_making_a_function_w_return():
    14    def test_making_a_function_w_return(self):
    15        result = src.functions.w_return()
    16        # assert src.functions.w_return() is None
    17        assert result is None
    18        # self.assertIsNot(
    19        self.assertIs(
    20            # src.functions.w_return(), None
    21            result, None
    22        )
    23
    24
    25def test_making_a_function_w_return_none():
    
  • I remove the commented lines from test_making_a_function_w_return

    11        self.assertIs(result, None)
    12
    13    def test_making_a_function_w_return(self):
    14        result = src.functions.w_return()
    15
    16        assert result is None
    17        self.assertIs(result, None)
    18
    19
    20def test_making_a_function_w_return_none():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_making_a_function_w_return to TestFunctions'
    

test_making_a_function_w_return_none with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_making_a_function_w_return_none

19    # def test_making_a_function_w_return_none():
20    def test_making_a_function_w_return_none(self):
21        assert src.functions.w_return_none() is None

green.


REFACTOR: make it better


  • I add a call to the assertIsNot method

    19    # def test_making_a_function_w_return_none():
    20    def test_making_a_function_w_return_none(self):
    21        assert src.functions.w_return_none() is None
    22        self.assertIsNot(
    23            src.functions.w_return_none(), None
    24        )
    25
    26
    27def test_what_happens_after_functions_return():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: None
    
  • I change assertIsNot to assertIs

    19    # def test_making_a_function_w_return_none():
    20    def test_making_a_function_w_return_none(self):
    21        assert src.functions.w_return_none() is None
    22        # self.assertIsNot(
    23        self.assertIs(
    24            src.functions.w_return_none(), None
    25        )
    26
    27
    28def test_what_happens_after_functions_return():
    

    the test passes.

  • I add a variable for src.functions.w_return_none()

    19    # def test_making_a_function_w_return_none():
    20    def test_making_a_function_w_return_none(self):
    21        result = src.functions.w_return_none()
    
  • I use the variable to remove repetition of src.functions.w_return_none()

    19    # def test_making_a_function_w_return_none():
    20    def test_making_a_function_w_return_none(self):
    21        result = src.functions.w_return_none()
    22        # assert src.functions.w_return_none() is None
    23        assert result is None
    24        # self.assertIsNot(
    25        self.assertIs(
    26            # src.functions.w_return_none(), None
    27            result, None
    28        )
    29
    30
    31def test_what_happens_after_functions_return():
    
  • I remove the commented lines from test_making_a_function_w_return_none

    17        self.assertIs(result, None)
    18
    19    def test_making_a_function_w_return_none(self):
    20        result = src.functions.w_return_none()
    21
    22        assert result is None
    23        self.assertIs(result, None)
    24
    25
    26def test_what_happens_after_functions_return():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_making_a_function_w_return_none to TestFunctions'
    

test_what_happens_after_functions_return with unittest

RED: make it fail


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

  • I move test_what_happens_after_functions_return to make it a method of the TestFunctions class

    23        self.assertIs(result, None)
    24
    25    def test_what_happens_after_functions_return():
    26        assert (
    27            src.functions
    28               .return_leaves_the_function()
    29        ) is None
    30
    31
    32def test_constant_function():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_what_happens_after_functions_return()
        takes 0 positional arguments but 1 was given
    

GREEN: make it pass


I add self to the parentheses of test_what_happens_after_functions_return

25    # def test_what_happens_after_functions_return():
26    def test_what_happens_after_functions_return(self):

green again.


REFACTOR: make it better


  • I add a call to the assertIsNot method

    25    # def test_what_happens_after_functions_return():
    26    def test_what_happens_after_functions_return(self):
    27        assert (
    28            src.functions
    29               .return_leaves_the_function()
    30        ) is None
    31        self.assertIsNot(
    32            (
    33                src.functions
    34                   .return_leaves_the_function()
    35            ),
    36            None
    37        )
    38
    39
    40def test_constant_function():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: unexpectedly identical: None
    
  • I change assertIsNot to assertIs

    25    # def test_what_happens_after_functions_return():
    26    def test_what_happens_after_functions_return(self):
    27        assert (
    28            src.functions
    29               .return_leaves_the_function()
    30        ) is None
    31        # self.assertIsNot(
    32        self.assertIs(
    33            (
    34                src.functions
    35                   .return_leaves_the_function()
    36            ),
    37            None
    38        )
    39
    40
    41def test_constant_function():
    

    the test passes.

  • I add a variable for src.functions.return_leaves_the_function()

    25    # def test_what_happens_after_functions_return():
    26    def test_what_happens_after_functions_return(self):
    27        result = src.functions.return_leaves_the_function()
    
  • I use the variable to remove repetition of src.functions.return_leaves_the_function()

    25    # def test_what_happens_after_functions_return():
    26    def test_what_happens_after_functions_return(self):
    27        result = src.functions.return_leaves_the_function()
    28        # assert (
    29        #     src.functions
    30        #        .return_leaves_the_function()
    31        # ) is None
    32        # self.assertIsNot(
    33        # self.assertIs(
    34        #     (
    35        #         src.functions
    36        #            .return_leaves_the_function()
    37        #     ),
    38        #     None
    39        # )
    40        assert result is None
    41        self.assertIs(result, None)
    42
    43
    44def test_constant_function():
    
  • I remove the commented lines from test_what_happens_after_functions_return

    23        self.assertIs(result, None)
    24
    25    def test_what_happens_after_functions_return(self):
    26        result = src.functions.return_leaves_the_function()
    27
    28        assert result is None
    29        self.assertIs(result, None)
    30
    31
    32def test_constant_function():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_what_happens_after_functions_return to TestFunctions'
    

test_constant_function with unittest

RED: make it fail


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

  • I move test_constant_function to make it a method of the TestFunctions class

    29        self.assertIs(result, None)
    30
    31    def test_constant_function():
    32        assert src.functions.constant() == 'the same thing'
    33
    34
    35def test_identity_function():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_constant_function()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance


GREEN: make it pass


I add self to the parentheses of test_constant_function

31    # def test_constant_function():
32    def test_constant_function(self):
33        assert src.functions.constant() == 'the same thing'

green again.


REFACTOR: make it better


  • I add a call to the assertNotEqual method

    31    # def test_constant_function():
    32    def test_constant_function(self):
    33        assert src.functions.constant() == 'the same thing'
    34        self.assertNotEqual(
    35            src.functions.constant(),
    36            'the same thing'
    37        )
    38
    39
    40def test_identity_function():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: 'the same thing' == 'the same thing'
    
  • I change assertNotEqual to assertEqual for the first assertion

    31    # def test_constant_function():
    32    def test_constant_function(self):
    33        assert src.functions.constant() == 'the same thing'
    34        # self.assertNotEqual(
    35        self.assertEqual(
    36            src.functions.constant(),
    37            'the same thing'
    38        )
    39
    40
    41def test_identity_function():
    

    the test passes.

  • I add variables for src.functions.constant() and 'the same thing'

    31    # def test_constant_function():
    32    def test_constant_function(self):
    33        result = src.functions.constant()
    34        expectation = 'the same thing'
    35        assert src.functions.constant() == 'the same thing'
    
  • I use the variable to remove repetition of src.functions.constant() and 'the same thing'

    31    # def test_constant_function():
    32    def test_constant_function(self):
    33        result = src.functions.constant()
    34        expectation = 'the same thing'
    35        # assert src.functions.constant() == 'the same thing'
    36        # self.assertNotEqual(
    37        # self.assertEqual(
    38        #     src.functions.constant(),
    39        #     'the same thing'
    40        # )
    41        assert result == expectation
    42        self.assertEqual(result, expectation)
    43
    44
    45def test_identity_function():
    
  • I remove the commented lines from test_constant_function

    29        self.assertIs(result, None)
    30
    31    def test_constant_function(self):
    32        result = src.functions.constant()
    33        expectation = 'the same thing'
    34
    35        assert result == expectation
    36        self.assertEqual(result, expectation)
    37
    38
    39def test_identity_function():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_constant_function to TestFunctions'
    

test_identity_function with unittest

RED: make it fail


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

  • I move test_identity_function to make it a method of the TestFunctions class

    36        self.assertEqual(result, expectation)
    37
    38    def test_identity_function():
    39        assert src.functions.identity(None) == None
    40        assert src.functions.identity(object) == object
    41
    42
    43def test_why_use_a_function():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_identity_function()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_identity_function

38    # def test_identity_function():
39    def test_identity_function(self):

the test is green again.


REFACTOR: make it better


  • I add calls to the assertNotEqual method

    38    # def test_identity_function():
    39    def test_identity_function(self):
    40        assert src.functions.identity(None) == None
    41        self.assertNotEqual(
    42            src.functions.identity(None), None
    43        )
    44        assert src.functions.identity(object) == object
    45        self.assertNotEqual(
    46            src.functions.identity(object), object
    47        )
    48
    49
    50def test_why_use_a_function():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None == None
    
  • I change assertNotEqual to assertEqual for the first assertion in test_identity_function

    38    # def test_identity_function():
    39    def test_identity_function(self):
    40        assert src.functions.identity(None) == None
    41        # self.assertNotEqual(
    42        self.assertEqual(
    43            src.functions.identity(None), None
    44        )
    45        assert src.functions.identity(object) == object
    46        self.assertNotEqual(
    47            src.functions.identity(object), object
    48        )
    49
    50
    51def test_why_use_a_function():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: <class 'object'> == <class 'object'>
    
  • I change assertNotEqual to assertEqual for the second assertion in test_identity_function

    38    # def test_identity_function():
    39    def test_identity_function(self):
    40        assert src.functions.identity(None) == None
    41        # self.assertNotEqual(
    42        self.assertEqual(
    43            src.functions.identity(None), None
    44        )
    45        assert src.functions.identity(object) == object
    46        # self.assertNotEqual(
    47        self.assertEqual(
    48            src.functions.identity(object), object
    49        )
    50
    51
    52def test_why_use_a_function():
    

    the test passes.

  • I add variables for src.functions.identity

    38    # def test_identity_function():
    39    def test_identity_function(self):
    40        result = src.functions.identity(None)
    41        assert src.functions.identity(None) == None
    42        # self.assertNotEqual(
    43        self.assertEqual(
    44            src.functions.identity(None), None
    45        )
    46
    47        result = src.functions.identity(object)
    48        assert src.functions.identity(object) == object
    49        # self.assertNotEqual(
    50        self.assertEqual(
    51            src.functions.identity(object), object
    52        )
    53
    54
    55def test_why_use_a_function():
    
  • I use the variables to remove repetition of src.functions.identity

    38    # def test_identity_function():
    39    def test_identity_function(self):
    40        result = src.functions.identity(None)
    41        # assert src.functions.identity(None) == None
    42        assert result == None
    43        # self.assertNotEqual(
    44        self.assertEqual(
    45            # src.functions.identity(None), None
    46            result, None
    47        )
    48
    49        result = src.functions.identity(object)
    50        # assert src.functions.identity(object) == object
    51        assert result == object
    52        # self.assertNotEqual(
    53        self.assertEqual(
    54            # src.functions.identity(object), object
    55            result, object
    56        )
    57
    58
    59def test_why_use_a_function():
    
  • I remove the commented lines from test_identity_function

    36        self.assertEqual(result, expectation)
    37
    38    def test_identity_function(self):
    39        result = src.functions.identity(None)
    40
    41        assert result == None
    42        self.assertEqual(result, None)
    43
    44        result = src.functions.identity(object)
    45
    46        assert result == object
    47        self.assertEqual(result, object)
    48
    49
    50def test_why_use_a_function():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_identity_function to TestFunctions'
    

test_why_use_a_function with unittest

RED: make it fail


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

  • I move test_why_use_a_function to make it a method of the TestFunctions class

    47        self.assertEqual(result, object)
    48
    49    def test_why_use_a_function():
    50        def add_x(number):
    51            return 3 + number
    52
    53        assert add_x(0) == 3
    54        assert add_x(1) == 4
    55        assert add_x(2) == 5
    56        assert add_x(3) == 6
    57        assert add_x(4) == 7
    58        assert add_x(5) == 8
    59        assert add_x(6) == 9
    60        assert add_x(7) == 10
    61        assert add_x(8) == 11
    62        assert add_x(9) == 12
    63
    64
    65def test_positional_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_why_use_a_function()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_why_use_a_function

49    # def test_why_use_a_function():
50    def test_why_use_a_function(self):
51        def add_x(number):

green again.


REFACTOR: make it better


  • I add calls to the assertNotEqual method in test_why_use_a_function

    49    # def test_why_use_a_function():
    50    def test_why_use_a_function(self):
    51        def add_x(number):
    52            return 3 + number
    53
    54        assert add_x(0) == 3
    55        self.assertNotEqual(add_x(0), 3)
    56        assert add_x(1) == 4
    57        self.assertNotEqual(add_x(1), 4)
    
    58        assert add_x(2) == 5
    59        self.assertNotEqual(add_x(2), 5)
    60        assert add_x(3) == 6
    61        self.assertNotEqual(add_x(3), 6)
    62        assert add_x(4) == 7
    63        self.assertNotEqual(add_x(4), 7)
    64        assert add_x(5) == 8
    65        self.assertNotEqual(add_x(5), 8)
    
    66        assert add_x(6) == 9
    67        self.assertNotEqual(add_x(6), 9)
    68        assert add_x(7) == 10
    69        self.assertNotEqual(add_x(7), 10)
    70        assert add_x(8) == 11
    71        self.assertNotEqual(add_x(8), 11)
    72        assert add_x(9) == 12
    73        self.assertNotEqual(add_x(9), 12)
    74
    75
    76def test_positional_arguments():
    

    the terminal is my friend, and shows AssertionError.

  • I change the calls from assertNotEqual to assertEqual in test_why_use_a_function

    49    # def test_why_use_a_function():
    50    def test_why_use_a_function(self):
    51        def add_x(number):
    52            return 3 + number
    53
    54        assert add_x(0) == 3
    55        # self.assertNotEqual(add_x(0), 3)
    56        self.assertEqual(add_x(0), 3)
    57        assert add_x(1) == 4
    58        # self.assertNotEqual(add_x(1), 4)
    59        self.assertEqual(add_x(1), 4)
    
    60        assert add_x(2) == 5
    61        # self.assertNotEqual(add_x(2), 5)
    62        self.assertEqual(add_x(2), 5)
    63        assert add_x(3) == 6
    64        # self.assertNotEqual(add_x(3), 6)
    65        self.assertEqual(add_x(3), 6)
    
    66        assert add_x(4) == 7
    67        # self.assertNotEqual(add_x(4), 7)
    68        self.assertEqual(add_x(4), 7)
    69        assert add_x(5) == 8
    70        # self.assertNotEqual(add_x(5), 8)
    71        self.assertEqual(add_x(5), 8)
    
    72        assert add_x(6) == 9
    73        # self.assertNotEqual(add_x(6), 9)
    74        self.assertEqual(add_x(6), 9)
    75        assert add_x(7) == 10
    76        # self.assertNotEqual(add_x(7), 10)
    77        self.assertEqual(add_x(7), 10)
    
    78        assert add_x(8) == 11
    79        # self.assertNotEqual(add_x(8), 11)
    80        self.assertEqual(add_x(8), 11)
    81        assert add_x(9) == 12
    82        # self.assertNotEqual(add_x(9), 12)
    83        self.assertEqual(add_x(9), 12)
    84
    85
    86def test_positional_arguments():
    

    the test passes.

  • I add variables for add_x and the expectations

    49    # def test_why_use_a_function():
    50    def test_why_use_a_function(self):
    51        def add_x(number):
    52            return 3 + number
    53
    54        result = add_x(0)
    55        expectation = 3
    56        assert add_x(0) == 3
    57        # self.assertNotEqual(add_x(0), 3)
    58        self.assertEqual(add_x(0), 3)
    
    60        result = add_x(1)
    61        expectation = 4
    62        assert add_x(1) == 4
    63        # self.assertNotEqual(add_x(1), 4)
    64        self.assertEqual(add_x(1), 4)
    
    66        result = add_x(2)
    67        expectation = 5
    68        assert add_x(2) == 5
    69        # self.assertNotEqual(add_x(2), 5)
    70        self.assertEqual(add_x(2), 5)
    
    72        result = add_x(3)
    73        expectation = 6
    74        assert add_x(3) == 6
    75        # self.assertNotEqual(add_x(3), 6)
    76        self.assertEqual(add_x(3), 6)
    
    78        result = add_x(4)
    79        expectation = 7
    80        assert add_x(4) == 7
    81        # self.assertNotEqual(add_x(4), 7)
    82        self.assertEqual(add_x(4), 7)
    
    84        result = add_x(5)
    85        expectation = 8
    86        assert add_x(5) == 8
    87        # self.assertNotEqual(add_x(5), 8)
    88        self.assertEqual(add_x(5), 8)
    
    90        result = add_x(6)
    91        expectation = 9
    92        assert add_x(6) == 9
    93        # self.assertNotEqual(add_x(6), 9)
    94        self.assertEqual(add_x(6), 9)
    
     96        result = add_x(7)
     97        expectation = 10
     98        assert add_x(7) == 10
     99        # self.assertNotEqual(add_x(7), 10)
    100        self.assertEqual(add_x(7), 10)
    
    102        result = add_x(8)
    103        expectation = 11
    104        assert add_x(8) == 11
    105        # self.assertNotEqual(add_x(8), 11)
    106        self.assertEqual(add_x(8), 11)
    
    108        result = add_x(9)
    109        expectation = 12
    110        assert add_x(9) == 12
    111        # self.assertNotEqual(add_x(9), 12)
    112        self.assertEqual(add_x(9), 12)
    113
    114
    115def test_positional_arguments():
    
  • I use the variables to remove repetition of add_x and the expectations

    49    # def test_why_use_a_function():
    50    def test_why_use_a_function(self):
    51        def add_x(number):
    52            return 3 + number
    53
    54        result = add_x(0)
    55        expectation = 3
    56        # assert add_x(0) == 3
    57        # self.assertNotEqual(add_x(0), 3)
    58        # self.assertEqual(add_x(0), 3)
    59        assert result == expectation
    60        self.assertEqual(result, expectation)
    
    62        result = add_x(1)
    63        expectation = 4
    64        # assert add_x(1) == 4
    65        # self.assertNotEqual(add_x(1), 4)
    66        # self.assertEqual(add_x(1), 4)
    67        assert result == expectation
    68        self.assertEqual(result, expectation)
    
    70        result = add_x(2)
    71        expectation = 5
    72        # assert add_x(2) == 5
    73        # self.assertNotEqual(add_x(2), 5)
    74        # self.assertEqual(add_x(2), 5)
    75        assert result == expectation
    76        self.assertEqual(result, expectation)
    
    78        result = add_x(3)
    79        expectation = 6
    80        # assert add_x(3) == 6
    81        # self.assertNotEqual(add_x(3), 6)
    82        # self.assertEqual(add_x(3), 6)
    83        assert result == expectation
    84        self.assertEqual(result, expectation)
    
    86        result = add_x(4)
    87        expectation = 7
    88        # assert add_x(4) == 7
    89        # self.assertNotEqual(add_x(4), 7)
    90        # self.assertEqual(add_x(4), 7)
    91        assert result == expectation
    92        self.assertEqual(result, expectation)
    
     94        result = add_x(5)
     95        expectation = 8
     96        # assert add_x(5) == 8
     97        # self.assertNotEqual(add_x(5), 8)
     98        # self.assertEqual(add_x(5), 8)
     99        assert result == expectation
    100        self.assertEqual(result, expectation)
    
    102        result = add_x(6)
    103        expectation = 9
    104        # assert add_x(6) == 9
    105        # self.assertNotEqual(add_x(6), 9)
    106        # self.assertEqual(add_x(6), 9)
    107        assert result == expectation
    108        self.assertEqual(result, expectation)
    
    110        result = add_x(7)
    111        expectation = 10
    112        # assert add_x(7) == 10
    113        # self.assertNotEqual(add_x(7), 10)
    114        # self.assertEqual(add_x(7), 10)
    115        assert result == expectation
    116        self.assertEqual(result, expectation)
    
    118        result = add_x(8)
    119        expectation = 11
    120        # assert add_x(8) == 11
    121        # self.assertNotEqual(add_x(8), 11)
    122        # self.assertEqual(add_x(8), 11)
    123        assert result == expectation
    124        self.assertEqual(result, expectation)
    
    126        result = add_x(9)
    127        expectation = 12
    128        # assert add_x(9) == 12
    129        # self.assertNotEqual(add_x(9), 12)
    130        # self.assertEqual(add_x(9), 12)
    131        assert result == expectation
    132        self.assertEqual(result, expectation)
    133
    134
    135def test_positional_arguments():
    

    the test is still green.

  • I remove the commented lines from test_why_use_a_function

    47        self.assertEqual(result, object)
    48
    49    def test_why_use_a_function(self):
    50        def add_x(number):
    51            return 3 + number
    52
    53        result = add_x(0)
    54        expectation = 3
    55        assert result == expectation
    56        self.assertEqual(result, expectation)
    
    58        result = add_x(1)
    59        expectation = 4
    60        assert result == expectation
    61        self.assertEqual(result, expectation)
    
    63        result = add_x(2)
    64        expectation = 5
    65        assert result == expectation
    66        self.assertEqual(result, expectation)
    
    68        result = add_x(3)
    69        expectation = 6
    70        assert result == expectation
    71        self.assertEqual(result, expectation)
    
    73        result = add_x(4)
    74        expectation = 7
    75        assert result == expectation
    76        self.assertEqual(result, expectation)
    
    78        result = add_x(5)
    79        expectation = 8
    80        assert result == expectation
    81        self.assertEqual(result, expectation)
    
    83        result = add_x(6)
    84        expectation = 9
    85        assert result == expectation
    86        self.assertEqual(result, expectation)
    
    88        result = add_x(7)
    89        expectation = 10
    90        assert result == expectation
    91        self.assertEqual(result, expectation)
    
    93        result = add_x(8)
    94        expectation = 11
    95        assert result == expectation
    96        self.assertEqual(result, expectation)
    
     98        result = add_x(9)
     99        expectation = 12
    100        assert result == expectation
    101        self.assertEqual(result, expectation)
    102
    103
    104def test_positional_arguments():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_why_use_a_function to TestFunctions'
    

test_positional_arguments with unittest

RED: make it fail


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

  • I move test_positional_arguments to make it a method of the TestFunctions class

    101        self.assertEqual(result, expectation)
    102
    103    def test_positional_arguments():
    104        positional_arguments = (
    105            src.functions.positional_arguments
    106        )
    107        first, last = 'first', 'last'
    
    109        assert (
    110            positional_arguments(first, last)
    111        == (first, last)
    112        )
    113        assert (
    114            positional_arguments(last, first)
    115        == (last, first)
    116        )
    117        assert (
    118            positional_arguments(0, 1)
    119        == (0, 1)
    120        )
    
    122        a_tuple = (1, 2, 3, 'n')
    123        a_list = [1, 2, 3, 'n']
    124        assert (
    125            positional_arguments(a_tuple, a_list)
    126        == (a_tuple, a_list)
    127        )
    
    129        keyword_arguments = (
    130            src.functions.keyword_arguments
    131        )
    132        a_set = {1, 2, 3, 'n'}
    133        a_dictionary = {'key': 'value'}
    134        assert (
    135            keyword_arguments(
    136                a_set, a_dictionary,
    137            )
    138        == (a_set, a_dictionary)
    139        )
    140
    141
    142def test_keyword_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_positional_arguments()
        takes 0 positional arguments but 1 was given
    

    because …


GREEN: make it pass


I add self to the parentheses of test_positional_arguments

103    # def test_positional_arguments():
104    def test_positional_arguments(self):

green.


REFACTOR: make it better


  • I add calls to the assertNotEqual method in test_positional_arguments

    103    # def test_positional_arguments():
    104    def test_positional_arguments(self):
    105        positional_arguments = (
    106            src.functions.positional_arguments
    107        )
    108        first, last = 'first', 'last'
    109
    110        assert (
    111            positional_arguments(first, last)
    112        == (first, last)
    113        )
    114        self.assertNotEqual(
    115            positional_arguments(first, last),
    116            (first, last)
    117        )
    
    118        assert (
    119            positional_arguments(last, first)
    120        == (last, first)
    121        )
    122        self.assertNotEqual(
    123            positional_arguments(last, first),
    124            (last, first)
    125        )
    
    126        assert (
    127            positional_arguments(0, 1)
    128        == (0, 1)
    129        )
    130        self.assertNotEqual(
    131            positional_arguments(0, 1), (0, 1)
    132        )
    
    134        a_tuple = (1, 2, 3, 'n')
    135        a_list = [1, 2, 3, 'n']
    136        assert (
    137            positional_arguments(a_tuple, a_list)
    138        == (a_tuple, a_list)
    139        )
    140        self.assertNotEqual(
    141            positional_arguments(a_tuple, a_list),
    142            (a_tuple, a_list)
    143        )
    
    145        keyword_arguments = (
    146            src.functions.keyword_arguments
    147        )
    148        a_set = {1, 2, 3, 'n'}
    149        a_dictionary = {'key': 'value'}
    150        assert (
    151            keyword_arguments(
    152                a_set, a_dictionary,
    153            )
    154        == (a_set, a_dictionary)
    155        )
    156        self.assertNotEqual(
    157            keyword_arguments(
    158                a_set, a_dictionary,
    159            ),
    160            (a_set, a_dictionary)
    161        )
    162
    163
    164def test_keyword_arguments():
    

    the terminal is my friend, and shows AssertionError.

  • I change the calls from assertNotEqual to assertEqual in test_positional_arguments

    103    # def test_positional_arguments():
    104    def test_positional_arguments(self):
    105        positional_arguments = (
    106            src.functions.positional_arguments
    107        )
    108        first, last = 'first', 'last'
    109
    110        assert (
    111            positional_arguments(first, last)
    112        == (first, last)
    113        )
    114        # self.assertNotEqual(
    115        self.assertEqual(
    116            positional_arguments(first, last),
    117            (first, last)
    118        )
    
    119        assert (
    120            positional_arguments(last, first)
    121        == (last, first)
    122        )
    123        # self.assertNotEqual(
    124        self.assertEqual(
    125            positional_arguments(last, first),
    126            (last, first)
    127        )
    
    128        assert (
    129            positional_arguments(0, 1)
    130        == (0, 1)
    131        )
    132        # self.assertNotEqual(
    133        self.assertEqual(
    134            positional_arguments(0, 1), (0, 1)
    135        )
    
    137        a_tuple = (1, 2, 3, 'n')
    138        a_list = [1, 2, 3, 'n']
    139        assert (
    140            positional_arguments(a_tuple, a_list)
    141        == (a_tuple, a_list)
    142        )
    143        # self.assertNotEqual(
    144        self.assertEqual(
    145            positional_arguments(a_tuple, a_list),
    146            (a_tuple, a_list)
    147        )
    
    149        keyword_arguments = (
    150            src.functions.keyword_arguments
    151        )
    152        a_set = {1, 2, 3, 'n'}
    153        a_dictionary = {'key': 'value'}
    154        assert (
    155            keyword_arguments(
    156                a_set, a_dictionary,
    157            )
    158        == (a_set, a_dictionary)
    159        )
    160        # self.assertNotEqual(
    161        self.assertEqual(
    162            keyword_arguments(
    163                a_set, a_dictionary,
    164            ),
    165            (a_set, a_dictionary)
    166        )
    167
    168
    169def test_keyword_arguments():
    

    the test passes.

  • I add variables for the calls to positional_arguments, keyword_arguments and my expectations

    103    # def test_positional_arguments():
    104    def test_positional_arguments(self):
    105        positional_arguments = (
    106            src.functions.positional_arguments
    107        )
    108        first, last = 'first', 'last'
    109
    110        reality = positional_arguments(first, last)
    111        my_expectation = (first, last)
    112        assert (
    113            positional_arguments(first, last)
    114        == (first, last)
    115        )
    116        # self.assertNotEqual(
    
    117        self.assertEqual(
    118            positional_arguments(first, last),
    119            (first, last)
    120        )
    121
    122        reality = positional_arguments(last, first)
    123        my_expectation = (last, first)
    124        assert (
    125            positional_arguments(last, first)
    126        == (last, first)
    127        )
    128        # self.assertNotEqual(
    
    129        self.assertEqual(
    130            positional_arguments(last, first),
    131            (last, first)
    132        )
    133
    134        reality = positional_arguments(0, 1)
    135        my_expectation = (0, 1)
    136        assert (
    137            positional_arguments(0, 1)
    138        == (0, 1)
    139        )
    140        # self.assertNotEqual(
    141        self.assertEqual(
    142            positional_arguments(0, 1), (0, 1)
    143        )
    
    145        a_tuple = (1, 2, 3, 'n')
    146        a_list = [1, 2, 3, 'n']
    147
    148        reality = positional_arguments(
    149            a_tuple, a_list
    150        )
    151        my_expectation = (a_tuple, a_list)
    152        assert (
    153            positional_arguments(a_tuple, a_list)
    154        == (a_tuple, a_list)
    155        )
    156        # self.assertNotEqual(
    157        self.assertEqual(
    158            positional_arguments(a_tuple, a_list),
    159            (a_tuple, a_list)
    160        )
    
    162        keyword_arguments = (
    163            src.functions.keyword_arguments
    164        )
    165        a_set = {1, 2, 3, 'n'}
    166        a_dictionary = {'key': 'value'}
    167
    168        reality = keyword_arguments(
    169            a_set, a_dictionary,
    170        )
    171        my_expectation = (a_set, a_dictionary)
    172        assert (
    173            keyword_arguments(
    174                a_set, a_dictionary,
    175            )
    176        == (a_set, a_dictionary)
    177        )
    178        # self.assertNotEqual(
    179        self.assertEqual(
    180            keyword_arguments(
    181                a_set, a_dictionary,
    182            ),
    183            (a_set, a_dictionary)
    184        )
    185
    186
    187def test_keyword_arguments():
    
  • I use the variables to remove repetition of the calls to positional_arguments, keyword_arguments and my expectations, from test_positional_arguments

    103    # def test_positional_arguments():
    104    def test_positional_arguments(self):
    105        positional_arguments = (
    106            src.functions.positional_arguments
    107        )
    108        first, last = 'first', 'last'
    109
    110        reality = positional_arguments(first, last)
    111        my_expectation = (first, last)
    112        # assert (
    113        #     positional_arguments(first, last)
    114        # == (first, last)
    115        # )
    116        # self.assertNotEqual(
    117        # self.assertEqual(
    118        #     positional_arguments(first, last),
    119        #     (first, last)
    120        # )
    121        assert reality == my_expectation
    122        self.assertEqual(reality, my_expectation)
    
    124        reality = positional_arguments(last, first)
    125        my_expectation = (last, first)
    126        # assert (
    127        #     positional_arguments(last, first)
    128        # == (last, first)
    129        # )
    130        # self.assertNotEqual(
    131        # self.assertEqual(
    132        #     positional_arguments(last, first),
    133        #     (last, first)
    134        # )
    135        assert reality == my_expectation
    136        self.assertEqual(reality, my_expectation)
    
    138        reality = positional_arguments(0, 1)
    139        my_expectation = (0, 1)
    140        # assert (
    141        #     positional_arguments(0, 1)
    142        # == (0, 1)
    143        # )
    144        # self.assertNotEqual(
    145        # self.assertEqual(
    146        #     positional_arguments(0, 1), (0, 1)
    147        # )
    148        assert reality == my_expectation
    149        self.assertEqual(reality, my_expectation)
    
    151        a_tuple = (1, 2, 3, 'n')
    152        a_list = [1, 2, 3, 'n']
    153
    154        reality = positional_arguments(
    155            a_tuple, a_list
    156        )
    157        my_expectation = (a_tuple, a_list)
    158        # assert (
    159        #     positional_arguments(a_tuple, a_list)
    160        # == (a_tuple, a_list)
    161        # )
    162        # self.assertNotEqual(
    163        # self.assertEqual(
    164        #     positional_arguments(a_tuple, a_list),
    165        #     (a_tuple, a_list)
    166        # )
    167        assert reality == my_expectation
    168        self.assertEqual(reality, my_expectation)
    
    170        keyword_arguments = (
    171            src.functions.keyword_arguments
    172        )
    173        a_set = {1, 2, 3, 'n'}
    174        a_dictionary = {'key': 'value'}
    175
    176        reality = keyword_arguments(
    177            a_set, a_dictionary,
    178        )
    179        my_expectation = (a_set, a_dictionary)
    180        # assert (
    181        #     keyword_arguments(
    182        #         a_set, a_dictionary,
    183        #     )
    184        # == (a_set, a_dictionary)
    185        # )
    186        # self.assertNotEqual(
    187        # self.assertEqual(
    188        #     keyword_arguments(
    189        #         a_set, a_dictionary,
    190        #     ),
    191        #     (a_set, a_dictionary)
    192        # )
    193        assert reality == my_expectation
    194        self.assertEqual(reality, my_expectation)
    195
    196
    197def test_keyword_arguments():
    

    the test is still green.

  • I remove the commented lines from test_positional_arguments

    101        self.assertEqual(result, expectation)
    102
    103    def test_positional_arguments(self):
    104        positional_arguments = (
    105            src.functions.positional_arguments
    106        )
    107        first, last = 'first', 'last'
    
    109        reality = positional_arguments(first, last)
    110        my_expectation = (first, last)
    111        assert reality == my_expectation
    112        self.assertEqual(reality, my_expectation)
    
    114        reality = positional_arguments(last, first)
    115        my_expectation = (last, first)
    116        assert reality == my_expectation
    117        self.assertEqual(reality, my_expectation)
    
    119        reality = positional_arguments(0, 1)
    120        my_expectation = (0, 1)
    121        assert reality == my_expectation
    122        self.assertEqual(reality, my_expectation)
    
    124        a_tuple = (1, 2, 3, 'n')
    125        a_list = [1, 2, 3, 'n']
    126
    127        reality = positional_arguments(
    128            a_tuple, a_list
    129        )
    130        my_expectation = (a_tuple, a_list)
    131        assert reality == my_expectation
    132        self.assertEqual(reality, my_expectation)
    
    134        keyword_arguments = (
    135            src.functions.keyword_arguments
    136        )
    137        a_set = {1, 2, 3, 'n'}
    138        a_dictionary = {'key': 'value'}
    139
    140        reality = keyword_arguments(
    141            a_set, a_dictionary,
    142        )
    143        my_expectation = (a_set, a_dictionary)
    144        assert reality == my_expectation
    145        self.assertEqual(reality, my_expectation)
    146
    147
    148def test_keyword_arguments():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_positional_arguments to TestFunctions'
    

test_keyword_arguments with unittest

RED: make it fail


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

  • I move test_keyword_arguments to make it a method of the TestFunctions class

    145        self.assertEqual(reality, my_expectation)
    146
    147    def test_keyword_arguments():
    148        keyword_arguments = (
    149            src.functions.keyword_arguments
    150        )
    151        first, last = 'first', 'last'
    
    153        assert (
    154            keyword_arguments(
    155                first_input=first, last_input=last,
    156            )
    157        == (first, last)
    158        )
    159        assert (
    160            keyword_arguments(
    161                last_input=last, first_input=first,
    162            )
    163        == (first, last)
    164        )
    165        assert (
    166            keyword_arguments(
    167                last_input=0, first_input=1,
    168            )
    169        == (1, 0)
    170        )
    
    172        a_tuple = (1, 2, 3, 'n')
    173        a_list = [1, 2, 3, 'n']
    174        assert (
    175            keyword_arguments(
    176                first_input=a_tuple,
    177                last_input=a_list,
    178            )
    179        == (a_tuple, a_list)
    180        )
    
    182        positional_arguments = (
    183            src.functions.positional_arguments
    184        )
    185        a_set = {1, 2, 3, 'n'}
    186        a_dictionary = {'key': 'value'}
    187        assert (
    188            positional_arguments(
    189                last_input=a_dictionary,
    190                first_input=a_set,
    191            )
    192        == (a_set, a_dictionary)
    193        )
    194
    195
    196def test_args_and_kwargs():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_keyword_arguments()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_keyword_arguments

147    # def test_keyword_arguments():
148    def test_keyword_arguments(self):

green again.


REFACTOR: make it better


  • I add calls to the assertNotEqual method in test_keyword_arguments

    147    # def test_keyword_arguments():
    148    def test_keyword_arguments(self):
    149        keyword_arguments = (
    150            src.functions.keyword_arguments
    151        )
    152        first, last = 'first', 'last'
    153
    154        assert (
    155            keyword_arguments(
    156                first_input=first, last_input=last,
    157            )
    158        == (first, last)
    159        )
    160        self.assertNotEqual(
    161            keyword_arguments(
    162                first_input=first, last_input=last,
    163            ),
    164            (first, last)
    165        )
    
    166        assert (
    167            keyword_arguments(
    168                last_input=last, first_input=first,
    169            )
    170        == (first, last)
    171        )
    172        self.assertNotEqual(
    173            keyword_arguments(
    174                last_input=last, first_input=first,
    175            ),
    176            (first, last)
    177        )
    
    178        assert (
    179            keyword_arguments(
    180                last_input=0, first_input=1,
    181            )
    182        == (1, 0)
    183        )
    184        self.assertNotEqual(
    185            keyword_arguments(
    186                last_input=0, first_input=1,
    187            ),
    188            (1, 0)
    189        )
    
    191        a_tuple = (1, 2, 3, 'n')
    192        a_list = [1, 2, 3, 'n']
    193        assert (
    194            keyword_arguments(
    195                first_input=a_tuple,
    196                last_input=a_list,
    197            )
    198        == (a_tuple, a_list)
    199        )
    200        self.assertEqual(
    201            keyword_arguments(
    202                first_input=a_tuple,
    203                last_input=a_list,
    204            ),
    205            (a_tuple, a_list)
    206        )
    
    208        positional_arguments = (
    209            src.functions.positional_arguments
    210        )
    211        a_set = {1, 2, 3, 'n'}
    212        a_dictionary = {'key': 'value'}
    213        assert (
    214            positional_arguments(
    215                last_input=a_dictionary,
    216                first_input=a_set,
    217            )
    218        == (a_set, a_dictionary)
    219        )
    220        self.assertNotEqual(
    221            positional_arguments(
    222                last_input=a_dictionary,
    223                first_input=a_set,
    224            ),
    225            (a_set, a_dictionary)
    226        )
    227
    228
    229def test_args_and_kwargs():
    

    the terminal is my friend, and shows AssertionError.

  • I change the calls from assertNotEqual to assertEqual in test_keyword_arguments

    147    # def test_keyword_arguments():
    148    def test_keyword_arguments(self):
    149        keyword_arguments = (
    150            src.functions.keyword_arguments
    151        )
    152        first, last = 'first', 'last'
    153
    154        assert (
    155            keyword_arguments(
    156                first_input=first, last_input=last,
    157            )
    158        == (first, last)
    159        )
    160        # self.assertNotEqual(
    161        self.assertEqual(
    162            keyword_arguments(
    163                first_input=first, last_input=last,
    164            ),
    165            (first, last)
    166        )
    
    167        assert (
    168            keyword_arguments(
    169                last_input=last, first_input=first,
    170            )
    171        == (first, last)
    172        )
    173        # self.assertNotEqual(
    174        self.assertEqual(
    175            keyword_arguments(
    176                last_input=last, first_input=first,
    177            ),
    178            (first, last)
    179        )
    
    180        assert (
    181            keyword_arguments(
    182                last_input=0, first_input=1,
    183            )
    184        == (1, 0)
    185        )
    186        # self.assertNotEqual(
    187        self.assertEqual(
    188            keyword_arguments(
    189                last_input=0, first_input=1,
    190            ),
    191            (1, 0)
    192        )
    
    194        a_tuple = (1, 2, 3, 'n')
    195        a_list = [1, 2, 3, 'n']
    196        assert (
    197            keyword_arguments(
    198                first_input=a_tuple,
    199                last_input=a_list,
    200            )
    201        == (a_tuple, a_list)
    202        )
    203        # self.assertNotEqual(
    204        self.assertEqual(
    205            keyword_arguments(
    206                first_input=a_tuple,
    207                last_input=a_list,
    208            ),
    209            (a_tuple, a_list)
    210        )
    
    212        positional_arguments = (
    213            src.functions.positional_arguments
    214        )
    215        a_set = {1, 2, 3, 'n'}
    216        a_dictionary = {'key': 'value'}
    217        assert (
    218            positional_arguments(
    219                last_input=a_dictionary,
    220                first_input=a_set,
    221            )
    222        == (a_set, a_dictionary)
    223        )
    224        # self.assertNotEqual(
    225        self.assertEqual(
    226            positional_arguments(
    227                last_input=a_dictionary,
    228                first_input=a_set,
    229            ),
    230            (a_set, a_dictionary)
    231        )
    232
    233
    234def test_args_and_kwargs():
    

    the test passes.

  • I add variables for the calls to keyword_arguments, positional_arguments and my expectations

    147    # def test_keyword_arguments():
    148    def test_keyword_arguments(self):
    149        keyword_arguments = (
    150            src.functions.keyword_arguments
    151        )
    152        first, last = 'first', 'last'
    153
    154        reality = keyword_arguments(
    155            first_input=first, last_input=last,
    156        )
    157        my_expectation = (first, last)
    158        assert (
    159            keyword_arguments(
    160                first_input=first, last_input=last,
    161            )
    162        == (first, last)
    163        )
    164        # self.assertNotEqual(
    
    165        self.assertEqual(
    166            keyword_arguments(
    167                first_input=first, last_input=last,
    168            ),
    169            (first, last)
    170        )
    171
    172        reality = keyword_arguments(
    173            last_input=last, first_input=first,
    174        )
    175        my_expectation = (first, last)
    176        assert (
    177            keyword_arguments(
    178                last_input=last, first_input=first,
    179            )
    180        == (first, last)
    181        )
    182        # self.assertNotEqual(
    
    183        self.assertEqual(
    184            keyword_arguments(
    185                last_input=last, first_input=first,
    186            ),
    187            (first, last)
    188        )
    189
    190        reality = keyword_arguments(
    191            last_input=0, first_input=1,
    192        )
    193        my_expectation = (1, 0)
    194        assert (
    195            keyword_arguments(
    196                last_input=0, first_input=1,
    197            )
    198        == (1, 0)
    199        )
    200        # self.assertNotEqual(
    201        self.assertEqual(
    202            keyword_arguments(
    203                last_input=0, first_input=1,
    204            ),
    205            (1, 0)
    206        )
    
    208        a_tuple = (1, 2, 3, 'n')
    209        a_list = [1, 2, 3, 'n']
    210
    211        reality = keyword_arguments(
    212            first_input=a_tuple,
    213            last_input=a_list,
    214        )
    215        my_expectation = (a_tuple, a_list)
    216        assert (
    217            keyword_arguments(
    218                first_input=a_tuple,
    219                last_input=a_list,
    220            )
    221        == (a_tuple, a_list)
    222        )
    223        # self.assertNotEqual(
    224        self.assertEqual(
    225            keyword_arguments(
    226                first_input=a_tuple,
    227                last_input=a_list,
    228            ),
    229            (a_tuple, a_list)
    230        )
    
    232        positional_arguments = (
    233            src.functions.positional_arguments
    234        )
    235        a_set = {1, 2, 3, 'n'}
    236        a_dictionary = {'key': 'value'}
    237
    238        reality = positional_arguments(
    239            last_input=a_dictionary,
    240            first_input=a_set,
    241        )
    242        my_expectation = (a_set, a_dictionary)
    243        assert (
    244            positional_arguments(
    245                last_input=a_dictionary,
    246                first_input=a_set,
    247            )
    248        == (a_set, a_dictionary)
    249        )
    250        # self.assertNotEqual(
    251        self.assertEqual(
    252            positional_arguments(
    253                last_input=a_dictionary,
    254                first_input=a_set,
    255            ),
    256            (a_set, a_dictionary)
    257        )
    258
    259
    260def test_args_and_kwargs():
    
  • I use the variables to remove repetition of the calls to keyword_arguments, positional_arguments and my expectations, from test_keyword_arguments

    147    # def test_keyword_arguments():
    148    def test_keyword_arguments(self):
    149        keyword_arguments = (
    150            src.functions.keyword_arguments
    151        )
    152        first, last = 'first', 'last'
    153
    154        reality = keyword_arguments(
    155            first_input=first, last_input=last,
    156        )
    157        my_expectation = (first, last)
    158        # assert (
    159        #     keyword_arguments(
    160        #         first_input=first, last_input=last,
    161        #     )
    162        # == (first, last)
    163        # )
    164        # self.assertNotEqual(
    165        # self.assertEqual(
    166        #     keyword_arguments(
    167        #         first_input=first, last_input=last,
    168        #     ),
    169        #     (first, last)
    170        # )
    171        assert reality == my_expectation
    172        self.assertEqual(reality, my_expectation)
    
    174        reality = keyword_arguments(
    175            last_input=last, first_input=first,
    176        )
    177        my_expectation = (first, last)
    178        # assert (
    179        #     keyword_arguments(
    180        #         last_input=last, first_input=first,
    181        #     )
    182        # == (first, last)
    183        # )
    184        # self.assertNotEqual(
    185        # self.assertEqual(
    186        #     keyword_arguments(
    187        #         last_input=last, first_input=first,
    188        #     ),
    189        #     (first, last)
    190        # )
    191        assert reality == my_expectation
    192        self.assertEqual(reality, my_expectation)
    
    194        reality = keyword_arguments(
    195            last_input=0, first_input=1,
    196        )
    197        my_expectation = (1, 0)
    198        # assert (
    199        #     keyword_arguments(
    200        #         last_input=0, first_input=1,
    201        #     )
    202        # == (1, 0)
    203        # )
    204        # self.assertNotEqual(
    205        # self.assertEqual(
    206        #     keyword_arguments(
    207        #         last_input=0, first_input=1,
    208        #     ),
    209        #     (1, 0)
    210        # )
    211        assert reality == my_expectation
    212        self.assertEqual(reality, my_expectation)
    
    214        a_tuple = (1, 2, 3, 'n')
    215        a_list = [1, 2, 3, 'n']
    216
    217        reality = keyword_arguments(
    218            first_input=a_tuple,
    219            last_input=a_list,
    220        )
    221        my_expectation = (a_tuple, a_list)
    222        # assert (
    223        #     keyword_arguments(
    224        #         first_input=a_tuple,
    225        #         last_input=a_list,
    226        #     )
    227        # == (a_tuple, a_list)
    228        # )
    229        # self.assertNotEqual(
    230        # self.assertEqual(
    231        #     keyword_arguments(
    232        #         first_input=a_tuple,
    233        #         last_input=a_list,
    234        #     ),
    235        #     (a_tuple, a_list)
    236        # )
    237        assert reality == my_expectation
    238        self.assertEqual(reality, my_expectation)
    
    240        positional_arguments = (
    241            src.functions.positional_arguments
    242        )
    243        a_set = {1, 2, 3, 'n'}
    244        a_dictionary = {'key': 'value'}
    245
    246        reality = positional_arguments(
    247            last_input=a_dictionary,
    248            first_input=a_set,
    249        )
    250        my_expectation = (a_set, a_dictionary)
    251        # assert (
    252        #     positional_arguments(
    253        #         last_input=a_dictionary,
    254        #         first_input=a_set,
    255        #     )
    256        # == (a_set, a_dictionary)
    257        # )
    258        # self.assertNotEqual(
    259        # self.assertEqual(
    260        #     positional_arguments(
    261        #         last_input=a_dictionary,
    262        #         first_input=a_set,
    263        #     ),
    264        #     (a_set, a_dictionary)
    265        # )
    266        assert reality == my_expectation
    267        self.assertEqual(reality, my_expectation)
    268
    269
    270def test_args_and_kwargs():
    

    the test is still green.

  • I remove the commented lines from test_keyword_arguments

    145        self.assertEqual(reality, my_expectation)
    146
    147    def test_keyword_arguments(self):
    148        keyword_arguments = (
    149            src.functions.keyword_arguments
    150        )
    151        first, last = 'first', 'last'
    
    153        reality = keyword_arguments(
    154            first_input=first, last_input=last,
    155        )
    156        my_expectation = (first, last)
    157        assert reality == my_expectation
    158        self.assertEqual(reality, my_expectation)
    
    160        reality = keyword_arguments(
    161            last_input=last, first_input=first,
    162        )
    163        my_expectation = (first, last)
    164        assert reality == my_expectation
    165        self.assertEqual(reality, my_expectation)
    
    167        reality = keyword_arguments(
    168            last_input=0, first_input=1,
    169        )
    170        my_expectation = (1, 0)
    171        assert reality == my_expectation
    172        self.assertEqual(reality, my_expectation)
    
    174        a_tuple = (1, 2, 3, 'n')
    175        a_list = [1, 2, 3, 'n']
    176
    177        reality = keyword_arguments(
    178            first_input=a_tuple,
    179            last_input=a_list,
    180        )
    181        my_expectation = (a_tuple, a_list)
    182        assert reality == my_expectation
    183        self.assertEqual(reality, my_expectation)
    
    185        positional_arguments = (
    186            src.functions.positional_arguments
    187        )
    188        a_set = {1, 2, 3, 'n'}
    189        a_dictionary = {'key': 'value'}
    
    191        reality = positional_arguments(
    192            last_input=a_dictionary,
    193            first_input=a_set,
    194        )
    195        my_expectation = (a_set, a_dictionary)
    196        assert reality == my_expectation
    197        self.assertEqual(reality, my_expectation)
    198
    199
    200def test_args_and_kwargs():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_keyword_arguments to TestFunctions'
    

test_args_and_kwargs with unittest

RED: make it fail


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

  • I move test_args_and_kwargs to make it a method of the TestFunctions class

    197        self.assertEqual(reality, my_expectation)
    198
    199    def test_args_and_kwargs(self):
    200        first, last = 'first', 'last'
    201
    202        assert (
    203            src.functions.args_and_kwargs(
    204                first, last_input=last,
    205            )
    206        == (first, last)
    207        )
    208
    209
    210def test_optional_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_args_and_kwargs()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_args_and_kwargs

199    # def test_args_and_kwargs():
200    def test_args_and_kwargs(self):
201        first, last = 'first', 'last'

the test is green again.


REFACTOR: make it better


  • I add a call to the assertNotEqual method in test_args_and_kwargs

    199    # def test_args_and_kwargs():
    200    def test_args_and_kwargs(self):
    201        first, last = 'first', 'last'
    202
    203        assert (
    204            src.functions.args_and_kwargs(
    205                first, last_input=last,
    206            )
    207        == (first, last)
    208        )
    209        self.assertNotEqual(
    210            src.functions.args_and_kwargs(
    211                first, last_input=last,
    212            ),
    213            (first, last)
    214        )
    215
    216
    217def test_optional_arguments():
    

    the terminal is my friend, and shows AssertionError

    AssertionError: ('first', 'last') == ('first', 'last')
    
  • I change the call from assertNotEqual to assertEqual in test_args_and_kwargs

    199    # def test_args_and_kwargs():
    200    def test_args_and_kwargs(self):
    201        first, last = 'first', 'last'
    202
    203        assert (
    204            src.functions.args_and_kwargs(
    205                first, last_input=last,
    206            )
    207        == (first, last)
    208        )
    209        # self.assertNotEqual(
    210        self.assertNotEqual(
    211            src.functions.args_and_kwargs(
    212                first, last_input=last,
    213            ),
    214            (first, last)
    215        )
    216
    217
    218def test_optional_arguments():
    

    the test passes.

  • I add variables for the call to src.functions.args_and_kwargs and my expectation

    199    # def test_args_and_kwargs():
    200    def test_args_and_kwargs(self):
    201        first, last = 'first', 'last'
    202
    203        reality = src.functions.args_and_kwargs(
    204            first, last_input=last,
    205        )
    206        my_expectation = (first, last)
    207        assert (
    208            src.functions.args_and_kwargs(
    209                first, last_input=last,
    210            )
    211        == (first, last)
    212        )
    213        # self.assertNotEqual(
    214        self.assertNotEqual(
    215            src.functions.args_and_kwargs(
    216                first, last_input=last,
    217            ),
    218            (first, last)
    219        )
    220
    221
    222def test_optional_arguments():
    
  • I use the variables to remove repetition of the call to src.functions.args_and_kwargs and my expectation, from test_args_and_kwargs

    199    # def test_args_and_kwargs():
    200    def test_args_and_kwargs(self):
    201        first, last = 'first', 'last'
    202
    203        reality = src.functions.args_and_kwargs(
    204            first, last_input=last,
    205        )
    206        my_expectation = (first, last)
    207        # assert (
    208        #     src.functions.args_and_kwargs(
    209        #         first, last_input=last,
    210        #     )
    211        # == (first, last)
    212        # )
    213        # self.assertNotEqual(
    214        # self.assertNotEqual(
    215        #     src.functions.args_and_kwargs(
    216        #         first, last_input=last,
    217        #     ),
    218        #     (first, last)
    219        # )
    220        assert reality == my_expectation
    221        self.assertEqual(reality, my_expectation)
    222
    223
    224def test_optional_arguments():
    

    the test is still green.

  • I remove the commented lines from test_args_and_kwargs

    197        self.assertEqual(reality, my_expectation)
    198
    199    def test_args_and_kwargs(self):
    200        first, last = 'first', 'last'
    201
    202        reality = src.functions.args_and_kwargs(
    203            first, last_input=last,
    204        )
    205        my_expectation = (first, last)
    206        assert reality == my_expectation
    207        self.assertEqual(reality, my_expectation)
    208
    209
    210def test_optional_arguments():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_args_and_kwargs to TestFunctions'
    

test_optional_arguments with unittest

RED: make it fail


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

  • I move test_optional_arguments to make it a method of the TestFunctions class

    207        self.assertEqual(reality, my_expectation)
    208
    209    def test_optional_arguments():
    210        optional_arguments = (
    211            src.functions.optional_arguments
    212        )
    213
    214        first_name, last_name = 'jane', 'doe'
    215
    216        assert (
    217            optional_arguments(
    218                first_name,
    219            )
    220        == (first_name, last_name)
    221        )
    
    223        first_name, blow = 'joe', 'blow'
    224        assert (
    225            optional_arguments(
    226                first_name, blow
    227            )
    228        == (first_name, blow)
    229        )
    
    231        first_name = 'john'
    232        assert (
    233            optional_arguments(
    234                first_input=first_name
    235            )
    236        == (first_name, last_name)
    237        )
    
    239        last_name = 'smith'
    240        assert (
    241            optional_arguments(
    242                last_input=last_name,
    243                first_input=first_name,
    244            )
    245        == (first_name, last_name)
    246        )
    247
    248
    249def test_unknown_number_of_arguments():
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_optional_arguments()
        takes 0 positional arguments but 1 was given
    

GREEN: make it pass


I add self to the parentheses of test_optional_arguments

209    # def test_optional_arguments():
210    def test_optional_arguments(self):

green again.


REFACTOR: make it better


  • I add calls to the assertNotEqual method in test_optional_arguments

    209    # def test_optional_arguments():
    210    def test_optional_arguments(self):
    211        optional_arguments = (
    212            src.functions.optional_arguments
    213        )
    214
    215        first_name, last_name = 'jane', 'doe'
    216
    217        assert (
    218            optional_arguments(
    219                first_name,
    220            )
    221        == (first_name, last_name)
    222        )
    223        self.assertNotEqual(
    224            optional_arguments(
    225                first_name,
    226            ),
    227            (first_name, last_name)
    228        )
    
    230        first_name, blow = 'joe', 'blow'
    231        assert (
    232            optional_arguments(
    233                first_name, blow
    234            )
    235        == (first_name, blow)
    236        )
    237        self.assertNotEqual(
    238            optional_arguments(
    239                first_name, blow
    240            ),
    241            (first_name, blow)
    242        )
    
    244        first_name = 'john'
    245        assert (
    246            optional_arguments(
    247                first_input=first_name
    248            )
    249        == (first_name, last_name)
    250        )
    251        self.assertNotEqual(
    252            optional_arguments(
    253                first_input=first_name
    254            ),
    255            (first_name, last_name)
    256        )
    
    258        last_name = 'smith'
    259        assert (
    260            optional_arguments(
    261                last_input=last_name,
    262                first_input=first_name,
    263            )
    264        == (first_name, last_name)
    265        )
    266        self.assertNotEqual(
    267            optional_arguments(
    268                last_input=last_name,
    269                first_input=first_name,
    270            ),
    271            (first_name, last_name)
    272        )
    273
    274
    275def test_unknown_number_of_arguments():
    

    the terminal is my friend, and shows AssertionError.

  • I change the calls from assertNotEqual to assertEqual in test_optional_arguments

    209    # def test_optional_arguments():
    210    def test_optional_arguments(self):
    211        optional_arguments = (
    212            src.functions.optional_arguments
    213        )
    214
    215        first_name, last_name = 'jane', 'doe'
    216
    217        assert (
    218            optional_arguments(
    219                first_name,
    220            )
    221        == (first_name, last_name)
    222        )
    223        # self.assertNotEqual(
    224        self.assertEqual(
    225            optional_arguments(
    226                first_name,
    227            ),
    228            (first_name, last_name)
    229        )
    
    231        first_name, blow = 'joe', 'blow'
    232        assert (
    233            optional_arguments(
    234                first_name, blow
    235            )
    236        == (first_name, blow)
    237        )
    238        # self.assertNotEqual(
    239        self.assertEqual(
    240            optional_arguments(
    241                first_name, blow
    242            ),
    243            (first_name, blow)
    244        )
    
    246        first_name = 'john'
    247        assert (
    248            optional_arguments(
    249                first_input=first_name
    250            )
    251        == (first_name, last_name)
    252        )
    253        # self.assertNotEqual(
    254        self.assertEqual(
    255            optional_arguments(
    256                first_input=first_name
    257            ),
    258            (first_name, last_name)
    259        )
    
    261        last_name = 'smith'
    262        assert (
    263            optional_arguments(
    264                last_input=last_name,
    265                first_input=first_name,
    266            )
    267        == (first_name, last_name)
    268        )
    269        # self.assertNotEqual(
    270        self.assertEqual(
    271            optional_arguments(
    272                last_input=last_name,
    273                first_input=first_name,
    274            ),
    275            (first_name, last_name)
    276        )
    277
    278
    279def test_unknown_number_of_arguments():
    

    the test passes.

  • I add variables for the calls to src.functions.optional_arguments and my expectations

    209    # def test_optional_arguments():
    210    def test_optional_arguments(self):
    211        optional_arguments = (
    212            src.functions.optional_arguments
    213        )
    214
    215        first_name, last_name = 'jane', 'doe'
    216
    217        reality = optional_arguments(first_name)
    218        my_expectation = (first_name, last_name)
    219        assert (
    220            optional_arguments(
    221                first_name,
    222            )
    223        == (first_name, last_name)
    224        )
    225        # self.assertNotEqual(
    226        self.assertEqual(
    227            optional_arguments(
    228                first_name,
    229            ),
    230            (first_name, last_name)
    231        )
    
    233        first_name, blow = 'joe', 'blow'
    234
    235        reality = optional_arguments(
    236            first_name, blow
    237        )
    238        my_expectation = (first_name, blow)
    239        assert (
    240            optional_arguments(
    241                first_name, blow
    242            )
    243        == (first_name, blow)
    244        )
    245        # self.assertNotEqual(
    246        self.assertEqual(
    247            optional_arguments(
    248                first_name, blow
    249            ),
    250            (first_name, blow)
    251        )
    
    253        first_name = 'john'
    254
    255        reality = optional_arguments(
    256            first_input=first_name
    257        )
    258        my_expectation = (first_name, last_name)
    259        assert (
    260            optional_arguments(
    261                first_input=first_name
    262            )
    263        == (first_name, last_name)
    264        )
    265        # self.assertNotEqual(
    266        self.assertEqual(
    267            optional_arguments(
    268                first_input=first_name
    269            ),
    270            (first_name, last_name)
    271        )
    
    273        last_name = 'smith'
    274
    275        reality = optional_arguments(
    276            last_input=last_name,
    277            first_input=first_name,
    278        )
    279        my_expectation = (first_name, last_name)
    280        assert (
    281            optional_arguments(
    282                last_input=last_name,
    283                first_input=first_name,
    284            )
    285        == (first_name, last_name)
    286        )
    287        # self.assertNotEqual(
    288        self.assertEqual(
    289            optional_arguments(
    290                last_input=last_name,
    291                first_input=first_name,
    292            ),
    293            (first_name, last_name)
    294        )
    295
    296
    297def test_unknown_number_of_arguments():
    
  • I use the variables to remove repetition of the calls to src.functions.optional_arguments and my expectations, from test_optional_arguments

    209    # def test_optional_arguments():
    210    def test_optional_arguments(self):
    211        optional_arguments = (
    212            src.functions.optional_arguments
    213        )
    214
    215        first_name, last_name = 'jane', 'doe'
    216
    217        reality = optional_arguments(first_name)
    218        my_expectation = (first_name, last_name)
    219        # assert (
    220        #     optional_arguments(
    221        #         first_name,
    222        #     )
    223        # == (first_name, last_name)
    224        # )
    225        # self.assertNotEqual(
    226        # self.assertEqual(
    227        #     optional_arguments(
    228        #         first_name,
    229        #     ),
    230        #     (first_name, last_name)
    231        # )
    232        assert reality == my_expectation
    233        self.assertEqual(reality, my_expectation)
    
    235        first_name, blow = 'joe', 'blow'
    236
    237        reality = optional_arguments(
    238            first_name, blow
    239        )
    240        my_expectation = (first_name, blow)
    241        # assert (
    242        #     optional_arguments(
    243        #         first_name, blow
    244        #     )
    245        # == (first_name, blow)
    246        # )
    247        # self.assertNotEqual(
    248        # self.assertEqual(
    249        #     optional_arguments(
    250        #         first_name, blow
    251        #     ),
    252        #     (first_name, blow)
    253        # )
    254        assert reality == my_expectation
    255        self.assertEqual(reality, my_expectation)
    
    257        first_name = 'john'
    258
    259        reality = optional_arguments(
    260            first_input=first_name
    261        )
    262        my_expectation = (first_name, last_name)
    263        # assert (
    264        #     optional_arguments(
    265        #         first_input=first_name
    266        #     )
    267        # == (first_name, last_name)
    268        # )
    269        # self.assertNotEqual(
    270        # self.assertEqual(
    271        #     optional_arguments(
    272        #         first_input=first_name
    273        #     ),
    274        #     (first_name, last_name)
    275        # )
    276        assert reality == my_expectation
    277        self.assertEqual(reality, my_expectation)
    
    279        last_name = 'smith'
    280
    281        reality = optional_arguments(
    282            last_input=last_name,
    283            first_input=first_name,
    284        )
    285        my_expectation = (first_name, last_name)
    286        # assert (
    287        #     optional_arguments(
    288        #         last_input=last_name,
    289        #         first_input=first_name,
    290        #     )
    291        # == (first_name, last_name)
    292        # )
    293        # self.assertNotEqual(
    294        # self.assertEqual(
    295        #     optional_arguments(
    296        #         last_input=last_name,
    297        #         first_input=first_name,
    298        #     ),
    299        #     (first_name, last_name)
    300        # )
    301        assert reality == my_expectation
    302        self.assertEqual(reality, my_expectation)
    303
    304
    305def test_unknown_number_of_arguments():
    

    the test is still green.

  • I remove the commented lines from test_optional_arguments

    207        self.assertEqual(reality, my_expectation)
    208
    209    def test_optional_arguments(self):
    210        optional_arguments = (
    211            src.functions.optional_arguments
    212        )
    213
    214        first_name, last_name = 'jane', 'doe'
    215
    216        reality = optional_arguments(first_name)
    217        my_expectation = (first_name, last_name)
    218        assert reality == my_expectation
    219        self.assertEqual(reality, my_expectation)
    
    221        first_name, blow = 'joe', 'blow'
    222
    223        reality = optional_arguments(
    224            first_name, blow
    225        )
    226        my_expectation = (first_name, blow)
    227        assert reality == my_expectation
    228        self.assertEqual(reality, my_expectation)
    
    230        first_name = 'john'
    231
    232        reality = optional_arguments(
    233            first_input=first_name
    234        )
    235        my_expectation = (first_name, last_name)
    236        assert reality == my_expectation
    237        self.assertEqual(reality, my_expectation)
    
    239        last_name = 'smith'
    240
    241        reality = optional_arguments(
    242            last_input=last_name,
    243            first_input=first_name,
    244        )
    245        my_expectation = (first_name, last_name)
    246        assert reality == my_expectation
    247        self.assertEqual(reality, my_expectation)
    248
    249
    250def test_unknown_number_of_arguments():
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_optional_arguments to TestFunctions'
    

test_unknown_number_of_arguments with unittest

RED: make it fail


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

  • I move test_unknown_number_of_arguments to make it a method of the TestFunctions class

    247            self.assertEqual(reality, my_expectation)
    248
    249    def test_unknown_number_of_arguments():
    250        unknown_number_of_arguments = (
    251            src.functions.unknown_number_of_arguments
    252        )
    253
    254        a_tuple = (0, 1)
    255        a_dictionary = {'a': 2, 'b': 3}
    256        assert (
    257            unknown_number_of_arguments(
    258                *a_tuple, **a_dictionary
    259            )
    260        == (a_tuple, a_dictionary)
    261        )
    
    263        a_tuple = (0, 1)
    264        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
    265        assert (
    266            unknown_number_of_arguments(
    267                *a_tuple, **a_dictionary
    268            )
    269        == (a_tuple, a_dictionary)
    270        )
    
    272        a_tuple = (0, 1, 2)
    273        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
    274        assert (
    275            unknown_number_of_arguments(
    276                *a_tuple, **a_dictionary
    277            )
    278        == (a_tuple, a_dictionary)
    279        )
    
    281        a_tuple = (1, 2, 3, 'n')
    282        assert (
    283            unknown_number_of_arguments(*a_tuple)
    284        == (a_tuple, {})
    285        )
    
    287        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
    288        assert (
    289            unknown_number_of_arguments(**a_dictionary)
    290        == ((), a_dictionary)
    291        )
    
    293        assert (
    294            unknown_number_of_arguments()
    295        == ((), {})
    296        )
    297
    298
    299# Exceptions seen
    

    the terminal is my friend, and shows TypeError

    TypeError:
        TestFunctions.test_unknown_number_of_arguments()
        takes 0 positional arguments but 1 was given
    

    because a method of an instance takes the instance of the class (self) it belongs to as the first argument.


GREEN: make it pass


I add self to the parentheses of test_unknown_number_of_arguments

249    # def test_unknown_number_of_arguments():
250    def test_unknown_number_of_arguments(self):

green.


REFACTOR: make it better


  • I add calls to the assertNotEqual method in test_unknown_number_of_arguments

    249    # def test_unknown_number_of_arguments():
    250    def test_unknown_number_of_arguments(self):
    251        unknown_number_of_arguments = (
    252            src.functions.unknown_number_of_arguments
    253        )
    254
    255        a_tuple = (0, 1)
    256        a_dictionary = {'a': 2, 'b': 3}
    257        assert (
    258            unknown_number_of_arguments(
    259                *a_tuple, **a_dictionary
    260            )
    261        == (a_tuple, a_dictionary)
    262        )
    263        self.assertNotEqual(
    264            unknown_number_of_arguments(
    265                *a_tuple, **a_dictionary
    266            ),
    267            (a_tuple, a_dictionary)
    268        )
    
    270        a_tuple = (0, 1)
    271        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
    272        assert (
    273            unknown_number_of_arguments(
    274                *a_tuple, **a_dictionary
    275            )
    276        == (a_tuple, a_dictionary)
    277        )
    278        self.assertNotEqual(
    279            unknown_number_of_arguments(
    280                *a_tuple, **a_dictionary
    281            ),
    282            (a_tuple, a_dictionary)
    283        )
    
    285        a_tuple = (0, 1, 2)
    286        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
    287        assert (
    288            unknown_number_of_arguments(
    289                *a_tuple, **a_dictionary
    290            )
    291        == (a_tuple, a_dictionary)
    292        )
    293        self.assertNotEqual(
    294            unknown_number_of_arguments(
    295                *a_tuple, **a_dictionary
    296            ),
    297            (a_tuple, a_dictionary)
    298        )
    
    300        a_tuple = (1, 2, 3, 'n')
    301        assert (
    302            unknown_number_of_arguments(*a_tuple)
    303        == (a_tuple, {})
    304        )
    305        self.assertNotEqual(
    306            unknown_number_of_arguments(*a_tuple),
    307            (a_tuple, {})
    308        )
    
    310        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
    311        assert (
    312            unknown_number_of_arguments(**a_dictionary)
    313        == ((), a_dictionary)
    314        )
    315        self.assertNotEqual(
    316            unknown_number_of_arguments(**a_dictionary),
    317            ((), a_dictionary)
    318        )
    
    320        assert (
    321            unknown_number_of_arguments()
    322        == ((), {})
    323        )
    324        self.assertNotEqual(
    325            unknown_number_of_arguments(),
    326            ((), {})
    327        )
    328
    329
    330# Exceptions seen
    

    the terminal is my friend, and shows AssertionError.

  • I change the calls from assertNotEqual to assertEqual in test_unknown_number_of_arguments

    249    # def test_unknown_number_of_arguments():
    250    def test_unknown_number_of_arguments(self):
    251        unknown_number_of_arguments = (
    252            src.functions.unknown_number_of_arguments
    253        )
    254
    255        a_tuple = (0, 1)
    256        a_dictionary = {'a': 2, 'b': 3}
    257        assert (
    258            unknown_number_of_arguments(
    259                *a_tuple, **a_dictionary
    260            )
    261        == (a_tuple, a_dictionary)
    262        )
    263        # self.assertNotEqual(
    264        self.assertEqual(
    265            unknown_number_of_arguments(
    266                *a_tuple, **a_dictionary
    267            ),
    268            (a_tuple, a_dictionary)
    269        )
    
    271        a_tuple = (0, 1)
    272        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
    273        assert (
    274            unknown_number_of_arguments(
    275                *a_tuple, **a_dictionary
    276            )
    277        == (a_tuple, a_dictionary)
    278        )
    279        # self.assertNotEqual(
    280        self.assertEqual(
    281            unknown_number_of_arguments(
    282                *a_tuple, **a_dictionary
    283            ),
    284            (a_tuple, a_dictionary)
    285        )
    
    287        a_tuple = (0, 1, 2)
    288        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
    289        assert (
    290            unknown_number_of_arguments(
    291                *a_tuple, **a_dictionary
    292            )
    293        == (a_tuple, a_dictionary)
    294        )
    295        # self.assertNotEqual(
    296        self.assertEqual(
    297            unknown_number_of_arguments(
    298                *a_tuple, **a_dictionary
    299            ),
    300            (a_tuple, a_dictionary)
    301        )
    
    303        a_tuple = (1, 2, 3, 'n')
    304        assert (
    305            unknown_number_of_arguments(*a_tuple)
    306        == (a_tuple, {})
    307        )
    308        # self.assertNotEqual(
    309        self.assertEqual(
    310            unknown_number_of_arguments(*a_tuple),
    311            (a_tuple, {})
    312        )
    
    314        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
    315        assert (
    316            unknown_number_of_arguments(**a_dictionary)
    317        == ((), a_dictionary)
    318        )
    319        # self.assertNotEqual(
    320        self.assertEqual(
    321            unknown_number_of_arguments(**a_dictionary),
    322            ((), a_dictionary)
    323        )
    
    271        assert (
    272            unknown_number_of_arguments()
    273        == ((), {})
    274        )
    275        # self.assertNotEqual(
    276        self.assertEqual(
    277            unknown_number_of_arguments(),
    278            ((), {})
    279        )
    280
    281
    282# Exceptions seen
    

    the test passes.

  • I add variables for the calls to src.functions.unknown_number_of_arguments and my expectations

    249    # def test_unknown_number_of_arguments():
    250    def test_unknown_number_of_arguments(self):
    251        unknown_number_of_arguments = (
    252            src.functions.unknown_number_of_arguments
    253        )
    254
    255        a_tuple = (0, 1)
    256        a_dictionary = {'a': 2, 'b': 3}
    257
    258        reality = unknown_number_of_arguments(
    259            *a_tuple, **a_dictionary
    260        )
    261        my_expectation = (a_tuple, a_dictionary)
    262        assert (
    263            unknown_number_of_arguments(
    264                *a_tuple, **a_dictionary
    265            )
    266        == (a_tuple, a_dictionary)
    267        )
    268        # self.assertNotEqual(
    269        self.assertEqual(
    270            unknown_number_of_arguments(
    271                *a_tuple, **a_dictionary
    272            ),
    273            (a_tuple, a_dictionary)
    274        )
    
    276        a_tuple = (0, 1)
    277        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
    278
    279        reality = unknown_number_of_arguments(
    280            *a_tuple, **a_dictionary
    281        )
    282        my_expectation = (a_tuple, a_dictionary)
    283        assert (
    284            unknown_number_of_arguments(
    285                *a_tuple, **a_dictionary
    286            )
    287        == (a_tuple, a_dictionary)
    288        )
    289        # self.assertNotEqual(
    290        self.assertEqual(
    291            unknown_number_of_arguments(
    292                *a_tuple, **a_dictionary
    293            ),
    294            (a_tuple, a_dictionary)
    295        )
    
    297        a_tuple = (0, 1, 2)
    298        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
    299
    300        reality = unknown_number_of_arguments(
    301            *a_tuple, **a_dictionary
    302        )
    303        my_expectation = (a_tuple, a_dictionary)
    304        assert (
    305            unknown_number_of_arguments(
    306                *a_tuple, **a_dictionary
    307            )
    308        == (a_tuple, a_dictionary)
    309        )
    310        # self.assertNotEqual(
    311        self.assertEqual(
    312            unknown_number_of_arguments(
    313                *a_tuple, **a_dictionary
    314            ),
    315            (a_tuple, a_dictionary)
    316        )
    
    318        a_tuple = (1, 2, 3, 'n')
    319
    320        reality = unknown_number_of_arguments(*a_tuple)
    321        my_expectation = (a_tuple, {})
    322        assert (
    323            unknown_number_of_arguments(*a_tuple)
    324        == (a_tuple, {})
    325        )
    326        # self.assertNotEqual(
    327        self.assertEqual(
    328            unknown_number_of_arguments(*a_tuple),
    329            (a_tuple, {})
    330        )
    
    332        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
    333
    334        reality = unknown_number_of_arguments(
    335            **a_dictionary
    336        )
    337        my_expectation = ((), a_dictionary)
    338        assert (
    339            unknown_number_of_arguments(**a_dictionary)
    340        == ((), a_dictionary)
    341        )
    342        # self.assertNotEqual(
    343        self.assertEqual(
    344            unknown_number_of_arguments(**a_dictionary),
    345            ((), a_dictionary)
    346        )
    
    348        reality = unknown_number_of_arguments()
    349        my_expectation = ((), {})
    350        assert (
    351            unknown_number_of_arguments()
    352        == ((), {})
    353        )
    354        # self.assertNotEqual(
    355        self.assertEqual(
    356            unknown_number_of_arguments(),
    357            ((), {})
    358        )
    359
    360
    361# Exceptions seen
    
  • I use the variables to remove repetition of the calls to src.functions.unknown_number_of_arguments and my expectations, from test_unknown_number_of_arguments

    249    # def test_unknown_number_of_arguments():
    250    def test_unknown_number_of_arguments(self):
    251        unknown_number_of_arguments = (
    252            src.functions.unknown_number_of_arguments
    253        )
    254
    255        a_tuple = (0, 1)
    256        a_dictionary = {'a': 2, 'b': 3}
    257
    258        reality = unknown_number_of_arguments(
    259            *a_tuple, **a_dictionary
    260        )
    261        my_expectation = (a_tuple, a_dictionary)
    262        # assert (
    263        #     unknown_number_of_arguments(
    264        #         *a_tuple, **a_dictionary
    265        #     )
    266        # == (a_tuple, a_dictionary)
    267        # )
    268        # self.assertNotEqual(
    269        # self.assertEqual(
    270        #     unknown_number_of_arguments(
    271        #         *a_tuple, **a_dictionary
    272        #     ),
    273        #     (a_tuple, a_dictionary)
    274        # )
    275        assert reality == my_expectation
    276        self.assertEqual(reality, my_expectation)
    
    278        a_tuple = (0, 1)
    279        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
    280
    281        reality = unknown_number_of_arguments(
    282            *a_tuple, **a_dictionary
    283        )
    284        my_expectation = (a_tuple, a_dictionary)
    285        # assert (
    286        #     unknown_number_of_arguments(
    287        #         *a_tuple, **a_dictionary
    288        #     )
    289        # == (a_tuple, a_dictionary)
    290        # )
    291        # self.assertNotEqual(
    292        # self.assertEqual(
    293        #     unknown_number_of_arguments(
    294        #         *a_tuple, **a_dictionary
    295        #     ),
    296        #     (a_tuple, a_dictionary)
    297        # )
    298        assert reality == my_expectation
    299        self.assertEqual(reality, my_expectation)
    
    301        a_tuple = (0, 1, 2)
    302        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
    303
    304        reality = unknown_number_of_arguments(
    305            *a_tuple, **a_dictionary
    306        )
    307        my_expectation = (a_tuple, a_dictionary)
    308        # assert (
    309        #     unknown_number_of_arguments(
    310        #         *a_tuple, **a_dictionary
    311        #     )
    312        # == (a_tuple, a_dictionary)
    313        # )
    314        # self.assertNotEqual(
    315        # self.assertEqual(
    316        #     unknown_number_of_arguments(
    317        #         *a_tuple, **a_dictionary
    318        #     ),
    319        #     (a_tuple, a_dictionary)
    320        # )
    321        assert reality == my_expectation
    322        self.assertEqual(reality, my_expectation)
    
    324        a_tuple = (1, 2, 3, 'n')
    325
    326        reality = unknown_number_of_arguments(*a_tuple)
    327        my_expectation = (a_tuple, {})
    328        # assert (
    329        #     unknown_number_of_arguments(*a_tuple)
    330        # == (a_tuple, {})
    331        # )
    332        # self.assertNotEqual(
    333        # self.assertEqual(
    334        #     unknown_number_of_arguments(*a_tuple),
    335        #     (a_tuple, {})
    336        # )
    337        assert reality == my_expectation
    338        self.assertEqual(reality, my_expectation)
    
    340        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
    341
    342        reality = unknown_number_of_arguments(
    343            **a_dictionary
    344        )
    345        my_expectation = ((), a_dictionary)
    346        # assert (
    347        #     unknown_number_of_arguments(**a_dictionary)
    348        # == ((), a_dictionary)
    349        # )
    350        # self.assertNotEqual(
    351        # self.assertEqual(
    352        #     unknown_number_of_arguments(**a_dictionary),
    353        #     ((), a_dictionary)
    354        # )
    355        assert reality == my_expectation
    356        self.assertEqual(reality, my_expectation)
    
    358        reality = unknown_number_of_arguments()
    359        my_expectation = ((), {})
    360        # assert (
    361        #     unknown_number_of_arguments()
    362        # == ((), {})
    363        # )
    364        # self.assertNotEqual(
    365        # self.assertEqual(
    366        #     unknown_number_of_arguments(),
    367        #     ((), {})
    368        # )
    369        assert reality == my_expectation
    370        self.assertEqual(reality, my_expectation)
    371
    372
    373# Exceptions seen
    

    the test is still green.

  • I remove the commented lines from test_unknown_number_of_arguments

    247        self.assertEqual(reality, my_expectation)
    248
    249    def test_unknown_number_of_arguments(self):
    250        unknown_number_of_arguments = (
    251            src.functions.unknown_number_of_arguments
    252        )
    253
    254        a_tuple = (0, 1)
    255        a_dictionary = {'a': 2, 'b': 3}
    256
    257        reality = unknown_number_of_arguments(
    258            *a_tuple, **a_dictionary
    259        )
    260        my_expectation = (a_tuple, a_dictionary)
    261        assert reality == my_expectation
    262        self.assertEqual(reality, my_expectation)
    
    264        a_tuple = (0, 1)
    265        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
    266
    267        reality = unknown_number_of_arguments(
    268            *a_tuple, **a_dictionary
    269        )
    270        my_expectation = (a_tuple, a_dictionary)
    271        assert reality == my_expectation
    272        self.assertEqual(reality, my_expectation)
    
    274        a_tuple = (0, 1, 2)
    275        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
    276
    277        reality = unknown_number_of_arguments(
    278            *a_tuple, **a_dictionary
    279        )
    280        my_expectation = (a_tuple, a_dictionary)
    281        assert reality == my_expectation
    282        self.assertEqual(reality, my_expectation)
    
    284        a_tuple = (1, 2, 3, 'n')
    285
    286        reality = unknown_number_of_arguments(*a_tuple)
    287        my_expectation = (a_tuple, {})
    288        assert reality == my_expectation
    289        self.assertEqual(reality, my_expectation)
    
    291        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
    292
    293        reality = unknown_number_of_arguments(
    294            **a_dictionary
    295        )
    296        my_expectation = ((), a_dictionary)
    297        assert reality == my_expectation
    298        self.assertEqual(reality, my_expectation)
    
    300        reality = unknown_number_of_arguments()
    301        my_expectation = ((), {})
    302        assert reality == my_expectation
    303        self.assertEqual(reality, my_expectation)
    304
    305
    306# Exceptions seen
    307# AssertionError
    308# NameError
    309# TypeError
    310# SyntaxError
    311# ModuleNotFoundError
    312# AttributeError
    
  • I add a git commit message in the other terminal

    git commit -am \
    'move test_unknown_number_of_arguments to TestFunctions'
    

close the project

  • I close test_functions.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 functions

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I can use the unittest library to write tests with the methods of the unittest.TestCase class or I can write them with bare assert statements.


code from the chapter

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


what is next?

So far, I know

Would you like to test the person project with the unittest library?


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.