forked from arbulu89/python-ut-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
third_module_test.py
137 lines (107 loc) · 4.05 KB
/
third_module_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Unitary tests for utproject/third_module.py
:author: xarbulu
:organization: SUSE Linux GmbH
:contact: [email protected]
:since: 2019-06-02
"""
# pylint:disable=C0103,C0111,W0212,W0611
import sys
import logging
import unittest
import string
try:
from unittest import mock
except ImportError:
import mock
from utproject import third_module
class TestThirdModule(unittest.TestCase):
"""
Unitary tests for utproject/third_module.py
"""
@classmethod
def setUpClass(cls):
"""
Global setUp.
"""
logging.basicConfig(level=logging.INFO)
def setUp(self):
"""
Test setUp.
"""
# Use the setup to create a fresh instance for each test
self._my_test_module = third_module.ThirdModule()
def tearDown(self):
"""
Test tearDown.
"""
@classmethod
def tearDownClass(cls):
"""
Global tearDown.
"""
# Some exceptional cases we can use the original module instead of mocking it (constans for instance)
@mock.patch('random.choice')
def test_random_string(self, mock_choice):
mock_choice.side_effect = ['a', 'b', 'c', 'd', 'e']
my_str = third_module.ThirdModule.random_string(5)
self.assertEqual(my_str, 'abcde')
mock_choice.assert_has_calls([
mock.call(string.ascii_lowercase),
mock.call(string.ascii_lowercase),
mock.call(string.ascii_lowercase),
mock.call(string.ascii_lowercase),
mock.call(string.ascii_lowercase)
])
# Use return_value when the method is executed only once
@mock.patch('logging.Logger.info')
@mock.patch('utproject.second_module.SecondModule')
def test_use_other_classes(self, mock_second_module, mock_logger):
mock_instance = mock.Mock()
mock_second_module.return_value = mock_instance
self._my_test_module.use_other_classes('tom', 25)
mock_second_module.assert_called_once_with('tom', 25)
mock_instance.print_name.assert_called_once_with()
mock_instance.print_year_of_birth.assert_called_once_with()
mock_logger.assert_has_calls([
mock.call('using other module classes'),
mock.call('method execution finished')
])
# Use side_effect when the method is executed several times or to raise an exception
# Check here how a returned value might be a mock too
@mock.patch('random.randint')
@mock.patch('utproject.second_module.SecondModule')
@mock.patch('utproject.third_module.ThirdModule.random_string')
def test_use_other_classes_multiple(self, mock_random_string, mock_second_module, mock_randint):
mock_instance_first = mock.Mock()
mock_instance_second = mock.Mock()
mock_instance_third = mock.Mock()
mock_second_module.side_effect = [
mock_instance_first, mock_instance_second, mock_instance_third]
mock_random_string.side_effect = ['abc', 'def', 'ghi']
mock_randint.side_effect = [5, 10 ,15]
# Method under test
result = self._my_test_module.use_other_classes_multiple(3)
self.assertListEqual(result, [
mock_instance_first, mock_instance_second, mock_instance_third])
mock_second_module.assert_has_calls([
mock.call('abc', 5),
mock.call('def', 10),
mock.call('ghi', 15),
])
mock_randint.assert_has_calls([
mock.call(1, 100),
mock.call(1, 100),
mock.call(1, 100)
])
mock_random_string.assert_has_calls([
mock.call(5),
mock.call(5),
mock.call(5)
])
mock_instance_first.print_name.assert_called_once_with()
mock_instance_first.print_year_of_birth.assert_called_once_with()
mock_instance_second.print_name.assert_called_once_with()
mock_instance_second.print_year_of_birth.assert_called_once_with()
mock_instance_third.print_name.assert_called_once_with()
mock_instance_third.print_year_of_birth.assert_called_once_with()