5import isystem.connect
as ic
6import isystem.itest
as it
13 is_pylab_available =
True
14except Exception
as ex:
15 is_pylab_available =
False
19 def __init__(self, no_of_sub_tasks):
20 self.no_of_sub_tasks = no_of_sub_tasks
26 def subTask(self, task_desc):
29 def worked(self, count):
30 self.test_count += count
31 print(
'#' * self.test_count +
'-' * (self.no_of_sub_tasks - self.test_count))
34def init_target() -> ic.ConnectionMgr:
35 cmgr = ic.ConnectionMgr()
37 cmgr.connect(ic.CConnectionConfig().instanceId(winidea_id))
39 debug_ctrl = ic.CDebugFacade(cmgr)
40 session_ctrl = ic.CSessionCtrl(cmgr)
43 session_ctrl.begin_reset()
45 debug_ctrl.runUntilFunction(
'main')
46 debug_ctrl.waitUntilStopped()
51def run_test(connection_mgr: ic.ConnectionMgr) -> ic.CTestReportContainer:
52 test_case = it.PTestCase(connection_mgr)
60 test_spec = ic.CTestSpecification.parseTestSpec(
63 func: [fibonacci, [0], rv]
66 document: fibonacci.trd
68 isDecisionCoverage: true
74 - func: [fibonacci, [0], rv]
78 document: fibonacci.trd
96 expect: [rv == 13 @@ d]
99 document: fibonacci.trd
101 isDecisionCoverage: true
111 monitor = Monitor(test_spec.getNoOfTests(True))
113 print(
'Running tests to achieve full coverage...')
114 test_case.runTests(test_spec,
None, monitor)
116 test_results = test_case.getTestResultsContainer()
120def save_test_Results(test_results: ic.CTestReportContainer, resultFileName: str):
121 error_file_stream = ic.CFileStream(resultFileName)
122 err_emitter = ic.EmitterFactory.createCSVEmitter(error_file_stream,
",",
True)
123 test_results.resetTestResultIterator()
125 while test_results.hasNextTestResult():
126 result = test_results.nextTestResult()
128 err_emitter.startDocument(
False)
129 result.serialize(err_emitter,
None)
130 err_emitter.endDocument(
False)
133 err_stream = ic.CStringStream()
134 emitter = ic.EmitterFactory.createYamlEmitter(err_stream)
136 emitter.startStream()
137 emitter.startDocument(
False)
138 result.serializeErrorsOnly(emitter,
None)
139 emitter.endDocument(
False)
142 print(err_stream.getString())
144 err_emitter.endStream()
145 error_file_stream.close()
148def plot_coverage(test_results: ic.CTestReportContainer, is_plot_all=
False):
150 Draws a plot showing the amount of coverage in each test.
151 This function requires
'matplotlib' and 'numpy' Python packages to be
156 branches_executed = []
158 branches_not_taken = []
161 test_results.resetTestResultIterator()
162 while test_results.hasNextTestResult():
163 result = test_results.nextTestResult()
164 cvrg_result = ic.StrCoverageTestResultsMap()
165 result.getCoverageResults(cvrg_result)
166 for function
in cvrg_result:
167 stats = cvrg_result[function].getMeasuredCoverage(
True)
169 bytes_executed.append(stats.getBytesExecuted())
170 lines_executed.append(stats.getSourceLinesExecuted())
171 branches_executed.append(stats.getBranchExecuted())
172 branches_taken.append(stats.getBranchTaken())
173 branches_not_taken.append(stats.getBranchNotTaken())
174 branches_both.append(stats.getBranchBoth())
176 x = range(len(bytes_executed))
177 pl.plot(x, bytes_executed,
'.-', label=
'bytes executed')
178 pl.plot(x, branches_both,
'.-', label=
'branches both')
180 pl.plot(x, lines_executed,
'o', label=
'lines executed')
181 pl.plot(x, branches_taken,
'--', label=
'branches taken')
182 pl.plot(x, branches_not_taken,
'--', label=
'branches not taken')
183 pl.plot(x, branches_executed,
'--', label=
'branches executed')
185 pl.axis([0, 5, 0, 110])
187 pl.xlabel(
'test number')
189 pl.title(
"Coverage of function under test")
197 test_results = run_test(cmgr)
199 result_file_name =
'errorsReport.csv'
200 print(
'Tests finished, saving results to file: ', result_file_name)
201 save_test_Results(test_results, result_file_name)
203 if is_pylab_available:
204 print(
'Showing results in graph (matplotlib and numpy packages must be installed!).')
205 plot_coverage(test_results)
207 print(
"Can't plot coverage results, pylab module is not available.")
210if __name__ ==
'__main__':