Coder Social home page Coder Social logo

k6-junit's Introduction

k6-junit

NPM   CI  

k6 JUnit summary exporter library.

Usage

import {jUnit} from "k6-junit";
// ...
export function handleSummary(data) {
    console.log('Preparing the end-of-test summary...');
    return {
        "./test-results.xml": jUnit(data)
    };
}

Typescript integration

Based on k6-template-typescript.

Add dev-dependency to package.json:

{
  "devDependencies": {
    "k6-junit": "X.X.X"
  }
}

Resolve k6-junit package as internal dependecy in webpack.config.js:

// ...
module.exports = {
  // ...
    externals: [
        function ({context, request}, c) {
            if (request.startsWith('k6') || request.startsWith('https://')) {
                return request === 'k6-junit' ? c() : c(null, 'commonjs ' + request);
            }
            return c();
        },
    ],
  // ...
}

Using without transpiling

In case when it is necessary to avoid transpiling to js and run k6 right on typescript tests, you should modify import statement:

import {jUnit} from "./node_modules/k6-junit/index.js";

Configuration

Since your project may have its own features there is a possibility to adjust behaviour to your needs. There are several configuration parameters available. Please, see their reference below:

Parameter Name Description Default value
includeThresholds Allows you to control the inclusion of Threshold metrics during the export true
testCasePassCondition Allows you to control the logic which decides whether testCase passed or not passed > 0 && failed === 0
maxGroupNestingLevel Defines the maximum level of group nesting, more nested groups will be flattened as test-cases 1

If you need to override default values, please call jUnit function with cfg argument defined. So that, your specific configuration will be used by the library.

import {jUnit} from "k6-junit";
// ...
    return {
        "./test-results.xml": jUnit(data, {
           includeThresholds: false,
           testCasePassCondition: (passed, failed) => passed > 0 && failed <= passed,
           maxGroupNestingLevel: 2
        })
    };
// ...

Example k6 JSON Summary

{
  "metrics": {
    "http_req_duration": {
      "avg": 123.456,
      "max": 123.456,
      "med": 123.456,
      "min": 123.456,
      "p(90)": 123.456,
      "p(95)": 123.456,
      "thresholds": {
        "p(90) < 100": false,
        "p(95) < 130": true
      }
    }
  },
  "root_group": {
    "name": "",
    "path": "",
    "id": "d41d8cd98f00b204e9800998ecf8427e",
    "groups": [
      {
        "name": "Register New User",
        "path": "::Register New User",
        "id": "ef69e341fa4ed9426351b0cf6861b7ac",
        "groups": [],
        "checks": [
          {
            "passes": 1,
            "fails": 0,
            "name": "Access token:[empty]",
            "path": "::Register New User::Access token:[empty]",
            "id": "0de369f3d729210fd6916a3a19b78660"
          },
          {
            "path": "::Register New User::Sign-up confirmation is successful",
            "id": "c1575f06e564d14c750ade1e016a4f44",
            "passes": 0,
            "fails": 1,
            "name": "Sign-up confirmation is successful"
          }
        ]
      }
    ],
    "checks": [
      {
        "name": "200 OK",
        "path": "::200 OK",
        "id": "a6e8273230e2df18dd7e2067b12fc13c",
        "passes": 5,
        "fails": 70
      }
    ]
  }
}
<?xml version="1.0"?>
<testsuites tests="5" failures="3">
  <testsuite id="0" name="Root" tests="1" failures="1">
    <testcase name="200 OK" classname="Root" >
      <failure message="5 / 70 (6.67%) checks passed" />
    </testcase>
  </testsuite>
  <testsuite id="1" name="Register New User" tests="2" failures="1">
    <testcase name="Access token:[empty]" classname="Register New User" />
    <testcase name="Sign-up confirmation is successful" classname="Register New User" >
      <failure message="0 / 1 (0.00%) checks passed" />
    </testcase>
  </testsuite>
  <testsuite id="2" name="Thresholds" tests="2" failures="1">
    <testcase name="http_req_duration: p(90) &lt; 100" classname="Thresholds" >
      <failure message="threshold exceeded" />
    </testcase>
    <testcase name="http_req_duration: p(95) &lt; 130" classname="Thresholds" />
  </testsuite>
</testsuites>

LICENSE

Distributed under the MIT License. See LICENSE for more information.

k6-junit's People

Contributors

eshepelyuk avatar mikocot avatar simbadltd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mmosallem mikocot

k6-junit's Issues

please add to jslib - https://jslib.k6.io/

I use this module because our ci gitlab-ci supports junit so I can see via gitlabs restapi which tests that fails.
this module works excellent, but I need node installed to install it, which bloats up the container.

I know I can craft it so I do not need node, but then I loose the flexibility of versioning control and updating.
So it would be great if it can be added to https://jslib.k6.io/.

Thanks
MortenB

Module specifier "k6-junit" was tried to be loaded as remote module by prepending "https://" to it, which didn't work

I get this error:

WARN[0000] The moduleSpecifier "k6-junit" has no scheme but we will try to resolve it as remote module. This will be deprecated in the future and all remote modules will need to explicitly use "https" as scheme. 
ERRO[0000] Module specifier "k6-junit" was tried to be loaded as remote module by prepending "https://" to it, which didn't work. If you are trying to import a nodejs module, this is not supported as k6 is _not_ nodejs based. Please read https://k6.io/docs/using-k6/modules for more information. Remote resolution error: "Get "https://k6-junit": dial tcp: lookup k6-junit: no such host"
        at go.k6.io/k6/js.(*InitContext).Require-fm (native)
        at file:///Users/poponuts/Repositories/k6/k6.config.ts:5:0(22)
        at go.k6.io/k6/js.(*InitContext).Require-fm (native)
        at file:///Users/poponuts/Repositories/k6/support/authenticate.ts:3:0(37)
        at go.k6.io/k6/js.(*InitContext).Require-fm (native)
        at file:///Users/poponuts/Repositories/k6/test/user.api.ts:4:0(46)  hint="script exception"

My webpack.config.js has:

    externals: [
        function ({context, request}, c) {
            if (request.startsWith('k6') || request.startsWith('https://')) {
                return request === 'k6-junit' ? c() : c(null, 'commonjs ' + request);
            }
            return c();
        },
    ],

I created a kg.config.ts with the following:

import {jUnit} from "k6-junit";

export function handleSummary(data) {
    console.log('Preparing the end-of-test summary...');
    return {
        "./junit.xml": jUnit(data)
    };
}

Understandably, package.json has:

{
  "devDependencies": {
    "k6-junit": "X.X.X"
  }
}

Support for nested checks

Hi, it looks like the current version doesn't support nested checks.

If tests are in form of

describe( .....
       describe(...... 
               describe(......
                       expect(........

only the outer case will be taken into account as a test suite, and since there are no checks directly in it, it will be ignored, generating empty file (with only the root test suite.

It's not trivial to add it, but it's also not super hard. Do you plan to do it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.