test functions with unittest

I want to use the unittest library in the functions project. I also want to use object attributes to remove repetition of some values from the tests.


preview

I have these tests by the end of the chapter

functions/tests/test_functions.py
 1import src.functions
 2import unittest
 3
 4
 5def assert_is_none(something):
 6    assert something is None
 7
 8
 9class TestFunctions(unittest.TestCase):
10
11    first = 'first'
12    last = 'last'
13    a_tuple = (0, 1, 2, 'n')
14    a_list = [0, 1, 2, 'n']
15    a_set = {0, 1, 2, 'n'}
16    a_dictionary = {'key': 'value'}
functions/tests/test_functions.py
18    def test_making_a_function_w_pass(self):
19        assert_is_none(src.functions.w_pass())
20        self.assertIs(src.functions.w_pass(), None)
21
22    def test_making_a_function_w_return(self):
23        assert_is_none(src.functions.w_return())
24        self.assertIs(
25            src.functions.w_return(), None
26        )
functions/tests/test_functions.py
28    def test_making_a_function_w_return_none(self):
29        assert_is_none(src.functions.w_return_none())
30        self.assertIs(
31            src.functions.w_return_none(), None
32        )
33
34    def test_what_happens_after_functions_return(self):
35        assert_is_none(
36            src.functions.return_leaves_the_function()
37        )
38        self.assertIs(
39            src.functions.return_leaves_the_function(),
40            None
41        )
functions/tests/test_functions.py
43    def test_constant_function(self):
44        self.assertEqual(
45            src.functions.constant(), 'the same thing'
46        )
47
48    def test_identity_function(self):
49        assert_is_none(src.functions.identity(None))
50        self.assertIs(
51            src.functions.identity(None), None
52        )
53
54        self.assertEqual(
55            src.functions.identity(object), object
56        )
functions/tests/test_functions.py
58    def test_why_use_a_function(self):
59        def add_x(number):
60            return 3 + number
61
62        self.assertEqual(add_x(0), 3)
63        self.assertEqual(add_x(1), 4)
64        self.assertEqual(add_x(2), 5)
65        self.assertEqual(add_x(3), 6)
66        self.assertEqual(add_x(4), 7)
67        self.assertEqual(add_x(5), 8)
68        self.assertEqual(add_x(6), 9)
69        self.assertEqual(add_x(7), 10)
70        self.assertEqual(add_x(8), 11)
71        self.assertEqual(add_x(9), 12)
functions/tests/test_functions.py
 73    def test_positional_arguments(self):
 74        positional_arguments = (
 75            src.functions.positional_arguments
 76        )
 77
 78        self.assertEqual(
 79            positional_arguments(self.first, self.last),
 80            (self.first, self.last)
 81        )
 82        self.assertEqual(
 83            positional_arguments(self.last, self.first),
 84            (self.last, self.first)
 85        )
 86
 87        self.assertEqual(
 88            positional_arguments(0, 1), (0, 1)
 89        )
 90
 91        self.assertEqual(
 92            positional_arguments(
 93                self.a_tuple, self.a_list
 94            ),
 95            (self.a_tuple, self.a_list)
 96        )
 97
 98        self.assertEqual(
 99            src.functions.keyword_arguments(
100                self.a_set, self.a_dictionary
101            ),
102            (self.a_set, self.a_dictionary)
103        )
functions/tests/test_functions.py
105    def test_keyword_arguments(self):
106        keyword_arguments = (
107            src.functions.keyword_arguments
108        )
109
110        self.assertEqual(
111            keyword_arguments(
112                first_input=self.first,
113                last_input=self.last,
114            ),
115            (self.first, self.last)
116        )
117        self.assertEqual(
118            keyword_arguments(
119                last_input=self.last,
120                first_input=self.first,
121            ),
122            (self.first, self.last)
123        )
124
125        self.assertEqual(
126            keyword_arguments(
127                last_input=0, first_input=1,
128            ),
129            (1, 0)
130        )
131
132        self.assertEqual(
133            keyword_arguments(
134                first_input=self.a_tuple,
135                last_input=self.a_list,
136            ),
137            (self.a_tuple, self.a_list)
138        )
139
140        self.assertEqual(
141            src.functions.positional_arguments(
142                last_input=self.a_dictionary,
143                first_input=self.a_set,
144            ),
145            (self.a_set, self.a_dictionary)
146        )
functions/tests/test_functions.py
148    def test_args_and_kwargs(self):
149        self.assertEqual(
150            src.functions.args_and_kwargs(
151                self.first, last_input=self.last
152            ),
153            (self.first, self.last)
154        )
functions/tests/test_functions.py
156    def test_optional_arguments(self):
157        optional_arguments = (
158            src.functions.optional_arguments
159        )
160
161        first_name, last_name = 'jane', 'doe'
162        self.assertEqual(
163            optional_arguments(
164                first_name,
165            ),
166            (first_name, last_name)
167        )
168
169        first_name, blow = 'joe', 'blow'
170        self.assertEqual(
171            optional_arguments(
172                first_name, blow
173            ),
174            (first_name, blow)
175        )
176
177        first_name = 'john'
178        self.assertEqual(
179            optional_arguments(
180                first_input=first_name,
181            ),
182            (first_name, last_name)
183        )
184
185        last_name = 'smith'
186        self.assertEqual(
187            optional_arguments(
188                last_input=last_name,
189                first_input=first_name,
190            ),
191            (first_name, last_name)
192        )
functions/tests/test_functions.py
194    def test_unknown_number_of_arguments(self):
195        unknown_number_of_arguments = (
196            src.functions.unknown_number_of_arguments
197        )
198
199        a_tuple = (0, 1)
200        a_dictionary = {'a': 2, 'b': 3}
201        self.assertEqual(
202            unknown_number_of_arguments(
203                *a_tuple, **a_dictionary
204            ),
205            (a_tuple, a_dictionary)
206        )
207
208        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
209        self.assertEqual(
210            unknown_number_of_arguments(
211                *a_tuple, **a_dictionary
212            ),
213            (a_tuple, a_dictionary)
214        )
215
216        a_tuple = (0, 1, 2)
217        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
218        self.assertEqual(
219            unknown_number_of_arguments(
220                *a_tuple, **a_dictionary
221            ),
222            (a_tuple, a_dictionary)
223        )
224
225        a_tuple = (0, 1, 2, 'n')
226        self.assertEqual(
227            unknown_number_of_arguments(*a_tuple),
228            (a_tuple, {})
229        )
230
231        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
232        self.assertEqual(
233            unknown_number_of_arguments(**a_dictionary),
234            ((), a_dictionary)
235        )
236
237        self.assertEqual(
238            unknown_number_of_arguments(), ((), {})
239        )
240
241
242# Exceptions seen
243# AssertionError
244# NameError
245# TypeError
246# SyntaxError
247# AttributeError

open the project

  • I open a terminal

  • I change directory to the project

    terminal
    cd functions
    

    the terminal shows I am in the functions folder

    .../pumping_python/functions
    
  • I open test_functions.py from the tests folder

  • I use pytest-watcher to run the tests automatically

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

    the terminal shows

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

add TestFunctions object

RED: make it fail


  • I add an object named Functions to test_functions.py

    the test is still green.

  • I change the name of the object to TestFunctions

    the terminal is my friend, and shows AttributeError

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

GREEN: make it pass



REFACTOR: make it better


  • I remove the commented lines

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

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

    terminal
    git commit -am \
    'add TestFunctions object'
    

test_making_a_function_w_pass with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_making_a_function_w_pass

13class TestFunctions(unittest.TestCase):
14
15    # def test_making_a_function_w_pass():
16    def test_making_a_function_w_pass(self):
17        assert_is_none(src.functions.w_pass())

the test is green again.


REFACTOR: make it better



test_making_a_function_w_return with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_making_a_function_w_return

17        self.assertIs(src.functions.w_pass(), None)
18
19    # def test_making_a_function_w_return():
20    def test_making_a_function_w_return(self):
21        assert_is_none(src.functions.w_return())

green again.


REFACTOR: make it better



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

21        self.assertIs(
22            src.functions.w_return(), None
23        )
24
25    # def test_making_a_function_w_return_none():
26    def test_making_a_function_w_return_none(self):
27        assert_is_none(src.functions.w_return_none())

green.


REFACTOR: make it better



test_what_happens_after_functions_return with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_what_happens_after_functions_return

27        self.assertIs(
28            src.functions.w_return_none(), None
29        )
30
31    # def test_what_happens_after_functions_return():
32    def test_what_happens_after_functions_return(self):
33        assert_is_none(
34            src.functions.return_leaves_the_function()
35        )
36
37
38def test_constant_function():

green again.


REFACTOR: make it better



test_constant_function with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_constant_function

35        self.assertIs(
36            src.functions.return_leaves_the_function(),
37            None
38        )
39
40    # def test_constant_function():
41    def test_constant_function(self):
42        assert_equal(
43            src.functions.constant(), 'the same thing'
44        )

green again.


REFACTOR: make it better



test_identity_function with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_identity_function

40    def test_constant_function(self):
41        self.assertEqual(
42            src.functions.constant(), 'the same thing'
43        )
44
45    # def test_identity_function():
46    def test_identity_function(self):
47        assert_is_none(src.functions.identity(None))

the test is green again.


REFACTOR: make it better



test_why_use_a_function with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_why_use_a_function

51        self.assertEqual(
52            src.functions.identity(object), object
53        )
54
55    # def test_why_use_a_function():
56    def test_why_use_a_function(self):
57        def add_x(number):
58            return 3 + number

green again.


REFACTOR: make it better



test_positional_arguments with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_positional_arguments

68        self.assertEqual(add_x(9), 12)
69
70    # def test_positional_arguments():
71    def test_positional_arguments(self):
72        positional_arguments = (
73            src.functions.positional_arguments
74        )

green.


REFACTOR: make it better



test_keyword_arguments with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_keyword_arguments

 96        a_set = {0, 1, 2, 'n'}
 97        a_dictionary = {'key': 'value'}
 98        self.assertEqual(
 99            src.functions.keyword_arguments(
100                a_set, a_dictionary
101            ),
102            (a_set, a_dictionary)
103        )
104
105    # def test_keyword_arguments():
106    def test_keyword_arguments(self):
107        keyword_arguments = (
108            src.functions.keyword_arguments
109        )
110        first, last = 'first', 'last'v

green again.


REFACTOR: make it better



test_args_and_kwargs with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_args_and_kwargs

141        a_set = {0, 1, 2, 'n'}
142        a_dictionary = {'key': 'value'}
143        self.assertEqual(
144            src.functions.positional_arguments(
145                last_input=a_dictionary,
146                first_input=a_set,
147            ),
148            (a_set, a_dictionary)
149        )
150
151    # def test_args_and_kwargs():
152    def test_args_and_kwargs(self):
153        first, last = 'first', 'last'

the test is green again.


REFACTOR: make it better



test_optional_arguments with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_optional_arguments

154        self.assertEqual(
155            src.functions.args_and_kwargs(
156                first, last_input=last
157            ),
158            (first, last)
159        )
160
161    # def test_optional_arguments():
162    def test_optional_arguments(self):
163        optional_arguments = (
164            src.functions.optional_arguments
165        )

green again.


REFACTOR: make it better



test_unknown_number_of_arguments with unittest

RED: make it fail



GREEN: make it pass


I add self to the parentheses of test_unknown_number_of_arguments

190        last_name = 'smith'
191        self.assertEqual(
192            optional_arguments(
193                last_input=last_name,
194                first_input=first_name,
195            ),
196            (first_name, last_name)
197        )
198
199    # def test_unknown_number_of_arguments():
200    def test_unknown_number_of_arguments(self):
201        unknown_number_of_arguments = (
202            src.functions.unknown_number_of_arguments
203        )

green.


REFACTOR: make it better



extract first, last object attributes

I want to use object attributes to remove repetition from the tests.


extract a_tuple object attribute


extract a_list object attribute


extract a_set object attribute


extract a_dictionary object attribute

I can use object attributes to remove repetition


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

    terminal
    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review


code from the chapter

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


what is next?

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.