How to integrate k6 with Xray/Jira

Samson - Sep 9 - - Dev Community

In this article, I shall demonstrate a simple way we can integrate k6 with XRAY/Jira.

Sometime back I was assigned a task to write a performance test for an API that was expected to handle quite several requests. For this reason, we needed a good tool which would be faster to pick up and easier for any QA engineer to contribute.
Having used load impact in the past, I was quite familiar with K6. These are the main reasons why we chose k6 over the other performance testing tools:

  • Uses Javascript: Most of the QA/Developers in my team were familiar with javascript, therefore, this made it easier to pick up as there was no need to learn a new language

  • Open-source: This means, no payments are required to use the tool, and the community is very active.

  • CI/CD: Integrating k6 with our CI/CD pipelines was straightforward

I can continue discussing the advantages of choosing K6, but I will write a new post specifically about that.

After completing our test framework, we wanted our test results on Jira. Since we were already using XRAY, we needed a solution to convert the k6 JSON report to x-ray format. I couldn't get any solution that worked with our case at that time, So I had to come up with a solution. k6 made it easy by allowing me to get all metrics after the test lifecycle through the handleSummary() function, I only needed to pass the object to my implemented script and convert it to any desired format. But first, let me describe briefly the handleSummary() function.

K6 handleSummary()

This is a function in K6 used to generate custom summary reports after a load test has been completed. This function allows you to define how the summary data should be formatted and where it should be saved.

This is an essential feature that can be used to get all the metrics with different options. These options are stdout, XML, and JSON.

For this, we only needed to create a script to take in the data object from the handleSummary function.

Below is the script to convert the data object from k6 to a simple XRAY format report:

k6-XRAY-script

How to Setup the generator Helper Script for k6 and Xray Integration

Clone the repo to your desired location:
Preferably, create a folder inside the main project.

Example:
helper, src, report

This will help you manage the imports well without issues:

Prerequisites

Before you begin, ensure you have the following installed on your machine:

Usage

If your k6 tests are organized in groups, and each group title corresponds to a test case on Xray, you can use the generator script to create a JSON file compatible with Xray.

Example

The image below from Xray docs shows test cases with keys CALC-01 and CALC-02.

Xray Test Cases

In your k6 test script, you can name the group titles as CALC-01 and CALC-02. The script will search for these group names and assign the test results to the respective test cases on Xray.

group('CALC-01', function() {
  // test code
});
group('CALC-02', function() {
  // test code
});
Enter fullscreen mode Exit fullscreen mode

Output

The script generates a JSON file compatible with Xray, saved in the same directory as the script.

Clone the repo

git clone https://github.com/skingori/k6-json-xray.git
Enter fullscreen mode Exit fullscreen mode

How to Setup the Script

We will use the handleSummary function provided by k6 and textSummary from our generator.js script to generate the JSON file. The handleSummary function takes in a data object, which we pass to getSummary to modify into an Xray-compatible format.

Read more about k6 HandleSummary here

Change open your execution script and add the following lines:

import { getSummary } from "./generator.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
Enter fullscreen mode Exit fullscreen mode

I am using the ./generator.js directly as it was in the same folder as my script. Let's assume you were using helper this should be:

import { getSummary } from "./helper/generator.js";
Enter fullscreen mode Exit fullscreen mode

Add the end of your code adds the handleSummary function:

export function handleSummary(data) {
    return {
        stdout: textSummary(data, { indent: " ", enableColors: true }),
        "summary.json": JSON.stringify(getSummary(data, "CALC-2062", "CALC"), null, 2)
    };
}
Enter fullscreen mode Exit fullscreen mode

Our function getSummary will convert the data object to x-ray format and save the output to summary.json file

Why are we using textSummary?

To have a printed output on the console, we need to import textSummary from k6 JS utility library

But this may not apply to everyone if you don't need any stdout report, you don't have to import the textSummary

Example
import http from 'k6/http';
import { sleep, group, check } from 'k6';
import { getSummary } from "./generator.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";

export const options = {
    vus: 10,
    duration: '30s',
};

export default function() {
    group('CALC-01', function() {
        const resp = http.get('http://test.k6.io');
        check(resp, {
            'status is 200': (r) => r.status === 200,
        });
        sleep(1);
    });

    group('CALC-02', function() {
        const resp = http.get('http://test.k6.io');
        check(resp, {
            'status is 200': (r) => r.status === 200,
        });
        sleep(1);
    });
};

export function handleSummary(data) {
    return {
        stdout: textSummary(data, { indent: " ", enableColors: true }),
        "summary.json": JSON.stringify(getSummary(data, "CALC-2062", "CALC"), null, 2)
    };
}
Enter fullscreen mode Exit fullscreen mode

Note: You can eliminate stdout: textSummary(data, { indent: " ", enableColors: true }), line if you don't want to import textSummary

handleSummary works by default and it's usually called at the end of the test lifecycle.

Running the Script

To run the script, use the following command:

k6 run script.js -e TEST_PLAN_KEY="CALC-2345" -e TEST_EXEC_KEY="CALC-0009"
Enter fullscreen mode Exit fullscreen mode

The TEST_PLAN_KEY and TEST_EXEC_KEY are used to identify the test plan and test execution on Xray.

Read more about the test plan and test execution keys here

Output

The above script will produce the following report under summary.json

{
  "info": {
    "summary": "K6 Test execution - Mon Sep 09 2024 21:20:16 GMT+0300 (EAT)",
    "description": "This is a k6 test with a maximum iteration duration of 4.95s, 198 passed requests and 0 failures on checks",
    "user": "k6-user",
    "startDate": "2024-09-09T18:20:16.000Z",
    "finishDate": "2024-09-09T18:20:16.000Z",
    "testPlanKey": "CALC-2345"
  },
  "testExecutionKey": "CALC-0009",
  "tests": [
    {
      "testKey": "CALC-01",
      "start": "2024-09-09T18:20:16.000Z",
      "finish": "2024-09-09T18:20:16.000Z",
      "comment": "Test execution passed",
      "status": "PASSED"
    },
    {
      "testKey": "CALC-02",
      "start": "2024-09-09T18:20:16.000Z",
      "finish": "2024-09-09T18:20:16.000Z",
      "comment": "Test execution passed",
      "status": "PASSED"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

To get more details about k6 and X-ray kindly reach out to their documentation:
K6 Document
XRAY Document

Check out this as well - How to create and manage test cases with Xray and Jira a cool article written by Sérgio Freire

And as always, feel free to reach out to me!

LinkedIn
Email
Github

.
Terabox Video Player