Designing a Unified API Testing Framework - Notes By ShariqSP
Designing a Unified API Testing Framework
The Unified API Testing Framework is designed to allow flexibility and scalability for testing multiple APIs. This framework will support different controllers and endpoints, ensuring that the architecture can easily adapt to future requirements.
Components of the Framework
- Generic Utility: Contains utility classes for operations that can be reused across the framework, such as logging, file handling, encryption, and test data manipulation.
- PojoUtility: Houses the POJOs that model the request and response objects of various API entities such as User, Event, Inventory, etc.
- BaseAPIClass: The base class for API tests, initializing common setup such as Base URL, authentication tokens, and headers for all requests.
- EndPoints: Centralized storage for all API endpoint URLs, which makes it easier to update or change endpoint values in one place.
- TestScripts: Test scripts that contain the actual test cases for testing various API endpoints. These test scripts can be written using JUnit, TestNG, or similar frameworks.
- Driver (TestNG.xml): This is the main entry point for executing the tests, where you define the test suite, groups, and parallel execution strategy.
- Env Config Data: Stores environment-specific data such as API URLs, database connections, and other environment-dependent configurations.
- Reports: Generates comprehensive test execution reports, usually using tools like ExtentReports or Allure to showcase the results of test runs.
- Resources: Contains external resources like test data, test input files (JSON, CSV), and any configuration files.
- POM.xml (Build File): The Maven build file, which includes dependencies and plugins necessary for the framework.
Framework Structure
src
├── main
│ ├── java
│ │ ├── base
│ │ │ └── BaseAPIClass.java
│ │ ├── endpoints
│ │ │ └── EndPoints.java
│ │ ├── pojo
│ │ │ ├── UserPojo.java
│ │ │ ├── EventPojo.java
│ │ │ ├── InventoryPojo.java
│ │ │ └── CardPojo.java
│ │ ├── utils
│ │ │ ├── FileUtility.java
│ │ │ ├── JSONUtility.java
│ │ │ ├── ExcelUtility.java
│ │ │ ├── RestClient.java
│ │ │ └── LoggerUtility.java
│ │ └── resources
│ │ └── config.yaml
│ └── test
│ ├── java
│ │ ├── testscripts
│ │ │ ├── UserTestScripts.java
│ │ │ ├── EventTestScripts.java
│ │ │ ├── InventoryTestScripts.java
│ │ │ ├── CompanyTestScripts.java
│ │ │ └── AdminTestScripts.java
│ │ ├── testng.xml
│ └── reports
│ └── test-report.html
pom.xml
Framework Diagram
Below is the high-level structure of how the unified framework is organized:
Env Config Data
Environment-specific configurations
BaseAPIClass
Initialize Base URL and headers
EndPoints
Centralized URL endpoints
PojoUtility
Request/Response Object Models
Generic Utility
Repetitive task utilities
Test Scripts
API Test Cases
Driver (TestNG.xml)
Test Execution
Reports
Test Execution Reports
Unified Framework Code Examples
BaseAPIClass.java
The base class initializes essential configuration for the API tests.
public class BaseAPIClass {
protected String baseUrl = "http://apitesting.shariqsp.com:8080";
protected RequestSpecification request;
@BeforeClass
public void setup() {
request = RestAssured.given().baseUri(baseUrl);
}
}
EndPoints.java
Centralized API endpoint management.
public class EndPoints {
public static final String REGISTER_USER = "/api/users/register";
public static final String UPDATE_USER = "/api/users/update/{id}";
public static final String LOGIN_USER = "/api/users/login";
public static final String UPDATE_EVENT = "/api/events/{id}/name";
}
PojoUtility
POJOs for User, Event, Inventory, etc. used for creating request/response models.
public class UserPojo {
private String username;
private String password;
// Getters and setters
}
Test Scripts (UserTestScripts.java)
TestNG script for testing the User API.
public class UserTestScripts extends BaseAPIClass {
@Test
public void testRegisterUser() {
UserPojo user = new UserPojo();
user.setUsername("testuser");
user.setPassword("password");
request.body(user)
.when().post(EndPoints.REGISTER_USER)
.then().statusCode(201);
}
}
Driver (TestNG.xml)
The entry point for executing the tests.
<suite name="UnifiedAPITests">
<test name="UserAPITests">
<classes>
<class name="testscripts.UserTestScripts"/>
</classes>
</test>
</suite>
Final Thoughts
This unified framework is flexible and scalable, making it easy to add or remove API endpoints as needed. Let me know if you have any questions or would like to dive deeper into any of the components!