winIDEA SDK
itest_sample.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5import isystem.connect as ic
6import isystem.itest as it
7
8
9winidea_id = ''
10
11
12def verify_result(results):
13 while results.hasNextTestResult():
14 result = results.nextTestResult()
15 if result.isError():
16 out = ic.CStringStream()
17 emitter = ic.EmitterFactory.createYamlEmitter(out)
18 emitter.startStream()
19 emitter.startDocument(False)
20 result.serializeErrorsOnly(emitter, None)
21 emitter.endDocument(False)
22 emitter.endStream()
23 print(out.getString())
24 else:
25 print("OK")
26
27
28def init_target() -> it.PTestCase:
29 cmgr = ic.ConnectionMgr()
30 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
31
32 debug_ctrl = ic.CDebugFacade(cmgr)
33
34 # initialize the target - this may differ in your environment
35 debug_ctrl.reset()
36 debug_ctrl.runUntilFunction("main")
37 debug_ctrl.waitUntilStopped()
38
39 test_case = it.PTestCase(cmgr)
40
41 return test_case
42
43
44def run_test_case(test_case: it.PTestCase):
45
46 # The simplest test case, not very useful for testing,
47 # but may be used to call functions on the target, for example
48 # for initialization.
49 test_case.itest("func: [get_random]", None)
50
51 print("'get_random' finished!")
52
53 # Simple test of function: int funcTestInt2(int a, int b, int c)
54
55 test_case.itest(
56 """
57 func: [sum, [3, 4, 5], retVal]
58 expect:
59 - retVal == 12
60 """,
61 None, True)
62
63 print('Test result:', end=' ')
64 verify_result(test_case.getTestResultsContainer())
65
66
67# Intentional error
68def run_test_case_with_error(test_case: it.PTestCase):
69
70 test_case.itest(
71 """
72 func: [return_smaller, [3, 2], retVal]
73 expect:
74 - retVal == 2
75 """,
76 None)
77
78 print('return_smaller test result if error:', end=' ')
79 verify_result(test_case.getTestResultsContainer())
80
81
82def main():
83 test_case = init_target()
84 run_test_case(test_case)
85 run_test_case_with_error(test_case)
86
87
88if __name__ == '__main__':
89 main()