-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFormatPythonPyTestJUnit.js
106 lines (89 loc) · 3.56 KB
/
FormatPythonPyTestJUnit.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
const { Webhooks } = require('@qasymphony/pulse-sdk');
exports.handler = function ({ event: body, constants, triggers }, context, callback) {
function emitEvent(name, payload) {
let t = triggers.find(t => t.name === name);
return t && new Webhooks().invoke(t, payload);
}
/////// Pulse version
var payload = body;
var testResults = payload.result;
var projectId = payload.projectId;
var cycleId = payload["test-cycle"];
xml2js = require('xml2js');
//////// Commandline version
// var fs = require('fs');
//
// var projectId = 12345;
// var cycleId = 12341234;
/// TODO: Remove above
var testLogs = [];
function FormatLogs(tr) {
var testResults = JSON.parse(tr);
testResults.testsuite.testcase.forEach(function (tc) {
var tcResult = tc["$"];
var tcName = "";
// Format the name
var note = "";
if (!tcResult.name)
tcName = "Unnamed";
else
tcName = tcResult.name.substring(0, tcResult.name.indexOf('['));
note = tcResult.name;
TCStatus = "PASS";
if (tc.failure) {
TCStatus = "FAIL";
if (note)
note = "\n" + JSON.stringify(tc.failure);
else
note = JSON.stringify(tc.failure);
}
// The automation content is what we're going to use to run this later so it's important to get that format for Python pytest
//$file :: $classname (after the last .) :: $name (before the [)
var tcShortClassName = tcResult.classname.substring(tcResult.classname.lastIndexOf('.') + 1)
var auto = tcResult.file + "::" + tcShortClassName + "::" + tcName;
var reportingLog = {
exe_start_date: new Date(), // TODO this could use the time to complete to be more precise
exe_end_date: new Date(),
module_names: [
'JUnitTests'
],
name: tcName,
automation_content: auto,
note: note
};
// There are no steps here, so we'll add one step entry
var testStepLogs = [{
order: 0,
description: tcName,
expected_result: tcName,
status: TCStatus
}];
reportingLog.description = "Test case imported from Python Test"
reportingLog.status = TCStatus;
reportingLog.test_step_logs = testStepLogs;
testLogs.push(reportingLog);
});
var formattedResults = {
"projectId": projectId,
"test-cycle": cycleId,
"logs": testLogs
};
return formattedResults;
}
// Pulse Version
var parser = new xml2js.Parser();
parser.parseString(testResults, function (err, result) {
var formattedResults = FormatLogs(JSON.stringify(result));
emitEvent('$YOUR_UPLOAD_TO_QTEST_EVENT_URL', formattedResults);
});
/// Command line version
// fs.readFile('results.xml', function(err, data) {
// parser.parseString(data, function (err, result) {
// var formattedResults = FormatLogs(JSON.stringify(result));
// Write new file
// var payload = fs.writeFile('formattedResults.json', JSON.stringify(formattedResults, null, " " ), 'utf8', function() {
// console.log("File written: formattedResults.json");
// });
// });
// });
}