Skip to content

Commit 7f9f4b5

Browse files
committed
Removed hardcoded test scenario. Now test scenario should be imported from external JSON file.
1 parent 0c79545 commit 7f9f4b5

File tree

1 file changed

+75
-35
lines changed

1 file changed

+75
-35
lines changed

workspace_tools/singletest.py

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
import sys
4343
import json
44+
import optparse
45+
import pprint
4446
from prettytable import PrettyTable
4547
from serial import Serial
4648

@@ -51,6 +53,7 @@
5153

5254
ROOT = abspath(join(dirname(__file__), ".."))
5355
sys.path.insert(0, ROOT)
56+
# Imports related to mbed build pi
5457
from workspace_tools.build_api import build_project, build_mbed_libs
5558
from workspace_tools.paths import BUILD_DIR
5659
from workspace_tools.targets import TARGET_MAP
@@ -60,6 +63,7 @@
6063
ROOT = abspath(join(dirname(__file__), ".."))
6164
sys.path.insert(0, ROOT)
6265

66+
# Imports related to mbed build pi
6367
from workspace_tools.utils import delete_dir_files
6468
from workspace_tools.settings import MUTs
6569

@@ -77,7 +81,9 @@ def reset(self, mcu_name, serial,
7781
"""
7882
if sleep_before_reset > 0:
7983
sleep(sleep_before_reset)
80-
verbose_msg = "Reset::cmd(sendBreak)"
84+
if verbose:
85+
verbose_msg = "Reset::cmd(sendBreak)"
86+
# Reset type decision
8187
if mcu_name.startswith('NRF51822'): # Nordic
8288
call(["nrfjprog", "-r"])
8389
verbose_msg = "Reset::cmd(nrfjprog)"
@@ -86,6 +92,7 @@ def reset(self, mcu_name, serial,
8692
verbose_msg = "Reset::cmd(ST-LINK_CLI.exe)"
8793
else:
8894
serial.sendBreak()
95+
8996
if sleep_before_reset > 0:
9097
sleep(sleep_after_reset)
9198
if verbose:
@@ -97,6 +104,7 @@ def flush_serial(self, serial):
97104
serial.flushOutput()
98105

99106
def is_peripherals_available(self, target, peripherals=None):
107+
""" Checks if specified target should run specific peripheral test case."""
100108
if peripherals is not None:
101109
peripherals = set(peripherals)
102110

@@ -114,7 +122,7 @@ def is_peripherals_available(self, target, peripherals=None):
114122
return False
115123

116124
def run_host_test(self, name, target_name, disk, port,
117-
duration, extra_serial, verbose=True):
125+
duration, extra_serial, verbose=False):
118126
"""
119127
Functions resets target and grabs by timeouted pooling test log
120128
via serial port.
@@ -246,25 +254,54 @@ def shape_test_request(mcu, image_path, test_id, duration=10):
246254

247255

248256
if __name__ == '__main__':
249-
start = time()
250-
single_test = SingleTestRunner()
257+
# Command line options
258+
parser = optparse.OptionParser()
259+
parser.add_option('-i', '--tests',
260+
dest='test_spec_filename',
261+
metavar="FILE",
262+
help='Points to file with test specification')
263+
264+
parser.add_option('-s', '--suppress-summary',
265+
dest='suppress_summary',
266+
default=False,
267+
action="store_true",
268+
help='Suppresses display of wellformatted table with test results')
269+
270+
parser.add_option('-v', '--verbose',
271+
dest='verbose',
272+
default=False,
273+
action="store_true",
274+
help='Verbose mode (pronts some extra information)')
275+
276+
parser.epilog="Example: singletest.py -i test_spec.json"
277+
(opts, args) = parser.parse_args()
251278

252279
# Below list tells script which targets and their toolchain(s)
253280
# should be covered by the test scenario
254-
test_spec = {
255-
"targets": {
256-
# "KL25Z": ["ARM", "GCC_ARM"],
257-
# "LPC1768": ["ARM", "GCC_ARM", "GCC_CR", "GCC_CS", "IAR"],
258-
# "LPC11U24": ["uARM"]
259-
# "UBLOX_C027": ["IAR"]
260-
# "NRF51822": ["ARM"]
261-
# "NUCLEO_F103RB": ["ARM"],
262-
# "LPC2368": ["ARM"],
263-
# "LPC812": ["uARM"],
264-
# "LPC1549": ["uARM"]
265-
"LPC4088": ["ARM"] # , "GCC_CR", "GCC_ARM"
266-
}
267-
}
281+
test_spec = None
282+
283+
# Open file with test specification
284+
if opts.test_spec_filename:
285+
try:
286+
with open(opts.test_spec_filename) as data_file:
287+
try:
288+
test_spec = json.load(data_file)
289+
except ValueError as json_error_msg:
290+
test_spec = None
291+
print "Error: %s" % (json_error_msg)
292+
except IOError as fileopen_error_msg:
293+
print "Error: %s" % (fileopen_error_msg)
294+
if opts.verbose and test_spec:
295+
pp = pprint.PrettyPrinter(indent=4)
296+
pp.pprint(test_spec)
297+
298+
if test_spec is None:
299+
parser.print_help()
300+
exit(-1)
301+
302+
# Magic happens here... ;)
303+
start = time()
304+
single_test = SingleTestRunner()
268305

269306
clean = test_spec.get('clean', False)
270307
test_ids = test_spec.get('test_ids', [])
@@ -287,7 +324,8 @@ def shape_test_request(mcu, image_path, test_id, duration=10):
287324

288325
if test.automated and test.is_supported(target, toolchain):
289326
if not single_test.is_peripherals_available(target, test.peripherals):
290-
print "TargetTest::%s::TestSkipped(%s)" % (target, ",".join(test.peripherals))
327+
if opts.verbose:
328+
print "TargetTest::%s::TestSkipped(%s)" % (target, ",".join(test.peripherals))
291329
continue
292330

293331
test_result = {
@@ -296,7 +334,7 @@ def shape_test_request(mcu, image_path, test_id, duration=10):
296334
'test_id': test_id,
297335
}
298336

299-
path = build_project(test.source_dir, join(build_dir, test_id), T, toolchain, test.dependencies, clean=clean, verbose=False)
337+
path = build_project(test.source_dir, join(build_dir, test_id), T, toolchain, test.dependencies, clean=clean, verbose=opts.verbose)
300338

301339
if target.startswith('NRF51822'): # Nordic:
302340
#Convert bin to Hex and Program nrf chip via jlink
@@ -314,19 +352,21 @@ def shape_test_request(mcu, image_path, test_id, duration=10):
314352

315353
elapsed_time = time() - start
316354

317-
print
318-
print "Test summary:"
319-
# Pretty table package is used to print results
320-
pt = PrettyTable(["Result", "Target", "Toolchain", "Test ID", "Test Description",
321-
"Elapsed Time (sec)", "Timeout (sec)"])
322-
pt.align["Result"] = "l" # Left align
323-
pt.align["Target"] = "l" # Left align
324-
pt.align["Toolchain"] = "l" # Left align
325-
pt.align["Test ID"] = "l" # Left align
326-
pt.align["Test Description"] = "l" # Left align
327-
pt.padding_width = 1 # One space between column edges and contents (default)
328-
329-
for test in test_summary:
330-
pt.add_row(test)
331-
print pt
355+
# Human readable summary
356+
if not opts.suppress_summary:
357+
print
358+
print "Test summary:"
359+
# Pretty table package is used to print results
360+
pt = PrettyTable(["Result", "Target", "Toolchain", "Test ID", "Test Description",
361+
"Elapsed Time (sec)", "Timeout (sec)"])
362+
pt.align["Result"] = "l" # Left align
363+
pt.align["Target"] = "l" # Left align
364+
pt.align["Toolchain"] = "l" # Left align
365+
pt.align["Test ID"] = "l" # Left align
366+
pt.align["Test Description"] = "l" # Left align
367+
pt.padding_width = 1 # One space between column edges and contents (default)
368+
369+
for test in test_summary:
370+
pt.add_row(test)
371+
print pt
332372
print "Completed in %d sec" % (time() - start)

0 commit comments

Comments
 (0)