DEV Community

Applying API Testing Frameworks: Real-World Examples with Code

API testing has become a cornerstone of modern software quality assurance. With the rise of microservices, cloud-native architectures, and CI/CD pipelines, API tests must be automated, maintainable, and easy to integrate. This article explores how to apply popular API testing frameworks — with real-world examples you can use today.


Why API Testing Matters

Modern applications rely heavily on APIs to communicate between services, systems, or external integrations. Proper API testing ensures:

  • Consistency and reliability
  • Early detection of integration problems
  • Confidence in deployments
  • Better automation across CI/CD pipelines

Popular API Testing Frameworks

Below are the most widely used API testing frameworks in real-world environments.


1. Postman + Newman

Postman is widely used for manual and automated API tests, while Newman lets you run Postman collections in CI/CD.

Real-World Example: Testing a Login API

Postman Pre-request Script

pm.environment.set("username", "test_user");
pm.environment.set("password", "password123");
Enter fullscreen mode Exit fullscreen mode

Postman Test Script

pm.test("Login successful", function () {
    pm.expect(pm.response.code).to.equal(200);
    pm.expect(pm.response.json()).to.have.property("token");
});
Enter fullscreen mode Exit fullscreen mode

Running via Newman

newman run auth_tests.postman_collection.json -e dev_environment.json
Enter fullscreen mode Exit fullscreen mode

2. REST Assured (Java)

REST Assured is a powerful Java library commonly used in enterprise environments.

Real-World Example: Validate GET Endpoint

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class UserTests {
    @Test
    public void testGetUser() {
        given()
            .baseUri("https://api.example.com")
        .when()
            .get("/user/42")
        .then()
            .statusCode(200)
            .body("name", equalTo("John Doe"))
            .body("active", equalTo(true));
    }
}
Enter fullscreen mode Exit fullscreen mode

3. SuperTest (Node.js)

Perfect for JavaScript-based applications and backend teams using Node.js.

Real-World Example: Test User API

const request = require("supertest");
const app = require("../src/app");

describe("GET /users", () => {
    it("should return all users", async () => {
        const res = await request(app).get("/users");

        expect(res.statusCode).toBe(200);
        expect(res.body.length).toBeGreaterThan(0);
    });
});
Enter fullscreen mode Exit fullscreen mode

4. Pytest + Requests (Python)

Simple, clean, and extremely flexible. Ideal for automation and DevOps pipelines.

Real-World Example: Testing an Orders API

import requests

BASE_URL = "https://api.shop.com"

def test_create_order():
    payload = {
        "product_id": 1001,
        "quantity": 3
    }

    response = requests.post(f"{BASE_URL}/orders", json=payload)

    assert response.status_code == 201
    assert response.json()["status"] == "created"
Enter fullscreen mode Exit fullscreen mode

5. Karate Framework

Karate is popular for its simplicity and native support for API, UI, and performance testing.

Real-World Example: Testing a Payment API

Feature: Payment API

Scenario: Submit payment

  Given url "https://api.payments.com/pay"
  And request { "amount": 100, "currency": "USD" }
  When method post
  Then status 200
  And match response.message == "Payment processed"
Enter fullscreen mode Exit fullscreen mode

CI/CD Integration Best Practices

Tool Integration Method
Postman/Newman GitHub Actions, GitLab CI, Jenkins, Azure DevOps
REST Assured Maven/Gradle pipelines
SuperTest npm scripts + CI
Pytest Python runners (GitHub, GitLab, Jenkins)
Karate Maven runners

Example GitHub Actions workflow:

name: API Tests

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install dependencies
        run: npm install
      - name: Run API tests
        run: npm test
Enter fullscreen mode Exit fullscreen mode

Conclusion

API testing is crucial for ensuring reliable applications in modern DevOps ecosystems. Frameworks like REST Assured, SuperTest, Pytest, Karate, and Postman/Newman provide everything needed to create scalable and maintainable automated tests. By integrating these tools with CI/CD pipelines, organizations can ensure faster delivery and higher quality.


Top comments (0)