Introducing Dynamic Variables: Generate Realistic Test Data Instantly
Source: Dev.to
What are dynamic variables?
Dynamic variables are special placeholders that resolve to automatically generated values at runtime. They use a $ prefix and are accessed with this template syntax:
{{$variableName}}
Every time the request executes, the variable produces a new value.
Examples
{{$randomUUID}}→ Generates a new UUID{{$timestamp}}→ Current UNIX timestamp{{$randomInt}}→ Random integer{{$guid}}→ GUID‑style identifier
The key difference: you don’t define these. They’re built‑in and ready to use.
Why generating test data is a pain
Developers and QA teams repeatedly encounter:
- Reused emails or user IDs causing signup and uniqueness checks to fail.
- Manual editing of payloads for each run, which is slow and error‑prone.
- Tests breaking when data already exists in the database.
Dynamic variables eliminate these issues by producing fresh, realistic inputs for every execution.
Using Dynamic Variables in Templates
Dynamic variables can be placed anywhere inside a request: URL, query parameters, headers, or request body.
Example request body
{
"id": "{{$randomUUID}}",
"email": "{{$randomEmail}}",
"createdAt": "{{$timestamp}}"
}
Each execution generates a unique user profile.
Using Dynamic Variables in Scripts
Dynamic variables are also accessible inside scripts via the rq object.
const userId = rq.$randomUUID();
const timestamp = rq.$timestamp();
const email = rq.$randomEmail();
Notice they are called like functions in scripts.
Variable Arguments
Some dynamic variables support optional arguments to customize output.
Template syntax
{{$variableName arg1 arg2 …}}
Example with arguments
{
"id": "{{$randomAlphaNumeric 10}}",
"email": "{{$randomEmail 'John' 'Doe'}}",
"age": "{{$randomInt 18 65}}",
"price": "{{$randomPrice 10 100 2 '$'}}"
}
This lets you control:
- Length of random strings
- Name‑based email generation
- Integer ranges
- Price ranges and formatting
You get flexibility without writing extra logic.
Real‑world use cases
Dynamic variables shine in various workflows:
- Signup flows: Create unique users for each test run without database cleanup.
- OTP/timestamp tests: Generate valid, time‑based payloads for authentication flows.
- Load testing: Feed randomized inputs to surface edge‑case bugs.
- CI/CD pipelines: Run reliable, repeatable tests that don’t rely on pre‑seeded data.
- Mocking servers: Return realistic, variable responses for front‑end development.
These turn brittle tests into reliable checks and speed up development feedback loops.