Skip to content

How To Use Collection In Postman

API Testing Blog

How to Use Collections in Postman for API Testing

Collections in Postman are powerful tools for organizing, running, and managing API tests. They allow you to group related requests together, share them with your team, and build efficient workflows for automated testing.

Understanding Postman Collections

A Postman collection is a container for a group of API requests. These requests can be simple GET, POST, PUT, DELETE requests or more complex scenarios with data-driven testing, environment variables, and scripts.

Think of Collections as folders for your API tests, allowing you to:

  • Organize & Structure: Group related API calls into logical units.
  • Collaborate: Share collections with your team for better communication and workflow.
  • Automate Testing: Use Collections to run automated tests and monitor API health.

Creating Your First Collection

  1. Open Postman: Begin by opening the Postman application.
  2. Create a New Collection: Click on the “New” button at the top left corner and select “Collection”.
  3. Name Your Collection: Give your collection a meaningful name (e.g., “Weather API Tests”).

Example: Let’s create a collection for testing a weather API.

  • Collection Name: “Weather API Tests”

Adding Requests to Your Collection

  1. Create a Request: In your new collection, click the “Add Request” button.
  2. Configure Request:
    • HTTP Method: Select the HTTP method for your request (e.g., GET, POST, PUT, DELETE).
    • URL: Enter the complete API endpoint URL.
    • Headers: Define any necessary headers for the request (example: “Content-Type: application/json”).
    • Body (Optional): Provide any required data in the body of your request (JSON, XML, etc.)

Example:

  • Request Name: “Get Current Weather”
  • HTTP Method: GET
  • URL: https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY
  • Headers: Content-Type: application/json

Running Your Requests

  1. Send the Request: Click the “Send” button to execute your API request.
  2. Inspect the Response: The response from the API will be displayed.
    • Status Code: Check the HTTP status code (e.g., 200 for Success).
    • Headers: Review the headers in the response.
    • Body: Analyze the response data.

Example:

  • Response: You’ll see the current weather data for London in JSON format.

Adding Tests to Validate Responses

  1. Add a Test: Click the “Tests” tab in the request.
  2. Write Tests: Use JavaScript syntax to write assertions that check the desired conditions in the response.
    • Status Code: pm.test("Status code is 200", () => { pm.response.to.have.status(200) });
    • Response Content: pm.test("Response body contains weather data", () => { pm.expect(pm.response.text()).to.include('main') });

Example:

  • Test:
pm.test("Status code is 200", () => {
pm.response.to.have.status(200);
});
pm.test("Response body contains weather data", () => {
pm.expect(pm.response.text()).to.include('main');
});

Creating Collection Folders

  1. Create a Folder: Within your collection, click the “Add Folder” button.
  2. Organize Requests: Drag and drop requests into folders to categorize them.

Example:

  • Folder Name: “Weather Forecasts”
  • Requests: “Get 5-day Forecast”, “Get Hourly Forecast”.

Using Environment Variables

  1. Define Environments: Go to “Environments” in Postman and create a new environment (e.g., “Staging” or “Production”).
  2. Add Variables: Define environment variables with their names and values (e.g., API_KEY).
  3. Use Variables in Requests: Use ${key} syntax in your requests to reference environment variables.

Example:

  • Environment Variable: API_KEY with value YOUR_API_KEY
  • Request URL: https://api.openweathermap.org/data/2.5/weather?q=London&appid=${API_KEY}

Running Collections

  1. Select Collection: Choose the collection you want to run.
  2. Set Environment: Select the appropriate environment for your tests.
  3. Run: Click the “Run” button.

Example: Complete Weather API Collection

[
{
"name": "Weather API Tests",
"id": "YOUR_COLLECTION_ID",
"description": "Basic weather API test collection using OpenWeatherMap.",
"item": [
{
"name": "Get Current Weather",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": "https://api.openweathermap.org/data/2.5/weather?q=London&appid={{API_KEY}}"
},
"response": [],
"tests": [
"pm.test(\"Status code is 200\", () => { pm.response.to.have.status(200); });",
"pm.test(\"Response body contains weather data\", () => { pm.expect(pm.response.text()).to.include('main'); });"
]
},
{
"name": "Get 5-day Forecast",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": "https://api.openweathermap.org/data/2.5/forecast?q=London&appid={{API_KEY}}"
},
"response": [],
"tests": [
"pm.test(\"Status code is 200\", () => { pm.response.to.have.status(200); });",
"pm.test(\"Response body contains forecast data\", () => { pm.expect(pm.response.text()).to.include('list'); });"
]
}
]
}
]

Sharing Collections

  1. Export Collection: Export your collection in JSON format.
  2. Share: Share the JSON file with your team or use Postman’s built-in sharing features.

Conclusion

Collections are essential for efficient API testing in Postman. They provide a structured approach for organizing requests, automating testing, and collaborating with your team. By mastering the use of collections, you can streamline your API testing workflow and ensure the quality of your APIs.

API Testing Blog