Skip to content
Learn with RV – Tech Blog
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

  • Home
  • Who am I?
  • 1-on-1 Mentoring
  • 18 Testimonials
  • YouTube
  • LinkedIn
  • Contact
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

k6-random-data
March 1, 2024September 27, 2024

k6 – How to generate Random Data

In this article I will show you how easy it is to generate random data in your k6 load test scripts.

As a brief context, in the real projects, there are moments when an API endpoint requires unique values on its request body because there might be some unique value constraints in the database, and so on.

Let’s think about a user registration process, where we cannot have multiple users registered with the same email or username.

For this use case, we will implement some random data in our test. I will be using k6 Test API application, that contains a POST endpoint for user registration. As you can see, the request body implies unique values for username and email properties.

k6-test-api-post-endpoint

Happily, k6 provides utility functions that we can use out of the box to generate random numbers between a given interval, random strings, items, and so on – docs HERE

Code Example

For demo purpose, we are going to use randomString function that accepts length value as argument

1. import randomString function into your script
import { randomString } from ‘https://jslib.k6.io/k6-utils/1.2.0/index.js’;

2. define a constant and initialize it with randomString(length)
const randomEmail = randomString(15) + “@gmail.com”;
const randomUsername = randomString(10);

Source code

You can find the source code HERE

Enjoyed this article? Make sure to subscribe to my YouTube Channel for more Test Automation tutorials, and follow me on LinkedIn and Twitter for regular insights.
Looking to accelerate your test automation journey?
I’ve designed a personalized 1-on-1 mentoring program tailored to boost your skills. Reach out at iamqarv@gmail.com for more details 🚀

Post Views: 397

Related

Share this article:
QA Tools

Post navigation

Previous post
Next post

Recent Posts

  • Using cross-env in Your Node.js Projects: A Practical Guide
  • How to copy-paste text from clipboard using TestCafe
  • How to copy-paste text from clipboard using Cypress
  • How to repeat any CLI Command for X times using NPM repeat-cli-command
  • k9s: A Guide to Master Kubernetes Clusters from your Terminal

Recent Comments

  1. Paul on Web Accessibility: A step-by-step guide to Testing with pa11y
  2. Automated Tests for website Accessibility with Axe and TestCafe - Learn with RV - Tech Blog on How to generate E2E TestCafe Framework in seconds
  3. RV on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  4. Adrian Maciuc on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  5. Nick on Cypress vs Playwright vs Testcafe – which framework is faster?

Archives

  • June 2025
  • March 2025
  • February 2025
  • January 2025
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Categories

  • Linux
  • Programming
  • QA
  • Tools
  • Uncategorized
©2025 Learn with RV – Tech Blog | WordPress Theme by SuperbThemes