Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
S2-group
GitHub Repository: S2-group/android-runner
Path: blob/master/tests/unit/test_scripts.py
908 views
1
import collections
2
import os.path as op
3
4
import pytest
5
from mock import Mock, call, patch
6
import time
7
import paths
8
import subprocess
9
from AndroidRunner.Python3 import Python3
10
from AndroidRunner.Script import Script, ScriptError
11
from AndroidRunner.Scripts import Scripts
12
from AndroidRunner.util import ConfigError, FileNotFoundError
13
14
15
class TestScripts(object):
16
@pytest.fixture()
17
def paths_dict(self, tmpdir):
18
paths.CONFIG_DIR = str(tmpdir)
19
20
@pytest.fixture()
21
def scripts(self, paths_dict):
22
with patch('AndroidRunner.Python3.Python3.__init__', return_value=None):
23
test_path = 'test/path/to/script.py'
24
test_config = collections.OrderedDict()
25
test_config['testscript'] = test_path
26
return Scripts(test_config)
27
28
@patch('AndroidRunner.Python3.Python3.__init__')
29
def test_experiment_script_init(self, mock, paths_dict):
30
mock.return_value = None
31
test_path = 'test/path/to/script.py'
32
test_config = collections.OrderedDict()
33
test_config['testscript'] = test_path
34
35
scripts = Scripts(test_config)
36
mock.assert_called_once_with(op.join(paths.CONFIG_DIR, test_path))
37
for script in scripts.scripts['testscript']:
38
assert type(script) == Python3
39
40
@patch('AndroidRunner.Python3.Python3.__init__')
41
def test_python3_interaction_script_init(self, mock, paths_dict):
42
mock.return_value = None
43
test_path = 'test/path/to/script.py'
44
test_config = collections.OrderedDict()
45
test_config['type'] = 'python3'
46
test_config['path'] = test_path
47
test_config_list = list()
48
test_config_list.append(test_config)
49
interaction_test_config = collections.OrderedDict()
50
interaction_test_config['interaction'] = test_config_list
51
scripts = Scripts(interaction_test_config)
52
53
mock.assert_called_once_with(op.join(paths.CONFIG_DIR, test_path), 0, None)
54
for script in scripts.scripts['interaction']:
55
assert type(script) == Python3
56
57
def test_unknown_interaction_script_init(self, paths_dict):
58
test_path = 'test/path/to/script.py'
59
test_config = collections.OrderedDict()
60
test_config['type'] = 'unknownScript'
61
test_config['path'] = test_path
62
test_config_list = list()
63
test_config_list.append(test_config)
64
interaction_test_config = collections.OrderedDict()
65
interaction_test_config['interaction'] = test_config_list
66
with pytest.raises(ConfigError) as _:
67
Scripts(interaction_test_config)
68
69
@patch('AndroidRunner.Script.Script.run')
70
def test_run(self, mock, scripts):
71
fake_device = Mock()
72
scripts.run('testscript', fake_device)
73
mock.assert_called_once_with(fake_device)
74
75
@patch('AndroidRunner.Script.Script.run')
76
def test_run_empty_script_set(self, mock, scripts):
77
fake_device = Mock()
78
scripts.run('testscript1', fake_device)
79
assert mock.call_count == 0
80
81
82
class TestPython3(object):
83
@pytest.fixture()
84
def script_path(self, tmpdir):
85
temp_file = tmpdir.join("script.py")
86
temp_file.write('\n'.join(['from time import sleep',
87
'def main(device_id):\n',
88
' sleep(0.5)\n'
89
' return "succes"']))
90
return str(temp_file)
91
92
@pytest.fixture()
93
def init_error_script_path(self, tmpdir):
94
temp_file = tmpdir.join("script.py")
95
temp_file.write('\n'.join(['from time import sleep\n',
96
'import nonexisting\n',
97
'def main(device_id):\n',
98
' raise NotImplementedError\n']))
99
return str(temp_file)
100
101
def test_python3_error_init(self, init_error_script_path):
102
with pytest.raises(ImportError):
103
Python3(init_error_script_path)
104
105
def test_python3_execute_script(self, script_path):
106
fake_device = Mock()
107
assert Python3(script_path).execute_script(fake_device) == 'succes'
108
109
class TestScript(object):
110
@pytest.fixture()
111
def script_path(self, tmpdir):
112
temp_file = tmpdir.join("script.py")
113
temp_file.write('\n'.join(['from time import sleep',
114
'def main(device_id):\n',
115
' sleep(0.5)\n'
116
' return "succes"']))
117
return str(temp_file)
118
119
@pytest.fixture()
120
def error_script_path(self, tmpdir):
121
temp_file = tmpdir.join("script.py")
122
temp_file.write('\n'.join(['from time import sleep',
123
'def main(device_id):\n',
124
' raise NotImplementedError\n']))
125
return str(temp_file)
126
127
@pytest.fixture()
128
def script(self, script_path):
129
return Script(script_path)
130
131
def test_logcat_regex(self, script):
132
fake_device = Mock()
133
test_queue = Mock()
134
manager = Mock()
135
136
manager.fake_device_mock = fake_device
137
manager.test_queu_mock = test_queue
138
139
script.mp_logcat_regex(test_queue, fake_device, 'test_regex')
140
141
expected_calls = [call.fake_device_mock.logcat_regex('test_regex'), call.test_queu_mock.put('logcat')]
142
143
assert manager.mock_calls == expected_calls
144
145
def test_script_not_found_init(self):
146
with pytest.raises(FileNotFoundError):
147
Script('fake/file/path')
148
149
def test_script_run_normal(self, script_path):
150
fake_device = Mock()
151
assert Python3(script_path).run(fake_device) == 'script'
152
153
def test_script_run_timeout(self, script_path):
154
fake_device = Mock()
155
assert Python3(script_path, timeout=10).run(fake_device) == 'timeout'
156
157
def test_script_run_logcat(self, script_path):
158
fake_device = Mock()
159
assert Python3(script_path, logcat_regex='').run(fake_device) == 'logcat'
160
161
def test_script_error(self, error_script_path):
162
fake_device = Mock()
163
with pytest.raises(ScriptError) as expect_ex:
164
Python3(error_script_path).run(fake_device)
165
assert 'NotImplementedError' in str(expect_ex.value)
166
167
def test_script_mp_run(self, script_path):
168
fake_device = Mock()
169
test_queue = Mock()
170
test_script = Python3(script_path)
171
test_script.mp_run(test_queue, fake_device)
172
173
test_queue.put.assert_called_once_with('script')
174
175
def test_script_mp_run_error(self, error_script_path):
176
fake_device = Mock()
177
test_queue = Mock()
178
test_script = Python3(error_script_path)
179
test_script.mp_run(test_queue, fake_device)
180
assert test_queue.put.call_count == 2
181
assert 'NotImplementedError' in str(test_queue.put.call_args_list)
182
assert 'script' in str(test_queue.put.call_args_list[1][0])
183
184
@patch("time.sleep")
185
def test_mp_logcat_regex_one_iteration(self, sleep, script_path):
186
fake_device = Mock()
187
fake_device.logcat_regex.side_effect = [False, True]
188
test_queue = Mock()
189
test_script = Python3(script_path)
190
test_script.mp_logcat_regex(test_queue, fake_device, "Test")
191
192
sleep.assert_called_once_with(1)
193
test_queue.put.assert_called_once_with("logcat")
194
195