When I test some APIs, I sometimes need fake name, phone number, emails, and so on.
On Postman, you can use random name, phone numbers, uuids easily. Just type predefined {{$variable_name}}
where you want use them, then Postman automatically set random data.
Dynamic variables that I use frequently
I use below variables especially for API testing.
Variables | Description | Example |
---|---|---|
{{$guid}} | UUID version 4 | 611c2e81-2ccb-42d8-9ddc-2d0bfa65c1b4 |
{{$timestamp}} | UNIX timetamp | 1562757107 |
{{$isoTimestamp}} | ISO format timestamp | 2020-06-09T21:10:36.177Z |
{{$randomIP}} | IPv4 address | 216.7.27.38 |
{{$randomUserAgent}} | User Agent | Mozilla/5.0 (Macintosh… |
{{$randomFirstName}} | First Name | Ethan |
{{$randomLastName}} | Last Name | Willms |
{{$randomFullName}} | Full Name | Connie Runolfsdottir |
{{$randomPhoneNumber}} | 10-digit phone number | 700-008-5275 |
{{$randomPhoneNumberExt}} | 12-digit phone number | 27-199-983-3864 |
{{$randomExampleEmail}} | Email address | Talon28@example.com |
Use dynamic variables in Pre-request / Tests scripts
If you want use dynamic variables in Pre-request script or Tests scripts, you should use pm.variables.replaceIn()
.
// Example for using the random eamil variable on script
var email = pm.variables.replaceIn('{{$randomExampleEmail}}');
console.log(email);
References
- Offical document about dynamic variables: Dynamic variables
- The library used for dynamic variables: faker.js