-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
231 lines (214 loc) · 6.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (c) 2020-2021, Pelion and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const version = '2.2.0'
var Mocha = require('mocha')
var fs = require('fs')
var jsonminify = require('jsonminify')
var program = require('commander')
var xml_builder = require('junit-report-builder')
program
.version(version)
.option(
'-c, --configuration [file]',
'specify the configuration file [config.json]',
'config.json'
)
.option('-e, --export [treasure]', 'export to csv and xml')
.allowUnknownOption()
.parse(process.argv)
global.config = JSON.parse(
jsonminify(fs.readFileSync(program.configuration, 'utf8'))
)
var Logger = require('./utils/logger')
var logger = new Logger({ moduleName: '', color: 'green' })
global.SQALogLevel = global.config.log_level || 2
let suiteReport = {
title: global.config.title,
relayID: require('./utils/getRelayID.js')(
'/userdata/edge_gw_config/identity.json'
),
softwareBuild: require('./utils/getSoftwareBuildVersion.js')(
'/wigwag/etc/versions.json'
),
hardwareVersion: require('./utils/getHardwareVersion.js')(
'/userdata/edge_gw_config/identity.json'
),
radioConfig: require('./utils/getRadioConfig.js')(
'/userdata/edge_gw_config/identity.json'
),
ipAddress: require('./utils/getNetworkAddress')(),
status: 'STARTING',
startTime: '',
endTime: '',
totalTime: '',
totalTestCasesRan: 0,
totalFailedTestCases: 0,
totalPassedTestCases: 0,
tests: []
}
var exportToCSVAndXML = code => {
if (code == 1) {
suiteReport.status = 'UNSTABLE'
} else {
suiteReport.status = 'STABLE'
}
var td_json_report = JSON.parse(
jsonminify(
fs.readFileSync('suite_reports/perts_mocha_suite_report.json', 'utf8')
)
)
suiteReport.totalTestCasesRan = td_json_report.stats.tests
suiteReport.totalPassedTestCases = td_json_report.stats.passes
suiteReport.totalFailedTestCases = td_json_report.stats.failures
suiteReport.startTime = td_json_report.stats.start
suiteReport.endTime = td_json_report.stats.end
suiteReport.totalTime = td_json_report.stats.duration
td_json_report.results.forEach(suites => {
suites.suites.forEach(subsuites => {
subsuites.suites.forEach(suite => {
suite.tests.forEach(test => {
test.fullFile = suite.fullFile
suiteReport.tests.push(test)
})
})
})
})
fs.writeFileSync(
'./suite_reports/perts_exported_report.json',
JSON.stringify(suiteReport, null, 4)
)
let suiteReportCSV
let testcase_id_counter = 0
let suiteName = ''
Object.keys(suiteReport).forEach(function (key) {
if (key !== 'tests') {
suiteReportCSV += key + ',' + suiteReport[key] + '\n'
} else {
//in categories
var suite = xml_builder
.testSuite()
.name(global.config.title)
.timestamp(new Date())
suiteReportCSV +=
'Functional Area,Level,Test Case ID,Category,Test Case,File Path,Automation Status,Automation Execution Time(ms),Error,Error Log\n'
suiteReport[key].forEach(function (test) {
var fullTitleArr = test.fullTitle.split(' ')
var level =
fullTitleArr[0].replace('[', '') + fullTitleArr[1].replace(']', '')
var cat = fullTitleArr[2]
var functional_area = fullTitleArr[3]
var testsuite_name = level + ' ' + cat
if (suiteName !== testsuite_name) {
suite = xml_builder
.testSuite()
.name(testsuite_name)
.timestamp(new Date())
suiteName = testsuite_name
}
if (!test.fail && test.state == 'passed') {
suite
.testCase()
.className(test.fullTitle)
.name(test.title)
.time(test.duration)
test.status = 'PASSED'
} else if (test.state == 'pending') {
test.status = 'SKIPPED'
suite
.testCase()
.className(test.fullTitle)
.name(test.title)
.time(test.duration)
.skipped(test.title)
} else {
test.status = 'FAILED'
if (test.err.message) {
test.err.message = test.err.message.replace(/[\n,]/g, '')
}
suite
.testCase()
.className(test.fullTitle)
.name(test.title)
.time(test.duration)
.failure(test.err.message)
}
suiteReportCSV +=
(functional_area || '--') +
',' +
(level || '--') +
',' +
level +
'-' +
testcase_id_counter +
',' +
cat +
',' +
test.title +
',' +
test.fullFile +
',' +
test.status +
',' +
(test.duration || '--') +
',' +
(test.err.message || '--') +
',' +
(test.err.stack || '--') +
'\n'
testcase_id_counter++
})
}
})
fs.writeFileSync('./suite_reports/perts_exported_report.csv', suiteReportCSV)
logger.info(
'CSV formatted report generated and saved as- ' +
'./suite_reports/perts_exported_report.csv'
)
xml_builder.writeTo('./suite_reports/perts_exported_report.xml')
logger.info(
'XML formatted report generated and saved as- ' +
'./suite_reports/perts_exported_report.xml'
)
process.exit(code)
}
var execute = () => {
var mocha = new Mocha({
reporter: 'mochawesome',
reporterOptions: {
reportDir: 'suite_reports',
reportFilename: 'perts_mocha_suite_report'
}
})
global.config.test_cases.forEach(function (file) {
if (fs.existsSync(file)) {
mocha.addFile(file)
} else {
logger.error('Testcase path ' + file + ' not found')
}
})
mocha.globals().suite._timeout = global.config.mocha_timeout
var code = 0
mocha.run(function (failures) {
if (failures) code = 1
if (program.export) {
exportToCSVAndXML(code)
} else {
process.exit(code)
}
})
}
execute()