What you'll learn
- Define tests using k6
- Define KPIs, run the test and push the test report to Xray
- Validate in Jira that the test results are available
Source-code for this tutorial
- code is available in GiHub
k6 also has a Cloud version that is a commercial SaaS product, positioning itself as a companion for the k6 open source solution.
The test will exercise 3 different endpoints:
- Perform GET requests to the "/login" endpoint
- Perform POST requests to "/reserve" endpoint (where we will attempt to to reserve a flight from Paris to Buenos+Aires)
- Perform POSt requests to "/purchase" endpoint (where we will try to acquire the above reserved flight adding the airline company and the price)
In the documentation you will find that there are several ways to use the tool and to define performance Tests.
In our case we are targeting to have requests that will exercise some endpoints in our application and that will produce a failed or successful output based on the KPIs that are suited for our application. Keep in mind that we also want to execute these Tests in a CI/CD tool and ingest the results back to Xray.
The tests, as we have defined above, will target 3 different endpoints, for that we wrote a default function().
Next we created three objects that are mirroring the operations we want to exercise:
For each, we have defined the endpoint we want to access, the parameters needed to perform the operation; that are previously defined in constants. Finally, set the Tests to run in parallel, in a batch, as you can see below:
We have defined it like below:
These options will instruct k6 how we want to execute the performance test, in more detail, this means that we will ramp up VUs (virtual users) to reach 50 VUs in one minute, maintain those 50 VUs for 30 seconds and decrease the users until 0 in on minute. The requests will be randomly chosen from the http.batch entries we have defined earlier.
In order to execute the tests you can use several ways, for our case we are using the command line.
The command line output will look like this:
This will be enough to execute performance tests, however a manual validation of results must always be done in the end to assess if the performance is enough or not, and looking at Json files is not always easy.
We need the ability to:
- Define KPI that will assert the performance results and fail the build of they are not fulfilled in an automated way (this will be useful to integrate in CI/CD tools)
- Convert the KPI result in a way that can be ingested in Xray
In order to do that we will use the Thresholds available in k6 and use the handleSummary callback to generate a Junit Test Result file ready to be imported to Xray. Notice that with this function you can parse and generate the output that is better suited for your tests.
In order to use performance tests in a pipeline we need them to be able to fail the build if the result is not the expected. We need to have the ability to automatically assess if the performance tests were successful (within the parameters we have defined) or not.
k6 has an out of the box ability to define Thresholds. In our case we want to define the following ones globally:
- the 90 percentile exceeds 500ms an error will be triggered,
- the requests per second will exceed 500ms an error will be generated
- any error appear during the execution an error will be triggered (because of the error rate KPI).
To achieve this we have added the following thresholds in the options :
Once we execute the test again we will notice that now we have information about the assertions and those results can be acted upon:
Now we are executing Tests to validate the performance of our application and we are capable of defining KPIs to validate each performance indicator in a build (enable us to add these Tests to CI/CD tools given that the execution time is not long). What we need is to be able to ship these results to Xray and to bring visibility over these types of Tests.
k6 prints a summary report to stdout that contains a general overview of your test results. It includes aggregated values for all built-in and custom metrics and sub-metrics, thresholds, groups, and checks.
k6 also provides the possibility to use the handleSummary callback. In this callback we can define in which way we want the output to be generated, it provides access to the data available in the test and allow to treat that data in the way you see fit for your purpose.
In our case we used pre-defined functions to produce 3 outputs and added code to produce a JUnit report with more information to be imported to Xray:
- textSummary - to write in stdout the summary of the execution.
- JUnit - to write to a xml file the JUnit results of the Tests.
- JSON.stringify - to produce a json file with the summary of the requests and metrics.
- generateXrayJUnitXML - to write a xml JUnit file with extra information, such as, more detail information of the thresholds and the ability to add a file as an evidence.
The code added will look like this:
The xrayJunit.xml file generated is:
With the extra code we have added extra information to the JUnit report, it is based in the default JUnit available in k6 with extra fields added.
To achieve it, we have added and extra file: "junitXray.js" that will handle these new informations.
The main method is the one that will generate the JUnit report.
As you can see we are treating two cases:
- When the threshold is ok, we add properties that will enrich the report in Xray, namely: comment, description and summary.
- When the threshold is not ok, we add the same above properties plus a failure message and an evidence file that will holds the details of the performance Test.
This is just an example of one possible integration, you can reuse it or come up with one that better suites your needs.
Integrating with Xray
As we saw in the above example where we are producing JUnit report with the result of the tests, it is now a matter of importing those results to your Jira instance. This can be done by simply submitting automation results to Xray through the REST API, by using one of the available CI/CD plugins (e.g. for Jenkins), or using the Jira interface to do so.
In this case we will show you how to import via the API.
Once you have the report file available you can upload it to Xray through a request to the REST API endpoint. For that, the first step is to follow the instructions in v1 or v2 (depending on your usage) to obtain the token we will be using in the subsequent requests.
The request made will look like:
The response of this request will return the token to be used in the subsequent requests for authentication purposes.
Once you have the token, we will use it in the API request passing the target project and the Test Plan.
With this command we are creating a new Test Execution that will have the results of the Tests that were executed and it will be associated to the Test Plan XT-251.
Once uploaded the Test Execution will look like the example below
We can see that a new Test Execution was created with a summary automatically generated and 3 Tests were added with the corresponding status and summary (matching the information from the json report).
In order to check the details we click on the details icon next to each Test (below the red arrow in the above screenshot), this will take us to the Test Execution Details Screen
In the Test Execution details we have the following relevant information:
- Summary - Combination of the metric and the threshold defined in the KPIs.
- Execution Status - Passed, this indicates the overall status of the execution of the performance tests.
- Evidence - Holding the file with more detailed information about the performance Test.
- Comment - Showing the comment we have defined in the XrayJunit.xml file.
- Test Description - Allowing adding a specific description for the Test Execution.
- Definition - A unique identifier generated by Xray to uniquely identify this automated Test
- Results - Detailed results with information of the KPI's defined and the value that has breached the KPIs (in case of failure).
Bringing the information of performance tests to your project will give you an overview of the entire testing process and bring that visibility up front for the team to have all the elements necessary to deliver quality products.
- after results are imported in Jira, Tests can be linked to existing requirements/user stories, so you can track the impacts on their coverage.
- results from multiple builds can be linked to an existing Test Plan, to facilitate the analysis of test result trends across builds.
- results can be associated with a Test Environment, in case you want to analyze coverage and test results using that environment later on. A Test Environment can be a testing stage (e.g. dev, staging, preprod, prod) or an identifier of the device/application used to interact with the system (e.g. browser, mobile OS).