API Testing: A Practical Guide
What is an API?
- An API
(Application Programming Interface) is a software intermediary that
enables two applications to communicate with each other. It comprises a
number of subroutine definitions, logs, and tools for creating application
software.
- APIs carry
data from servers (database) to the client (user-interface), meaning you
can carry data from the backend and populate it to your front-end.
- APIs are a
collection of functions that are executable by other functions of the
software application.
API
vs. Web Service
- All web
services are APIs, but not all APIs are web services.
- Web services
might not contain all the specifications and cannot perform all the tasks
that APIs would perform.
- A Web
service uses only three styles of use: SOAP, REST and XML-RPC for
communication whereas an API may be exposed in multiple ways.
- Web services
require a network to operate, while APIs don’t need a network for
operation.
Why
is API Testing Important?
- API testing
verifies APIs that drive application logic.
- It validates
data flow between client and server.
- API Testing
ensures data accuracy.
- It improves
application performance.
- API testing
reduces bugs early.
- APIs are the
backbone of modern applications, making testing crucial.
- API testing
identifies issues before they reach the user.
- API
contracts are designed for this process, which helps how data is stored
(like JSON, XML, Form-Data etc.).
- Testers
don't require the UI to be finished so that testing can start, because
only the backend setup or API contracts are needed.
API
Testing Objectives
- Validate
that the API performs expected operations.
- Ensure the
API is secure from unauthorized access.
- Test API
performance under high usage.
- Verify
proper error codes and messages for failed requests.
API
Architectural Styles
- REST (Representational State Transfer): An architectural style for developing web services which exploit the ubiquity of the HTTP protocol and uses HTTP methods to define actions. REST revolves around resources where every component is a resource that can be accessed through a shared interface using standard HTTP methods. Key characteristics of REST include it being stateless, having a layered system, and cacheability.
- Other API
architecture styles include: SOAP (Simple Object Access Protocol), GraphQL, WebSocket, Webhook, RPC/gRPC, and
MQTT.
Common
REST API Methods
- GET: Retrieves
data from the server. Not necessarily requiring a request body. After
request completes response is received. Use GET requests for safe and
idempotent operations.
- POST: Sends data
to the server to create a new resource. Requires a Request body and
provides a response once the request is completed. POST is neither safe
nor idempotent, so be cautious about retrying failed POST requests.
- PUT: Updates an
existing resource or creates a resource if it does not exist. Requires
Request body and provides response once request completed. PUT should be
idempotent, meaning repeated requests with the same data should produce
the same result.
- DELETE: Removes a
resource from the server. It can work with & without request body. A
response is received once the request is completed. DELETE operations
should also be idempotent.
API
Testing Types
- Functional
Testing: Ensures APIs perform their intended functions.
- Performance
Testing: Tests response time, throughput, and scalability.
- Security
Testing: Validates authentication, authorization, and data protection.
- Contract
Testing: Checks API request-response structure against specifications.
- Negative
Testing: Tests API behavior with invalid inputs or scenarios.
- Other types:
UI testing, Runtime/ Error Detection, Penetration testing, Fuzz testing,
Interoperability and WS Compliance testing.
API
Testing Process
- Understand
Requirements:
Review API documentation to understand endpoints, request methods, and
response formats. Identify key functionalities to test.
- Test
Planning:
Define test objectives, tools, and scenarios. Prioritize critical API
functionalities for testing.
- Test Case
Design:
Write test cases for valid inputs, invalid inputs, boundary values, and performance
scenarios. Include assertions for status codes, response times, and data
validation.
- Test
Execution:
Run test cases using tools like Postman or automated scripts. Document
results and track issues for resolution.
API
Test Design Principles
- Setup:
Create objects, start services, initialize data, etc.
- Execution:
Steps to apply API or the scenario, including logging.
- Verification:
Oracles to evaluate the result of the execution.
- Reporting:
Pass, failed or blocked.
- Clean up:
Pre-test state.
API
Testing Checklist
- Validate
HTTP response codes (e.g., 200 OK, 404 Not Found, 500 Internal Server
Error).
- 1xx:
Informational Responses.
- 2xx:
Successful Codes.
- 200 OK:
The request was successful.
- 201
Created: The request has been fulfilled, and a new resource was created.
- 202
Accepted: The request has been accepted for processing, but the
processing has not been completed.
- 204 No
Content: The server successfully processed the request, but there is no
content to return.
- 3xx:
Redirection.
- 302 Found:
The resource is temporarily located at a different URL.
- 304 Not
Modified: The resource has not been modified since the last request, so
the client can use the cached version.
- 4xx: Client
Error Responses.
- 400 Bad
Request: The server could not understand the request due to invalid
syntax.
- 401
Unauthorized: Authentication is required, and the user is not
authenticated.
- 403
Forbidden: The request is valid, but the server refuses to authorize it.
- 404 Not
Found: The server cannot find the requested resource.
- 409
Conflict: The request could not be completed due to a conflict with the
current state of the resource.
- 429 Too
Many Requests: The user has sent too many requests in a given amount of
time.
- 5xx: Server
Error Responses.
- 503
Service Unavailable: The server is not ready to handle the request,
often due to being overloaded or down for maintenance.
- 504
Gateway Timeout: The server was acting as a gateway or proxy and did not
get a response in time from the upstream server.
- Test API
endpoints (GET, POST, PUT, and DELETE).
- Verify
JSON/XML schema consistency.
- Ensure data
accuracy matches database records.
- Test
authentication and authorization (e.g., JWT tokens).
- Perform
negative testing with invalid inputs and methods.
- Load-test
APIs for performance under traffic.
- Validate meaningful
error messages.
- Ensure
security with checks against SQL injection and XSS.
- Test timeout
and retry mechanisms for reliability.
- Verify that
the API request parameters are correctly passed to the API.
- Verify that
the API endpoint URL is correct.
- Verify that
the API response headers are correct.
- Verify that
the API returns an error message if the request payload is missing.
- Verify that
the API returns an error message if the requested resource does not exist.
- Verify that
the API returns a success message if the resource is created successfully.
- Verify that
the API returns a success message if the resource is updated successfully.
- Verify that
the API returns a success message if the resource is deleted successfully.
- Verify that
the API returns a success message if the resource is retrieved
successfully.
- Check for
proper access controls to prevent unauthorized access to sensitive
resources.
- Validate the
API's compliance with secure data transmission practices (e.g., HTTPS).
API
Testing Tools
- Postman: User-friendly
for manual and automated API tests.
- SoapUI: Ideal for
SOAP and REST API functional testing.
- RestAssured: Java-based
library for automating API tests.
- JMeter:
Performance testing for APIs under heavy load.
- Swagger
(OpenAPI):
Simplifies API documentation and testing.
- Other tools
mentioned include: Newman, Burp Suite, JIRA.
API
Testing Challenges
- Incomplete
or unclear API documentation.
- Handling
dynamic input parameters.
- Ensuring
security.
- Testing APIs
in isolation.
- Initial
setup of API testing.
- Updating the
schema of API testing.
- Testing
parameter combinations and sequencing the API calls.
- Validating
parameters.
- Test data
management.
API
Testing Best Practices
- Start with
simple functionality tests before diving into complex scenarios.
- Use tools to
automate repetitive tests and ensure consistency.
- Validate all
aspects, including headers, payloads, status codes, and response times.
- Test with
real-world data to ensure reliability.
- Include
negative and boundary testing scenarios.
- Ensure your test
cases cover all scenarios, including happy paths and edge cases.
- API tests
should not depend on each other to avoid false negatives.
API
Documentation
- API
documentation is a complete, accurate technical writing giving
instructions on how to effectively use and integrate with an API.
- It is a
compact reference manual that has all the information needed to work with
the API, and helps you answer all the API testing questions with details
on functions, classes, return types, arguments, and also examples and
tutorials.
- API
documentation templates that are commonly used are Swagger, Miredot,
Slate, FlatDoc, API blueprint, RestDoc, and Web service API specification.
Free
Public APIs for Testing
- JSONPlaceholder
API: A free online REST API for testing and prototyping.
- Base URL:
https://jsonplaceholder.typicode.com/
- Example
Endpoints:
- GET
https://jsonplaceholder.typicode.com/posts
- POST
https://jsonplaceholder.typicode.com/posts
- ReqRes: A
hosted REST API that simulates user management scenarios.
- Base URL:
https://reqres.in/
- Example
Endpoints:
- GET
https://reqres.in/api/users
- POST
https://reqres.in/api/users
- HTTP
Stat.us: A simple service to return different HTTP status codes for
testing.
- Base URL:
https://httpstat.us/
- Example
Endpoints:
- GET
https://httpstat.us/200
- GET
https://httpstat.us/404
- The Dog API:
A fun API that returns pictures and information about dogs.
- Base URL:
https://thedogapi.com/
- Example
Endpoints:
- GET
https://api.thedogapi.com/v1/breeds
- GET https://api.thedogapi.com/v1/images/search
Quick Summary: Key aspects of
API testing
The following is a concise summary of the essential elements of API testing, based on the information provided above.
- What is an
API? An API
(Application Programming Interface) enables two applications to
communicate. It carries data from servers to the user interface and is a
collection of functions executable by other software functions.
- API vs. Web
Service:
Every web service is an API, but not every API is a web service. Web
services require a network, while APIs can be on- or offline.
- Why is API
Testing Important?
API testing validates data flow, ensures accuracy, improves performance,
and reduces bugs. It's crucial because APIs are the backbone of modern
applications.
- API Testing
Objectives:
Validate performance, ensure security, test performance under high usage,
and verify error codes.
- API
Architectural Styles:
REST is a common style using HTTP methods. Other styles include SOAP, GraphQL,
WebSocket, RPC/gRPC, and MQTT.
- Common REST
API Methods:
- GET: Retrieves
data. Safe and idempotent.
- POST: Creates
new resources. Not safe or idempotent.
- PUT: Updates
existing resources. Should be idempotent.
- DELETE: Removes
resources. Should be idempotent.
- API Testing
Types:
Functional, performance, security, contract, and negative testing. Also
UI, runtime/error detection, penetration, fuzz, and interoperability
testing.
- API Testing
Process:
Understand requirements, plan tests, design test cases, and execute tests.
- API Test
Design Principles:
Setup, execution, verification, reporting, and clean up.
- API Testing
Checklist:
- Validate
HTTP response codes. (e.g., 200 OK, 404 Not Found, 500 Internal Server
Error).
- Test API
endpoints.
- Verify
JSON/XML schema.
- Ensure data
accuracy.
- Test
authentication and authorization.
- Perform
negative testing.
- Load-test
APIs.
- Validate
error messages.
- Ensure
security with checks against SQL injection and XSS.
- Test timeout
and retry mechanisms.
- API Testing
Tools:
Postman, SoapUI, RestAssured, JMeter, Swagger. Also Newman, Burp Suite,
and JIRA.
- API Testing
Challenges:
Incomplete documentation, dynamic parameters, security, isolation, setup,
schema updates, parameter combinations, test data management.
- API Testing
Best Practices:
Start simple, automate, validate all aspects, use real-world data, include
negative and boundary testing, and ensure independence of tests.
- API
Documentation:
Crucial for effective use and integration, including functions, classes,
and arguments. Use tools like Swagger, Miredot, and Slate.
- Free Public APIs for Testing: JSONPlaceholder, ReqRes, HTTP Stat.us, and The Dog API.
Comments