winIDEA SDK
create_test_report.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 createExampleOfLogEntry():
13 lr = ic.CLogResult(None)
14 lr.add('counter', '234', ic.CLogResult.E_SECTION_BEFORE_ASSIGN)
15 lr.add('size', '25', ic.CLogResult.E_SECTION_AFTER_ASSIGN)
16
17 return lr
18
19
20def createExampleOfStubResult(hitNo, stepIdx):
21 sr = ic.CTestPointResult(None)
22 sr.setId('fillArray')
23 sr.setLogResult(createExampleOfLogEntry())
24 sr.setExecStatus(ic.CTestPointResult.EXECUTED)
25 sr.setHitNoAndStepIdx(hitNo, stepIdx)
26 sr.setScriptInfoVar("evaluated all expressions")
27 sr.setScriptRetVal("") # should be empty to indicate no error
28 sr.addExpressionError("a == 10", "a = 9")
29
30 return sr
31
32
33def createExampleOfTestPointResult():
34 sr = ic.CTestPointResult(None)
35 sr.setId('myTestPointID')
36 sr.setLocation('src/main.cpp')
37 sr.setLogResult(createExampleOfLogEntry())
38 sr.setExecStatus(ic.CTestPointResult.EXECUTED)
39 sr.setHitNoAndStepIdx(0, 0)
40 sr.setScriptInfoVar("evaluated all expressions")
41 # non-empty ret val means error and contains contains error message
42 sr.setScriptRetVal("Eval. failed!")
43
44 return sr
45
46
47def createExampleOfStackUsage():
48 su = ic.CStackUsageResult(None)
49 minExpectedSize = 200
50 maxExpectedSize = 600
51 stackUsedBeforeTest = 150
52 testUsage = 0 # amount of stack used for test, for example creation
53 # of test local variables for unit tests.
54 appUsage = 50 # usage of function under test
55 su.setValues(minExpectedSize, maxExpectedSize, stackUsedBeforeTest,
56 testUsage, appUsage)
57
58 return su
59
60
61def createExampleOfPreConditionResult(isError):
62 pc = ic.CTestExprResult(None)
63 pc.setExpression('a == 2')
64 pc.addSubexpression('a', '2')
65 pc.setError(isError)
66
67 return pc
68
69
70def createExampleOfExprResult(expr, subExpr, value, isError):
71 pc = ic.CTestExprResult(None)
72 pc.setExpression(expr)
73 pc.addSubexpression(subExpr, value)
74 pc.setError(isError)
75
76 return pc
77
78
79def createExampleOfTestResult():
80 testSpec = ic.CTestSpecification()
81 testSpec.setTestId('demoTest')
82 testResult = ic.CTestResult(testSpec)
83 testResult.setResultComment('This is demo script for test report creation')
84 testResult.setTargetException(False)
85 testResult.appendScriptOutput("", "timing OK")
86 # testResult.appendScriptError("afterTest: ", "size of list is invalid")
87 testResult.addStubResult(createExampleOfStubResult(0, 0))
88 # we can add one result per stub or test point hit
89 testResult.addStubResult(createExampleOfStubResult(1, 0))
90 testResult.addTestPointResult(createExampleOfTestPointResult())
91 testResult.setStackUsageResult(createExampleOfStackUsage())
92 testResult.addExprResult(ic.CTestResult.E_SECTION_PRE_CONDITION,
93 createExampleOfPreConditionResult(False))
94 testResult.addExprResult(ic.CTestResult.E_SECTION_ASSERT,
95 createExampleOfExprResult('a == 4', 'a', '2', True))
96 testResult.setLogResult(createExampleOfLogEntry())
97
98 return testResult
99
100
101def getWinIDEAVersion():
102 cm = ic.ConnectionMgr()
103 cm.connect(ic.CConnectionConfig().instanceId(winidea_id))
104 ide = ic.CIDEController(cm)
105 ver = ide.getWinIDEAVersion()
106 return ver.toString()
107
108
109def createExampleOfReportConfig():
110
111 reportConfig = ic.CTestReportConfig()
112
113 reportConfig.setReportContents(ic.CTestReportConfig.RCONFIG_FULL)
114 reportConfig.setTestIDEAVersion("0.9") # you can set your test script version here
115 cm = ic.ConnectionMgr()
116 cm.connect(ic.CConnectionConfig().instanceId(winidea_id))
117 ide = ic.CIDEController(cm)
118 reportConfig.setWinIDEAVersion(getWinIDEAVersion())
119 reportConfig.setOutputFormat(ic.CTestReportConfig.FMT_XML)
120 reportConfig.setFileName('reportGeneratedByCustomScript.xml')
121 # Must not be empty string, so let's indicate none was used. Path
122 # defines output dir for the generated report file, if report file
123 # name is not given as absolute path
124 reportConfig.setIYamlFileName("./")
125 # Use standard isystem XML -> HTML transformation file and style sheet
126 reportConfig.setXsltForFullReport(it.DEFAULT_XSLT)
127 reportConfig.setCssFile(it.BLUE_CSS)
128
129 reportConfig.setIncludeTestSpec(False)
130 # report title
131 reportConfig.setXmlReportHeader("CUSTOM REPORT")
132 # setAbsPathForLinks(bool iaAbsPathsForLinks)
133 reportConfig.addUserInfo("date", "2019-04-04")
134 reportConfig.addUserInfo("testerName", "john")
135
136 return reportConfig
137
138
139def saveTestReport(testResults, reportConfig):
140 resultContainer = ic.CTestReportContainer()
141 for testResult in testResults:
142 resultContainer.putTestResult(ic.CTestSpecification(), testResult)
143
144 ic.CTestBench.saveTestResults(resultContainer, reportConfig)
145
146
147reportConfig = createExampleOfReportConfig()
148testResults = []
149testResults.append(createExampleOfTestResult())
150saveTestReport(testResults, reportConfig)
151# Get standard isystem XML -> HTML transformation file and style sheet.
152# Call this method only if XSLT and CSS files were set with
153# it.XSLT_BUILT_IN_PREFIX in reportConfig, and file names are set to
154# isystem default values
155it.copyXsltAndCss(reportConfig)
156print("OK")