DEV Community

Mohaiminul Shovon
Mohaiminul Shovon

Posted on

Random Data Generation for API Test Automation Using Faker

Whenever we automation engineers try to create a API chaining requests, the most common obstacle and responses we get from the API is “Content Already Exists!”. Well our test has passed, but we need to move on from there. How? For the newbies, it’s a million dollar problem. Let’s take a scenario to understand the dilemma here.

We have an API in our hand which actually creates a user with some details such as gender, nationality, age, etc. We give a JSON body such as this to the API to process.

{
”name”: “Mr. X”, 
“age”: 25, 
“gender”: “male”, 
“nationality”: “Japanese”
}
Enter fullscreen mode Exit fullscreen mode

Now, the processes the body and send back a response, “New user created”. But we are in a automation journey. We can’t provide this same name and details every time our scripts run. Otherwise It will give back “Already created!”.

For this kind of test scenarios where we need to give dynamic data for each iteration. There are multiple ways to dynamically give data to request body. We can manually create some random data for this and store in a CSV file. For each iteration, we will pick from that CSV file. But this process is a manual process inside an automation process which we don’t want. There is another excellent way we can achieve our goal.

One excellent way to generate random data for API testing is by using the Faker library with TypeScript. Faker is a widely-used library that can generate fake data for various fields such as names, addresses, phone numbers, and more. Here's how you can use Faker with TypeScript to create dynamic data for your API tests.

Installing Faker

First, you need to install the Faker library. You can do this using npm:

npm install @faker-js/faker
Enter fullscreen mode Exit fullscreen mode

Using Faker in TypeScript

Once the installation is complete, you can use Faker to generate fake data in your TypeScript code. Below is an example of how you can use Faker to generate dynamic data for the user creation scenario:

import { faker } from '@faker-js/faker';

// Function to create a fake user
function createFakeUser() {
  const name = faker.name.findName();
  const age = faker.datatype.number({ min: 18, max: 100 });
  const gender = faker.name.gender(true); // Generates 'male' or 'female'
  const nationality = faker.address.country();

  return {
    name: name,
    age: age,
    gender: gender,
    nationality: nationality
  };
}

// Generate a new fake user
const fakeUser = createFakeUser();
console.log(fakeUser);

// Example of how you might use this fake user data in an API request
const requestBody = JSON.stringify(fakeUser);

// Now you can send this requestBody in your API request
// For example, using fetch or axios
// fetch('your-api-endpoint', { method: 'POST', body: requestBody });



Enter fullscreen mode Exit fullscreen mode

In this example, the createFakeUser function generates a new user object with a random name, age, gender, and nationality each time it is called. You can then use this object as the request body for your API tests, ensuring that each test run uses unique data and avoids the "Content Already Exists!" issue.

Top comments (0)