winIDEA SDK
itest_with_parameters.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 init_target() -> ic.ConnectionMgr:
13 cmgr = ic.ConnectionMgr()
14 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
15
16 debug_ctrl = ic.CDebugFacade(cmgr)
17 debug_ctrl.reset()
18 debug_ctrl.runUntilFunction('main')
19 debug_ctrl.waitUntilStopped()
20 return cmgr
21
22
23def run_test_with_param_in_host_var(test_case: it.PTestCase):
24 """
25 This function uses host variables to specify test parameters.
26 """
27 host_vars = ic.CTestHostVars()
28 test_spec = 'func: [sum, ["${param1}", 4, 2], retVal]\n' + \
29 'expect:\n' + \
30 '- retVal == ${param1} + 6'
31 test_spec_obj = ic.CTestSpecification.parseTestSpec(test_spec)
32
33 for param_a in range(0, 5, 2):
34 host_vars.setValue('${param1}', str(param_a))
35
36 test_case.itest(test_spec_obj, None, False, host_vars)
37
38 print('\nsum test result:', end=' ')
39 results = test_case.getTestResultsContainer()
40 result = results.nextTestResult()
41 if result.isError():
42 print(result.toString())
43 else:
44 print(f"OK a = {param_a}")
45
46
47def run_test_with_param_in_string(test_case: it.PTestCase):
48 """
49 Builds the test specification using Python's string formatting.
50 """
51
52 for param_a in range(0, 5, 2):
53 test_spec = (f'func: [sum, [{param_a}, 2, 1], retVal]\n'
54 f'expect:\n'
55 f'- retVal == 3 + {param_a}')
56 test_spec_obj = ic.CTestSpecification.parseTestSpec(test_spec)
57
58 test_case.itest(test_spec_obj, None)
59
60 print('\nsum test result:', end=' ')
61 results = test_case.getTestResultsContainer()
62 result = results.nextTestResult()
63 if result.isError():
64 print(result.toString())
65 else:
66 print(f"OK a = {param_a}")
67
68
69def run_test_with_param_as_local_var(test_case: it.PTestCase):
70 """
71 Modifies local variable, which is used as function parameter.
72 """
73 for param_a in range(0, 5, 2):
74 test_spec = f"""
75 locals:
76 a: int
77 init:
78 a: {param_a}
79 func: [sum, [a, 2, 1], retVal]
80 expect:
81 - retVal == 3 + a"""
82
83 test_case.itest(test_spec, None)
84
85 print('\nsum test result:', end=' ')
86 results = test_case.getTestResultsContainer()
87 result = results.nextTestResult()
88 if result.isError():
89 print(result.toString())
90 else:
91 print(f"OK a = {param_a}")
92
93
94def run_test_with_test_spec_as_object(test_case: it.PTestCase):
95 """
96 This example uses test specification as a starting point,
97 and sets parameters later by using API. 'testSpec' is no longer a string,
98 but an object with methods.
99 """
100
101 test_spec = ic.CTestSpecification()
102 func_under_test = test_spec.getFunctionUnderTest(False)
103 func_under_test.setName("sum")
104 func_under_test.setRetValueName("retVal")
105
106 for param_a in range(0, 5, 2):
107
108 params = ic.StrVector()
109 params.append(str(param_a))
110 params.append(str(2))
111 params.append(str(3))
112 test_spec.getFunctionUnderTest(False).setPositionParameters(params)
113
114 exprs = ic.StrVector()
115 exprs.append('retVal == 5 + ' + str(param_a))
116 test_spec.setExpectedResults(exprs)
117
118 print(test_spec.toString())
119
120 test_case.itest(test_spec, None)
121
122 print('\nsum test result:', end=' ')
123 results = test_case.getTestResultsContainer()
124 result = results.nextTestResult()
125 if result.isError():
126 print(result.toString())
127 else:
128 print(f"OK a = {param_a}")
129
130
131def main():
132 cmgr = init_target()
133 test_case = it.PTestCase(cmgr)
134 print("Tests with host variable")
135 run_test_with_param_in_host_var(test_case)
136 print("Tests with parameter in YAML test specification string.")
137 run_test_with_param_in_string(test_case)
138 print("Test with parameter as local variable")
139 run_test_with_param_as_local_var(test_case)
140 print("Use test specification as object.")
141 run_test_with_test_spec_as_object(test_case)
142
143
144if __name__ == '__main__':
145 main()