Skip to content

Commit a2bcae0

Browse files
committed
Rework test_api testing and fix a bug
Bug was found by the new tests
1 parent 817eb5a commit a2bcae0

File tree

2 files changed

+58
-106
lines changed

2 files changed

+58
-106
lines changed

tools/test/test_api/test_api_test.py

Lines changed: 52 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,127 +15,77 @@
1515
limitations under the License.
1616
"""
1717

18-
import unittest
18+
import pytest
1919
from mock import patch
20+
from tools.targets import set_targets_json_location
2021
from tools.test_api import find_tests, build_tests
2122

2223
"""
2324
Tests for test_api.py
2425
"""
2526

26-
class TestApiTests(unittest.TestCase):
27+
def setUp(self):
2728
"""
28-
Test cases for Test Api
29+
Called before each test case
30+
31+
:return:
32+
"""
33+
self.base_dir = 'base_dir'
34+
self.target = "K64F"
35+
self.toolchain_name = "ARM"
36+
37+
@pytest.mark.parametrize("base_dir", ["base_dir"])
38+
@pytest.mark.parametrize("target", ["K64F"])
39+
@pytest.mark.parametrize("toolchain_name", ["ARM"])
40+
@pytest.mark.parametrize("app_config", ["app_config", None])
41+
def test_find_tests_app_config(base_dir, target, toolchain_name, app_config):
2942
"""
43+
Test find_tests for correct use of app_config
3044
31-
def setUp(self):
32-
"""
33-
Called before each test case
34-
35-
:return:
36-
"""
37-
self.base_dir = 'base_dir'
38-
self.target = "K64F"
39-
self.toolchain_name = "ARM"
40-
41-
def tearDown(self):
42-
"""
43-
Called after each test case
44-
45-
:return:
46-
"""
47-
pass
48-
49-
@patch('tools.test_api.scan_resources')
50-
@patch('tools.test_api.prepare_toolchain')
51-
def test_find_tests_app_config(self, mock_prepare_toolchain, mock_scan_resources):
52-
"""
53-
Test find_tests for correct use of app_config
54-
55-
:param mock_prepare_toolchain: mock of function prepare_toolchain
56-
:param mock_scan_resources: mock of function scan_resources
57-
:return:
58-
"""
59-
app_config = "app_config"
45+
:param mock_prepare_toolchain: mock of function prepare_toolchain
46+
:param mock_scan_resources: mock of function scan_resources
47+
:return:
48+
"""
49+
set_targets_json_location()
50+
with patch('tools.test_api.scan_resources') as mock_scan_resources,\
51+
patch('tools.test_api.prepare_toolchain') as mock_prepare_toolchain:
6052
mock_scan_resources().inc_dirs.return_value = []
6153

62-
find_tests(self.base_dir, self.target, self.toolchain_name, app_config=app_config)
54+
find_tests(base_dir, target, toolchain_name, app_config=app_config)
6355

6456
args = mock_prepare_toolchain.call_args
65-
self.assertTrue('app_config' in args[1],
66-
"prepare_toolchain was not called with app_config")
67-
self.assertEqual(args[1]['app_config'], app_config,
68-
"prepare_toolchain was called with an incorrect app_config")
69-
70-
@patch('tools.test_api.scan_resources')
71-
@patch('tools.test_api.prepare_toolchain')
72-
def test_find_tests_no_app_config(self, mock_prepare_toolchain, mock_scan_resources):
73-
"""
74-
Test find_tests correctly deals with no app_config
75-
76-
:param mock_prepare_toolchain: mock of function prepare_toolchain
77-
:param mock_scan_resources: mock of function scan_resources
78-
:return:
79-
"""
80-
mock_scan_resources().inc_dirs.return_value = []
57+
assert 'app_config' in args[1],\
58+
"prepare_toolchain was not called with app_config"
59+
assert args[1]['app_config'] == app_config,\
60+
"prepare_toolchain was called with an incorrect app_config"
8161

82-
find_tests(self.base_dir, self.target, self.toolchain_name)
8362

84-
args = mock_prepare_toolchain.call_args
85-
self.assertTrue('app_config' in args[1],
86-
"prepare_toolchain was not called with app_config")
87-
self.assertEqual(args[1]['app_config'], None,
88-
"prepare_toolchain was called with an incorrect app_config")
89-
90-
@patch('tools.test_api.scan_resources')
91-
@patch('tools.test_api.build_project')
92-
def test_build_tests_app_config(self, mock_build_project, mock_scan_resources):
93-
"""
94-
Test build_tests for correct use of app_config
95-
96-
:param mock_prepare_toolchain: mock of function prepare_toolchain
97-
:param mock_scan_resources: mock of function scan_resources
98-
:return:
99-
"""
100-
tests = {'test1': 'test1_path','test2': 'test2_path'}
101-
src_paths = ['.']
102-
build_path = "build_path"
103-
app_config = "app_config"
104-
mock_build_project.return_value = "build_project"
105-
106-
build_tests(tests, src_paths, build_path, self.target, self.toolchain_name,
107-
app_config=app_config)
63+
@pytest.mark.parametrize("build_path", ["build_path"])
64+
@pytest.mark.parametrize("target", ["K64F"])
65+
@pytest.mark.parametrize("toolchain_name", ["ARM"])
66+
@pytest.mark.parametrize("app_config", ["app_config", None])
67+
def test_find_tests_app_config(build_path, target, toolchain_name, app_config):
68+
"""
69+
Test find_tests for correct use of app_config
10870
109-
arg_list = mock_build_project.call_args_list
110-
for args in arg_list:
111-
self.assertTrue('app_config' in args[1],
112-
"build_tests was not called with app_config")
113-
self.assertEqual(args[1]['app_config'], app_config,
114-
"build_tests was called with an incorrect app_config")
115-
116-
@patch('tools.test_api.scan_resources')
117-
@patch('tools.test_api.build_project')
118-
def test_build_tests_no_app_config(self, mock_build_project, mock_scan_resources):
119-
"""
120-
Test build_tests correctly deals with no app_config
121-
122-
:param mock_prepare_toolchain: mock of function prepare_toolchain
123-
:param mock_scan_resources: mock of function scan_resources
124-
:return:
125-
"""
126-
tests = {'test1': 'test1_path', 'test2': 'test2_path'}
127-
src_paths = ['.']
128-
build_path = "build_path"
71+
:param mock_prepare_toolchain: mock of function prepare_toolchain
72+
:param mock_scan_resources: mock of function scan_resources
73+
:return:
74+
"""
75+
tests = {'test1': 'test1_path','test2': 'test2_path'}
76+
src_paths = ['.']
77+
set_targets_json_location()
78+
with patch('tools.test_api.scan_resources') as mock_scan_resources,\
79+
patch('tools.test_api.build_project') as mock_build_project:
12980
mock_build_project.return_value = "build_project"
81+
mock_scan_resources().inc_dirs.return_value = []
13082

131-
build_tests(tests, src_paths, build_path, self.target, self.toolchain_name)
83+
build_tests(tests, src_paths, build_path, target, toolchain_name,
84+
app_config=app_config)
13285

13386
arg_list = mock_build_project.call_args_list
13487
for args in arg_list:
135-
self.assertTrue('app_config' in args[1],
136-
"build_tests was not called with app_config")
137-
self.assertEqual(args[1]['app_config'], None,
138-
"build_tests was called with an incorrect app_config")
139-
140-
if __name__ == '__main__':
141-
unittest.main()
88+
assert 'app_config' in args[1],\
89+
"build_tests was not called with app_config"
90+
assert args[1]['app_config'] == app_config,\
91+
"build_tests was called with an incorrect app_config"

tools/test_api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,9 +2198,10 @@ def build_tests(tests, base_source_paths, build_path, target, toolchain_name,
21982198
results.remove(r)
21992199

22002200
# Take report from the kwargs and merge it into existing report
2201-
report_entry = worker_result['kwargs']['report'][target_name][toolchain_name]
2202-
for test_key in report_entry.keys():
2203-
report[target_name][toolchain_name][test_key] = report_entry[test_key]
2201+
if report:
2202+
report_entry = worker_result['kwargs']['report'][target_name][toolchain_name]
2203+
for test_key in report_entry.keys():
2204+
report[target_name][toolchain_name][test_key] = report_entry[test_key]
22042205

22052206
# Set the overall result to a failure if a build failure occurred
22062207
if ('reason' in worker_result and
@@ -2224,7 +2225,8 @@ def build_tests(tests, base_source_paths, build_path, target, toolchain_name,
22242225
}
22252226

22262227
test_key = worker_result['kwargs']['project_id'].upper()
2227-
print report[target_name][toolchain_name][test_key][0][0]['output'].rstrip()
2228+
if report:
2229+
print report[target_name][toolchain_name][test_key][0][0]['output'].rstrip()
22282230
print 'Image: %s\n' % bin_file
22292231

22302232
except:

0 commit comments

Comments
 (0)