0
0
Postmantesting~15 mins

Creating collections in Postman - Automation Script Walkthrough

Choose your learning style9 modes available
Create a new collection in Postman
Preconditions (2)
Step 1: Click on the 'New' button in the top left corner
Step 2: Select 'Collection' from the options
Step 3: Enter 'My API Tests' as the collection name
Step 4: Add a description 'Collection for API testing exercises'
Step 5: Click the 'Create' button
✅ Expected Result: A new collection named 'My API Tests' is created and visible in the Collections sidebar with the correct description
Automation Requirements - Postman API with Newman
Assertions Needed:
Verify the collection is created with the correct name
Verify the collection description matches the input
Best Practices:
Use Postman API to create collections programmatically
Validate response status codes and response body
Use environment variables for authentication tokens
Clean up created collections after test to avoid clutter
Automated Solution
Postman
import requests
import json

# Set your Postman API key here
API_KEY = 'YOUR_POSTMAN_API_KEY'

# Postman API endpoint for collections
url = 'https://api.getpostman.com/collections'

# Headers with API key
headers = {
    'X-Api-Key': API_KEY,
    'Content-Type': 'application/json'
}

# Collection data to create
collection_data = {
    "collection": {
        "info": {
            "name": "My API Tests",
            "description": "Collection for API testing exercises",
            "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
        },
        "item": []
    }
}

# Send POST request to create collection
response = requests.post(url, headers=headers, data=json.dumps(collection_data))

# Assert response status code is 200 (success)
assert response.status_code == 200, f"Expected status 200 but got {response.status_code}"

# Parse response JSON
response_json = response.json()

# Assert collection name and description in response
created_collection = response_json.get('collection', {})
assert created_collection.get('info', {}).get('name') == 'My API Tests', "Collection name mismatch"
assert created_collection.get('info', {}).get('description') == 'Collection for API testing exercises', "Collection description mismatch"

print('Collection created successfully with correct name and description.')

This script uses the Postman API to create a new collection programmatically.

First, it sets the API key for authentication in the headers.

The collection data includes the name and description as required.

It sends a POST request to the Postman collections endpoint.

After receiving the response, it checks the status code to ensure the request succeeded.

Then it verifies the collection name and description in the response match what was sent.

This confirms the collection was created correctly.

Using the API avoids manual UI steps and allows automation.

Common Mistakes - 3 Pitfalls
Hardcoding API key directly in the script without using environment variables
Not checking the response status code before parsing the response body
Ignoring cleanup of created collections after tests
Bonus Challenge

Now add data-driven testing to create three different collections with different names and descriptions

Show Hint