Generate random test data with faker in k6

Generate random test data with faker in k6

Data Generation

What I want to address here in this section is majorly data generation during your test. I got a few questions from QA engineers who read the first post on k6. I will show you how to generate random data in your test .

To show this , I will test a API endpoint that requires unique usernames and emails for sign up . Since we will be running hundreds of virtual users, we need to reliably generate unique emails and usernames across the test.

Don't worry , we have something in the tool box that can do this for us, it's called Faker.Js. It's a very good library that you can use in your test to generate data. We can't use faker directly because it's a

Install Xde Faker Extension

But here is the idea, it's not just about importing a module, we need to use k6 faker extension since k6 does not run on node so importing the extension will not work.

Here is a step to setup the extension. After you're done installing the extension, we will need to run the k6 binary generated from the build instead of the k6 installed on the machine.

The new way to run test now becomes

./k6 run tests/user.js

Create Generator Function

To use faker , I wil like to create a new folder called generator. Within this folder, I will add a file called user.js for user information generation. See the generate User function below.

import { faker } from "k6/x/faker";

export const generateUser = () => ({
  firstname: faker.firstName(),
  lastname: faker.lastName(),
  username: faker.username(),
  password: faker.password() + faker.password(),
  title: faker.jobTitle(),
  email: faker.email(),
  usertype: 1,
  date_of_birth: faker.date()
});

The helper methods can easily handle your user generation and you can use them in your test. Full test code is shown below.

import http from 'k6/http';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { generateUser } from './generator/user.js';
import { expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.1/index.js';
const baseURL = "https://carestationrest.herokuapp.com/api/v1/"

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

const headerinfo = {
  headers: {
    'Content-Type': 'application/json'
  },
};

export  default function  () {
  const user = generateUser()
  user.date_of_birth  = '1990-01-01'
  const signupParam = JSON.stringify(user)
  console.log(signupParam)

  const signupURL = baseURL + 'users'

  const res = http.post(signupURL, signupParam, headerinfo);
  console.log(res.status)
  expect(res.status, 'response status').to.equal(200);
  expect(res).to.have.validJsonBody();

}


export function handleSummary(data) {
  return {
    "summary.html": htmlReport(data),
  };
}


export function teardown(data) {
  console.log("teardown function started")
}

A few things to mention, we used the helper method to create a user object from faker which will return random data each time. I added a log just to show you the data is unique within the test.

Screen Shot 2022-06-05 at 10.04.06 PM.png

Here is a view of the report showing the checks we did with the [k6chaijs](k6.io/docs/javascript-api/jslib/k6chaijs extension.

Screen Shot 2022-06-05 at 9.34.58 PM.png