-
Notifications
You must be signed in to change notification settings - Fork 87
/
test_core_test_runner.py
251 lines (215 loc) · 8.63 KB
/
test_core_test_runner.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
Unittests fror opal.core.test_runner
"""
import subprocess
import ffs
from mock import MagicMock, patch, call
from opal.core.test import OpalTestCase
from opal.core import test_runner
class RunPyTestsTestCase(OpalTestCase):
@patch('subprocess.check_call')
def test_run_tests(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = None
mock_args.failfast = False
test_runner._run_py_tests(mock_args)
check_call.assert_called_once_with(['python', 'runtests.py'])
@patch('subprocess.check_call')
@patch.object(test_runner.sys, 'exit')
def test_run_tests_errors(self, exiter, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = None
mock_args.failfast = False
check_call.side_effect = subprocess.CalledProcessError(None, None)
test_runner._run_py_tests(mock_args)
exiter.assert_called_once_with(1)
@patch('subprocess.check_call')
def test_run_tests_with_test_arg(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = 'opal.tests.foo'
mock_args.failfast = False
test_runner._run_py_tests(mock_args)
check_call.assert_called_once_with(['python', 'runtests.py', 'opal.tests.foo'])
@patch('subprocess.check_call')
def test_run_tests_with_coverage(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = True
mock_args.test = None
mock_args.failfast = False
test_runner._run_py_tests(mock_args)
calls = [
call(['coverage', 'run', 'runtests.py']),
call(['coverage', 'html'])
]
check_call.assert_has_calls(calls)
@patch('subprocess.check_call')
def test_run_tests_with_coverage_and_test_arg(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = True
mock_args.test = 'opal.tests.foo'
mock_args.failfast = False
test_runner._run_py_tests(mock_args)
calls = [
call(['coverage', 'run', 'runtests.py', 'opal.tests.foo']),
call(['coverage', 'html'])
]
@patch('subprocess.check_call')
@patch.object(test_runner.sys, 'exit')
def test_run_tests_with_coverage_errors(self, exiter, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = True
mock_args.test = None
mock_args.failfast = False
check_call.side_effect = [None, subprocess.CalledProcessError(None, None)]
test_runner._run_py_tests(mock_args)
self.assertEqual(2, check_call.call_count)
exiter.assert_called_once_with(1)
@patch('subprocess.check_call')
@patch.object(test_runner, '_has_file')
def test_run_tests_for_app_with_coverage(self, has_file, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = True
mock_args.failfast = False
mock_args.test = None
has_file.side_effect = lambda a, b: b == 'manage.py'
test_runner._run_py_tests(mock_args)
calls = [
call(['coverage', 'run', 'manage.py', 'test']),
call(['coverage', 'html'])
]
check_call.assert_has_calls(calls)
@patch('subprocess.check_call')
@patch.object(test_runner, '_has_file')
def test_run_tests_for_app_with_test(self, has_file, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.failfast = False
mock_args.test = 'foo.tests.bar'
has_file.side_effect = lambda a, b: b == 'manage.py'
test_runner._run_py_tests(mock_args)
check_call.assert_called_with(['python', 'manage.py', 'test', 'foo.tests.bar'])
@patch('subprocess.check_call')
def test_run_tests_failfast(self, check_call):
mock_args = MagicMock(name='args')
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = False
mock_args.failfast = True
test_runner._run_py_tests(mock_args)
check_call.assert_called_with(['python', 'runtests.py', '--failfast'])
@patch.object(test_runner, '_has_file')
@patch.object(test_runner, 'write')
@patch.object(test_runner.sys, 'exit')
def test_run_tests_for_unknown_config(self, sysexit, writer, has_file):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = None
mock_args.failfast = False
has_file.return_value = False
test_runner._run_py_tests(mock_args)
writer.assert_any_call("\n\nCripes!\n")
sysexit.assert_called_with(1)
class RunJSTestsTestCase(OpalTestCase):
def setUp(self):
self.TRAVIS = test_runner.TRAVIS
def tearDown(self):
test_runner.TRAVIS = self.TRAVIS
@patch('subprocess.check_call')
def test_run_tests(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = None
mock_args.failfast = False
test_runner.TRAVIS = False
test_runner._run_js_tests(mock_args)
self.assertEqual(
['karma', 'start', 'config/karma.conf.js', '--single-run'],
check_call.call_args[0][0]
)
@patch('subprocess.check_call')
def test_run_tests_travis(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = None
mock_args.failfast = False
test_runner.TRAVIS = True
test_runner._run_js_tests(mock_args)
self.assertEqual(
[
'./node_modules/karma/bin/karma',
'start',
'config/karma.conf.js',
'--single-run'
],
check_call.call_args[0][0]
)
self.assertIn("OPAL_LOCATION", check_call.call_args[1]["env"])
self.assertTrue(
isinstance(check_call.call_args[1]["env"]["OPAL_LOCATION"], str)
)
@patch.object(test_runner.subprocess, 'check_call')
def test_run_tests_failfast(self, check_call):
mock_args = MagicMock(name="args")
mock_args.userland_here = ffs.Path('.')
mock_args.coverage = False
mock_args.test = None
mock_args.failfast = True
test_runner.TRAVIS = False
test_runner._run_js_tests(mock_args)
self.assertEqual(
[
'karma',
'start',
'config/karma.conf.js',
'--single-run',
'--failfast'
],
check_call.call_args[0][0]
)
@patch.object(test_runner.subprocess, 'check_call')
@patch.object(test_runner.sys, 'exit')
def test_generic_error_in_call(self, exiter, check_call):
check_call.side_effect = subprocess.CalledProcessError(None, None)
mock_args = MagicMock(name="args")
test_runner._run_js_tests(mock_args)
exiter.assert_called_with(1)
@patch('subprocess.check_call')
@patch.object(test_runner.sys, 'exit')
@patch.object(test_runner, 'write')
def test_oserror_in_call(self, writer, exiter, check_call):
check_call.side_effect = OSError(2, 'No such file or directory')
mock_args = MagicMock(name="args")
test_runner._run_js_tests(mock_args)
exiter.assert_called_with(1)
writer.assert_any_call("We can't find the karma executable\n")
class RunTestsTestCase(OpalTestCase):
@patch.object(test_runner, '_run_js_tests')
@patch.object(test_runner, '_run_py_tests')
def test_run_tests(self, py, js):
mock_args = MagicMock(name='Args')
mock_args.what = 'all'
test_runner.run_tests(mock_args)
py.assert_called_with(mock_args)
js.assert_called_with(mock_args)
@patch.object(test_runner, '_run_js_tests')
@patch.object(test_runner, '_run_py_tests')
def test_run_py_tests(self, py, js):
mock_args = MagicMock(name='Args')
mock_args.what = ['py']
test_runner.run_tests(mock_args)
py.assert_called_with(mock_args)
self.assertEqual(0, js.call_count)