Skip to content
  • ZipCode Api
  • Blog
  • About RedLine13
RedLine13
RedLine13
Primary Navigation Menu
Menu
  • Start Testing
  • Demo
  • Pricing
  • JMeter
  • Partners
  • Docs
    • Documentation Home
    • AWS Set Up for load testing
    • AWS Approval for Large Tests
    • PHP, NodeJS, Python Load Tests
    • Scalability
    • Jenkins Plugin Setup
    • Premium Features
    • Knowledge Base

Functional Testing with k6

By: David Koziel

Functional Testing with k6

In previous posts we have covered the ability to use k6 for scalable load testing, however k6 is also a suitable framework for creating functional tests. In this brief post, we will show you how to get started with using k6 as a functional testing tool.

Assertions in k6

There are two basic ways to write assertions in k6. One is using the built-in check and group functions. Perhaps a better alternative is to use the ChaiJS extension (k6chaijs), which is actually the favored approach in the official k6 documentation. The latter allows use of the expect function making functional tests more human-readable.

Creating a k6 Functional Test

We will need to import the k6chaijs extension to allow us to use the describe and expect methods. Add the following line to the top of your k6 test plan script:

import chai, { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js';

Using the describe-expect pattern, we can write functional statements such as the following:

describe('Hello world!', () => {
    const response = http.get('https://test-api.k6.io/');
    expect(response.status, 'response status').to.equal(200);
});

In this example, a successful response from a request to the k6 test-api service is asserted. As long as the request out to this endpoint returns with a 200 success response, the test will continue to the next statement.

Advanced Functionality

The ChaiJS assertion library extends a suite of getter functions that can be chained together in complex ways. The above example shows the simplest assertion which chains the to and equal getters to create an equivalence test. Let’s consider the following assertion:

expect(response.json()).to.have.a.property('items');

Here the response object from a request is analyzed as JSON, and evaluated to contain an “items” property. Only if the response contains such a named property will the assertion succeed.

A few selected additional chained getters for assertions:

  • .to.have.validJsonBody() → stipulates that a valid JSON body must exist
  • .to.be.a('string') → analyzes the data type which must be a string
  • .to.not.be.empty → there must be a value specified for the analyzed target

There is an example in the k6 documentation that showcases all functionality of the ChaiJS describe-expect pattern. Furthermore, the official ChaiJS documentation contains detailed descriptions and additional examples for all chainable getters.


Did you know that RedLine13 offers a full-featured, time-limited free trial?  Sign up now, and move your k6 tests into the cloud today!

2024-02-14
Previous Post: The k6 Graphical Test Builder
Next Post: Common Tasks in k6

Recent Posts

  • JMeter XML Format Post Processor
  • Order of Elements in JMeter
  • The JMeter Synthesis Report
  • Using the JMeter Plugins Manager
  • JMeter Rotating JTL Listener

Related

  • JMeter XML Format Post Processor
  • Order of Elements in JMeter
  • SAML Configuration with Azure AD and RedLine13
  • SAML Configuration with OKTA and RedLine13
  • SAML Configuration with Google and RedLine13
  • The JMeter Synthesis Report
  • Using the JMeter Plugins Manager
  • JMeter Rotating JTL Listener
  • Using Test Fragments in JMeter Tests
  • Step-by-Step Guide to Testing with JMeter

© RedLine13, LLC | Privacy Policy | Contract
Contact Us: info@redline13.com

Designed using Responsive Brix. Powered by WordPress.