Small Real-World Implementation of API Automation
Introductionβ
A small real-world implementation provides a practical example of how to apply the concepts and techniques discussed in this documentation. By building a complete, end-to-end API automation framework for a sample application, you can consolidate your understanding and gain hands-on experience. In this section, weβll walk through a step-by-step implementation of an API automation framework using RestAssured, TestNG, and other tools.
1. Scenario Overviewβ
Letβs assume we are testing a simple User Management API with the following endpoints:
-
Create User:
POST /users- Request Body:
{ "name": "John", "email": "john@example.com" } - Response:
{ "id": 1, "name": "John", "email": "john@example.com" }
- Request Body:
-
Get User:
GET /users/{userId}- Response:
{ "id": 1, "name": "John", "email": "john@example.com" }
- Response:
-
Update User:
PUT /users/{userId}- Request Body:
{ "name": "John Updated" } - Response:
{ "id": 1, "name": "John Updated", "email": "john@example.com" }
- Request Body:
-
Delete User:
DELETE /users/{userId}- Response:
204 No Content
- Response:
2. Step-by-Step Implementationβ
Step 1: Set Up the Projectβ
-
Create a Maven Project:
- Use your IDE or command line to create a Maven project.
-
Add Dependencies:
<dependencies>
<!-- RestAssured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.0</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
</dependency>
<!-- Allure Reporting -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.21.0</version>
</dependency>
</dependencies>
- Configure TestNG:
<suite name="User Management API Tests">
<test name="User CRUD Tests">
<classes>
<class name="com.example.tests.UserTests"/>
</classes>
</test>
</suite>
Step 2: Write Base Test Setupβ
import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;
public class BaseTest {
@BeforeClass
public void setup() {
RestAssured.baseURI = "https://api.example.com";
RestAssured.requestSpecification = given()
.header("Content-Type", "application/json");
}
}
Step 3: Implement CRUD Testsβ
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.Test;
public class UserTests extends BaseTest {
private int userId;
@Test
public void testCreateUser() {
String requestBody = "{ "name": "John", "email": "john@example.com" }";
Response response = given()
.body(requestBody)
.when()
.post("/users");
response.then()
.statusCode(201);
userId = response.jsonPath().getInt("id");
Assert.assertNotNull(userId, "User ID should not be null");
}
@Test(dependsOnMethods = "testCreateUser")
public void testGetUser() {
given()
.pathParam("userId", userId)
.when()
.get("/users/{userId}")
.then()
.statusCode(200)
.body("name", equalTo("John"));
}
@Test(dependsOnMethods = "testGetUser")
public void testUpdateUser() {
String requestBody = "{ "name": "John Updated" }";
given()
.pathParam("userId", userId)
.body(requestBody)
.when()
.put("/users/{userId}")
.then()
.statusCode(200)
.body("name", equalTo("John Updated"));
}
@Test(dependsOnMethods = "testUpdateUser")
public void testDeleteUser() {
given()
.pathParam("userId", userId)
.when()
.delete("/users/{userId}")
.then()
.statusCode(204);
}
}
Step 4: Add Reportingβ
import io.qameta.allure.Description;
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
@Epic("API Testing")
@Feature("User Management")
public class UserTests extends BaseTest {
@Test
@Description("Verify user creation via POST /users")
public void testCreateUser() {
// Test logic here
}
}
Run:
mvn clean test
allure serve target/allure-results
Step 5: Integrate with CI/CDβ
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/api-tests.git'
}
}
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Report') {
steps {
allure includeProperties: false, results: [[path: 'target/allure-results']]
}
}
}
}
3. Best Practicesβ
- Follow Folder Structure
- Use POJOs for Requests/Responses
- Handle Data Cleanup
- Log Requests and Responses
- Monitor Trends
Conclusionβ
This small real-world implementation demonstrates how to build a complete API automation framework using RestAssured, TestNG, and reporting tools like Allure.