Unlocking Efficient API Testing with Postman and Automation

Why Automate Your API Tests?

Writing tests helps ensure the predictability and correctness of your applications. By automating these tests, you can run them as often as your codebase changes, giving you the confidence to make changes and add new features without introducing bugs.

Setting Up Postman for API Testing

Postman is a comprehensive platform for building, testing, documenting, and mocking APIs. To get started, create a free account and set up a new collection for your API tests. We’ll use a delivery API as an example, creating a collection called “Delivery-API Tests” with four requests:

  • Search Deliveries
  • Select Delivery
  • Confirm Pickup
  • Complete Delivery

Writing Tests in Postman

Postman’s built-in testing tool allows you to write tests using JavaScript syntax. We’ll write tests for each request, checking for correct status codes, data types, and required fields. For example, in the Search Deliveries request, we’ll test that the response returns a JSON object with the correct status code and data type.

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});

pm.test("Response is JSON", function () {
  pm.response.to.be.json;
});

pm.test("Response has required fields", function () {
  var jsonData = pm.response.json();
  pm.expect(jsonData).to.have.property("deliveries");
});

Running Manual Tests on Postman

To run our tests, we’ll need to create a backend server to handle the API requests. We’ll build a simple Node.js server using Koa, defining route handlers that correspond to our requests.

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  if (ctx.url === '/searchDeliveries') {
    ctx.body = { deliveries: [...] };
  } else if (ctx.url === '/selectDelivery') {
    ctx.body = { deliveryId: 1 };
  } else if (ctx.url === '/confirmPickup') {
    ctx.body = { pickupConfirmed: true };
  } else if (ctx.url === '/completeDelivery') {
    ctx.body = { deliveryCompleted: true };
  }
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

Once our server is running, we can run our tests manually using Postman’s collection runner or by sending individual requests.

Automating API Tests with Newman and GitHub Actions

To automate our testing process, we’ll use Postman’s command-line tool, Newman, which integrates seamlessly with CI/CD environments. We’ll set up a GitHub Actions workflow that runs our tests whenever we push changes to our repository.

Integrating Newman with GitHub Actions

In our GitHub Actions workflow, we’ll define a job that runs our Postman tests using Newman.

name: API Tests
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install Newman
        run: npm install -g newman
      - name: Run Postman tests
        run: newman run Delivery-API-Tests.postman_collection.json --reporters=html
      - name: Upload report
        uses: actions/upload-artifact@v2
        with:
          name: report
          path: newman-html-report.html

Bringing It All Together

Finally, we’ll push our project to GitHub and see our workflow run. We’ll initialize Git in our project, add our remote URL, commit our changes, and push them to GitHub. Our workflow will run automatically, testing our API and generating a report.

Limitations of Postman for API Testing

While Postman is a powerful tool for API testing, it has its limitations. For complex tests that depend on external services, automation may be challenging. Additionally, sharing and reusing code between tests can be difficult.

By following these steps and understanding the limitations of Postman, you can unlock efficient API testing and ensure the reliability of your applications. Happy coding!

Leave a Reply