winIDEA SDK
script_callback_methods.py
1# This script is licensed under BSD License, see file LICENSE.txt.
2#
3# (c) TASKING Germany GmbH, 2023
4
5import isystem.itest as it
6import isystem.connect as ic
7
8
9winidea_id = ''
10
11
12class TestCustomMethods:
13 """
14 This class contains methods to be called during test execution.
15 The names of the methods must match those in test specification
16 tags 'endFunc' and 'initFunc'.
17 """
18
19 def __init__(self, connection_mgr: ic.ConnectionMgr, debug_ctrl: ic.CDebugFacade):
20 """
21 We can use constructor to store references to other objects
22 used by methods of this class.
23 """
24 self.connectionMgr = connection_mgr
25 self.__dbg = debug_ctrl
26
27 def my_init_test_func(self, a, b):
28 print('my_init_test_func:')
29 print(' Is target stopped:', self.__dbg.getCPUStatus().isStopped())
30
31 print(' a = ' + str(a))
32 print(' b = ' + str(b))
33
34 def my_end_test_func(self, a, b, c):
35 print('my_end_test_func:')
36 print(' a = ' + str(a))
37 print(' b = ' + str(b))
38 print(' c = ' + str(c))
39
40 def my_init_target_func(self, a, b):
41 print('my_init_target_func:')
42 print(' target status:' + self.__dbg.getCPUStatus().toString())
43 print(' a = ' + str(a))
44 print(' b = ' + str(b))
45
46 def my_restore_target_func(self, param):
47 print('my_restore_target_func:')
48 print(' ' + param)
49
50 def my_stub_hit_func(self, param):
51 print('my_stub_hit_func:')
52 print(' ' + str(param))
53 # modify stub return value
54 testCtrl = ic.CTestCaseController(self.connectionMgr, self._isys_testCaseHandle)
55 testCtrl.modify('srv', '144')
56
57
58def init_target() -> [ic.ConnectionMgr, ic.CDebugFacade]:
59 cmgr = ic.ConnectionMgr()
60 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
61
62 debug_ctrl = ic.CDebugFacade(cmgr)
63 debug_ctrl.reset()
64 debug_ctrl.runUntilFunction('main')
65 debug_ctrl.waitUntilStopped()
66 return cmgr, debug_ctrl
67
68
69def run_test(cmgr: ic.ConnectionMgr, debug_ctrl: ic.CDebugFacade):
70
71 test_case = it.PTestCase(cmgr)
72
73 # create and initialize the object, which contains method to be
74 # called during test. The same object can contain other methods
75 # to be called from other tests.
76 custom_methods = TestCustomMethods(cmgr, debug_ctrl)
77
78 test_case.itest("""
79 func: [init_globals]
80 stubs:
81 - func: [fibonacci, srv]
82 script: [my_stub_hit_func, ["['param1', 'param2']"]] # single param as list of strings
83 # double quotes separate YAML [] from Python []
84 initFunc: [my_init_test_func, [1, 2]]
85 endFunc: [my_end_test_func, [3, 4, '''a''']]
86 initTargetFunc: [my_init_target_func, [123, 987]]
87 restoreTargetFunc: [my_restore_target_func, ['"paramX"']]
88 """, custom_methods)
89
90 results = test_case.getTestResultsContainer()
91
92 # there is only one result, because we've executed only one test
93 result = results[0]
94
95 print('\n--------------------------------\n')
96
97 if not result.isError():
98 print('OK')
99 else:
100 print('Test ERROR')
101 if result.isException():
102 print('Exception: ', result.getExceptionString())
103
104 if result.isExpressionError():
105 data_results = ic.StrVector()
106 result.getExpressionResults(data_results)
107 for expr_result in data_results:
108 if len(expr_result) > 0:
109 print(expr_result)
110
111 if result.isCodeCoverageError():
112 print('Code Coverage Error')
113
114 if result.isProfilerCodeError():
115 print('Profiler Code Error')
116
117 if result.isProfilerDataError():
118 print('Profiler Data Error')
119
120
121def main():
122 cmgr, debug_ctrl = init_target()
123 print('initialized')
124 run_test(cmgr, debug_ctrl)
125
126
127if __name__ == '__main__':
128 main()