How To Use A Variable In Postman
Using Variables in Postman for API Testing
Variables are essential for streamlining your API testing workflow in Postman. They allow you to store and reuse values dynamically, making your tests more flexible and maintainable.
What are Variables?
Variables in Postman are placeholders that represent data within your requests, tests, and collections. They provide a way to dynamically change values in your test cases, which is useful for:
- Parameterizing requests: You can use variables to send different data in each request, such as user IDs, email addresses, or unique identifiers.
- Storing responses: You can store data extracted from API responses in variables for later use in subsequent requests or tests.
- Creating reusable test cases: Variables allow you to define test steps once and reuse them across multiple requests.
Types of Variables
Postman supports several types of variables:
- Environment variables: These variables are stored at the environment level and can be shared across multiple collections.
- Global variables: These variables are available across all environments and collections within a workspace.
- Collection variables: These variables are specific to a collection and can be accessed within any request or test within that collection.
- Data variables: These variables are used for data-driven testing, where you can define a set of data and use it to drive your requests and tests.
- Local variables: These variables are specific to individual requests or tests. They are created and used within a single scope and cannot be accessed outside that scope.
How to Create and Use Variables
Creating Environment Variables
- Navigate to the “Environments” tab: Click on the “Environments” tab in the Postman sidebar.
- Create a new environment: Click the “Add” button to create a new environment.
- Define your variable: Give your variable a name, set its initial value, and ensure the “Initial Value” field is set to “true”.
- Save your environment: Click “Save” to store your new environment.
Using Environment Variables
- Select the environment: Before sending a request, select the environment containing the variable you want to use.
- Reference your variable: In the request URL, headers, or body, use the syntax
{{variable_name}}
to reference your variable. For example:https://api.example.com/users/{{userId}}
.
Example
Consider testing a user registration API endpoint. You want to send different user details in each request. Here’s how you could use environment variables:
-
Create environment variables:
Variable Name Initial Value firstName
John
lastName
Doe
email
john.doe@example.com
-
Use the variables in the request body:
{"firstName": "{{firstName}}","lastName": "{{lastName}}","email": "{{email}}"}Now, you can change the environment variables to test different user registrations without modifying the request body.
Other Variable Types
Global Variables
- Navigate to the “Globals” tab
- Create a new variable as described above, only ensure the “Global” field is set to “true”.
Collection Variables
- Navigate to the collection where you want to create a variable.
- Click on the “Variables” tab within the collection.
- Create a new variable as described above.
Data Variables
-
Create a new data file (e.g.,
user_data.json
). -
Define your data: Store your data in the following format:
[{"firstName": "Alice","lastName": "Smith","email": "alice.smith@example.com"},{"firstName": "Bob","lastName": "Jones","email": "bob.jones@example.com"},// ... more user data] -
Add a data file to your collection: Go to the collection settings and add the data file.
-
Reference data variables: Use the
$
symbol within your requests to access data from the data file.{"firstName": "${firstName}","lastName": "${lastName}","email": "${email}"}
Local Variables
- Create a local variable in the Pre-request Script tab of a request.
- Reference the variable using the same syntax as other variables.
Tips for Efficient Variable Usage
- Use descriptive variable names: This improves readability and maintainability.
- Organize your variables: Group related variables together in environments or collections.
- Utilize data variables for data-driven testing to minimize code duplication and increase test coverage.
- Use environment variables for dynamic settings such as API URLs or authentication tokens.
Conclusion
Variables are powerful tools in Postman, enabling you to create flexible, maintainable, and data-driven API tests. By effectively incorporating variables into your test scripts, you can accelerate your testing process and improve the quality of your API development.