0
0
Postmantesting~15 mins

Workspace organization in Postman - Build an Automation Script

Choose your learning style9 modes available
Create and organize collections in Postman workspace
Preconditions (2)
Step 1: Open Postman and navigate to the workspace dashboard
Step 2: Create a new workspace named 'API Testing Workspace'
Step 3: Within the workspace, create a new collection named 'User API Tests'
Step 4: Add a GET request to 'https://jsonplaceholder.typicode.com/users' in the collection
Step 5: Save the request with the name 'Get Users'
Step 6: Create another collection named 'Post API Tests'
Step 7: Add a POST request to 'https://jsonplaceholder.typicode.com/posts' with a JSON body in the collection
Step 8: Save the request with the name 'Create Post'
Step 9: Verify that both collections appear under the 'API Testing Workspace'
Step 10: Switch between collections and verify requests are organized correctly
✅ Expected Result: The workspace 'API Testing Workspace' contains two collections named 'User API Tests' and 'Post API Tests'. Each collection contains the correct saved requests. The workspace and collections are organized and accessible.
Automation Requirements - Postman Collection Runner with Newman
Assertions Needed:
Verify workspace creation success
Verify collections exist in the workspace
Verify requests are saved with correct names and URLs
Verify response status code 200 for GET request
Verify response status code 201 for POST request
Best Practices:
Use environment variables for URLs
Use descriptive names for collections and requests
Use Newman CLI for running collections
Use JSON schema validation for response body
Organize tests logically in collections
Automated Solution
Postman
import newman from 'newman';

// Define environment variables
const environment = {
  "id": "env1",
  "name": "Local Environment",
  "values": [
    { "key": "baseUrl", "value": "https://jsonplaceholder.typicode.com", "enabled": true }
  ]
};

// Define the Postman collection JSON
const collection = {
  "info": {
    "name": "API Testing Workspace",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "User API Tests",
      "item": [
        {
          "name": "Get Users",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/users"
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "pm.test('Status code is 200', function () {",
                  "    pm.response.to.have.status(200);",
                  "});"
                ]
              }
            }
          ]
        }
      ]
    },
    {
      "name": "Post API Tests",
      "item": [
        {
          "name": "Create Post",
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}"
            },
            "url": "{{baseUrl}}/posts"
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "pm.test('Status code is 201', function () {",
                  "    pm.response.to.have.status(201);",
                  "});"
                ]
              }
            }
          ]
        }
      ]
    }
  ]
};

// Run the collection using Newman
newman.run({
  collection: collection,
  environment: environment,
  reporters: 'cli'
}, function (err, summary) {
  if (err) { throw err; }
  console.log('Collection run complete!');
  // Check if all tests passed
  const failures = summary.run.failures.length;
  if (failures === 0) {
    console.log('All tests passed successfully.');
  } else {
    console.log(`${failures} test(s) failed.`);
  }
});

This script uses Newman, the command-line runner for Postman collections.

We define an environment with a base URL to keep URLs flexible.

The collection JSON defines two collections inside one workspace: 'User API Tests' and 'Post API Tests'. Each has one request with tests checking the HTTP status codes.

Newman runs the collection and outputs results in the console.

This approach follows best practices: environment variables, descriptive names, and test assertions for response codes.

Common Mistakes - 4 Pitfalls
Hardcoding URLs directly in requests without environment variables
Not adding assertions to verify response status codes
Mixing multiple unrelated tests in one collection without organization
Running Newman without specifying environment or collection properly
Bonus Challenge

Now add data-driven testing with 3 different POST request bodies for 'Create Post'

Show Hint