Created
January 12, 2021 14:09
-
-
Save mstoykov/5e052293cc1bbdd11284a2cc5a90a194 to your computer and use it in GitHub Desktop.
junit summary function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<testsuites tests="2" failures="0"> | |
<testsuite name="tresholds_junit.js" tests="2" failures="0"> | |
<testcase name="http_req_duration + p(95)<50"><failure message="failed" ></testcase> | |
<testcase name="http_req_duration{url:http://httpbin.org/post} + max<1000" > | |
</testsuite> | |
</testsuites> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from "k6/http"; | |
import { check } from "k6"; | |
/* | |
* Thresholds are used to specify where a metric crosses into unacceptable | |
* territory. If a threshold is crossed the test is considered a failure | |
* and is marked as such by the program through a non-zero exit code. | |
* | |
* Thresholds are specified as part of the options structure. It's a set of | |
* key/value pairs where the name specifies the metric to watch (with optional | |
* tag filtering) and the values are JS expressions. Which could be a simple | |
* number or involve a statistical aggregate like avg, max, percentiles etc. | |
*/ | |
export let options = { | |
thresholds: { | |
// Declare a threshold over all HTTP response times, | |
// the 95th percentile should not cross 500ms | |
http_req_duration: ["p(95)<50"], | |
// Declare a threshold over HTTP response times for all data points | |
// where the URL tag is equal to "http://httpbin.org/post", | |
// the max should not cross 1000ms | |
"http_req_duration{url:http://httpbin.org/post}": ["max<1000"], | |
} | |
}; | |
export default function() { | |
http.get("http://httpbin.org/"); | |
http.post("http://httpbin.org/post", {data: "some data"}); | |
} | |
export function handleSummary(data) { | |
return {'junit.xml': generateJunitXML("tresholds_junit.js", data)} | |
} | |
function generateJunitXML(name, data) { | |
var failures = 0; | |
var cases = Object.entries(data.metrics).filter(k => k[1].thresholds != undefined).flatMap((e) => Object.entries(e[1].thresholds).flatMap((s) => { | |
if (s[1].ok) { | |
return `<testcase name="${e[0]} + ${s[0]}" >`; | |
} | |
return `<testcase name="${e[0]} + ${s[0]}"><failure message="failed" ></testcase>`; | |
})); | |
var result = `<?xml version="1.0"?> | |
<testsuites tests="${cases.length}" failures="${failures}"> | |
<testsuite name="${name}" tests="${cases.length}" failures="${failures}"> | |
${cases.join("\n")} | |
</testsuite> | |
</testsuites> | |
` | |
return result | |
} | |
Some HTML entity escaping is probably also required, but yes, basic JUnit support is simple. This also seems like it would run with --compatibility-mode=base
, but we should test it to be sure, before publishing to jslib.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is severely based on how it was done in https://github.com/Mattihew/k6-to-junit or at least partially:
The only thing I couldn't port is how long the test took ... But this is the bare minimum and I would argue checks should also be included in the JUnit report and maybe some better message should be thought off