|
1 |
| -export { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js'; |
| 1 | +export { textSummary } from 'https://jslib.k6.io/k6-summary/0.1.0/index.js'; |
2 | 2 |
|
3 |
| -let replacements = { |
4 |
| - '&': '&', |
5 |
| - '<': '<', |
6 |
| - '>': '>', |
7 |
| - "'": ''', |
8 |
| - '"': '"', |
9 |
| -}; |
| 3 | +export function generateJUnitXML(k6Json) { |
| 4 | + const xmlDoc = []; |
| 5 | + xmlDoc.push('<?xml version="1.0" encoding="UTF-8" ?>'); |
| 6 | + xmlDoc.push('<testsuites>'); |
10 | 7 |
|
11 |
| -function escapeHTML(str) { |
12 |
| - return str.replace(/[&<>'"]/g, function (char) { |
13 |
| - return replacements[char]; |
14 |
| - }); |
15 |
| -} |
| 8 | + function processGroup(group) { |
| 9 | + if (group.name) { // skip root group |
| 10 | + xmlDoc.push(`<testsuite name="${group.name}" tests="${group.checks.length}">`); |
16 | 11 |
|
17 |
| -/** |
18 |
| - * Generate a junit xml string from the summary of a k6 run considering each checks as a test case |
19 |
| - * @param {*} data |
20 |
| - * @param {String} suiteName Name of the test ex., filename |
21 |
| - * @returns junit xml string |
22 |
| - */ |
23 |
| -export function generateJUnitXML(data, suiteName) { |
| 12 | + group.checks.forEach(check => { |
| 13 | + let failed = check.fails > 0 ; |
| 14 | + xmlDoc.push(`<testcase classname="${group.name}" name="${check.name}">`); |
| 15 | + if (failed) { |
| 16 | + xmlDoc.push(`<failure message="Check failed. See output K6 task for more details.">${check.name}</failure>`); |
| 17 | + } |
| 18 | + xmlDoc.push('</testcase>'); |
| 19 | + }); |
| 20 | + } |
24 | 21 |
|
25 |
| - let failures = 0; |
26 |
| - let cases = []; |
27 |
| - let time = (data.state.testRunDurationMs) / 1000; |
28 |
| - let checks = []; |
29 |
| - if (data.root_group.checks.length > 0) { |
30 |
| - checks = data.root_group.checks; |
31 |
| - } else if (data.root_group.hasOwnProperty('groups') && data.root_group.groups.length > 0) { |
32 |
| - let groups = data.root_group.groups; |
33 |
| - groups.forEach((group) => { |
34 |
| - if (group.groups.length > 0) { |
35 |
| - let subGroups = group.groups; |
36 |
| - subGroups.forEach((subGroup) => { |
37 |
| - subGroup.checks.forEach((check) => { |
38 |
| - checks.push(check); |
39 |
| - }); |
40 |
| - }); |
41 |
| - } else { |
42 |
| - group.checks.forEach((check) => { |
43 |
| - checks.push(check); |
44 |
| - }); |
45 |
| - } |
| 22 | + group.groups.forEach(subGroup => { |
| 23 | + processGroup(subGroup); |
46 | 24 | });
|
| 25 | + |
| 26 | + xmlDoc.push('</testsuite>'); |
47 | 27 | }
|
48 |
| - checks.forEach((check) => { |
49 |
| - if (check.passes >= 1 && check.fails === 0) { |
50 |
| - cases.push(`<testcase classname="${escapeHTML(check.name)}" name="${escapeHTML(check.name)}" time="0"/>`); |
51 |
| - } else { |
52 |
| - failures++; |
53 |
| - let errmsg = "See the output of the "Run K6" step to see more details"; |
54 |
| - cases.push(`<testcase classname="${escapeHTML(check.name)}" name="${escapeHTML(check.name)}" time="0"><failure message="${errmsg}"/></testcase>`); |
55 |
| - } |
56 |
| - }); |
57 | 28 |
|
58 |
| - return ( |
59 |
| - `<?xml version="1.0" encoding="UTF-8" ?>\n` + |
60 |
| - `<testsuites>\n` + |
61 |
| - `<testsuite package="${escapeHTML(suiteName)}" name="${escapeHTML(suiteName)}" id="0" tests="${cases.length}" failures="${failures}" time="${time}">\n` + |
62 |
| - `${cases.join('\n')}\n</testsuite>\n</testsuites>` |
63 |
| - ); |
| 29 | + processGroup(k6Json.root_group); |
| 30 | + |
| 31 | + xmlDoc.push('</testsuites>'); |
| 32 | + return xmlDoc.join('\n'); |
64 | 33 | }
|
0 commit comments