Study Material: API and API Testing
What is an API?
An Application Programming Interface (API) is a set of rules that allow different software
applications to communicate with each other. APIs define the methods and data formats that
applications can use to request and exchange information.
Key Concepts:
● Types of APIs: Web APIs, Library APIs, Operating System APIs, etc.
● RESTful APIs: APIs that follow REST (Representational State Transfer) architecture,
using standard HTTP methods.
● SOAP APIs: Use XML-based messaging for communication.
● GraphQL APIs: Allow fetching specific data instead of entire resources.
What is API Testing?
API testing is the process of verifying that an API meets functional, performance, security, and
reliability expectations. It involves sending requests to an API and validating the responses.
Types of API Testing:
● Functional Testing: Ensures API functions work as expected.
● Performance Testing: Checks API speed and reliability under load.
● Security Testing: Ensures APIs are protected against vulnerabilities.
● Integration Testing: Ensures APIs communicate properly with other services.
● Negative Testing: Tests invalid inputs to check API robustness.
HTTP Methods (GET, POST, PUT, DELETE)
APIs use HTTP methods to define actions that can be performed on resources.
● GET: Retrieve data from a server. (e.g., fetching user details)
● POST: Send data to the server to create a new resource. (e.g., creating a new user)
● PUT: Update an existing resource. (e.g., updating user details)
● DELETE: Remove a resource from the server. (e.g., deleting a user)
Using Postman for API Testing
Postman is a popular tool for testing APIs. It allows sending HTTP requests and analyzing
responses.
Steps to Use Postman:
1. Install Postman from https://www.postman.com/
2. Create a New Request by selecting the HTTP method (GET, POST, etc.)
3. Enter the API Endpoint
4. Add Headers and Parameters if required
5. Send the Request and analyze the response
6. Use Tests to validate responses with JavaScript assertions
API Response Status Codes
Status codes indicate the outcome of an API request.
● 200 OK: Request was successful
● 400 Bad Request: Invalid input or request format
● 401 Unauthorized: Authentication failed
● 403 Forbidden: User does not have permission
● 404 Not Found: Requested resource does not exist
● 500 Internal Server Error: Generic server failure
Writing Test Cases for APIs
Key Elements of API Test Cases:
1. Test Case ID: Unique identifier for each test case
2. Test Scenario: Description of what is being tested
3. Preconditions: Any required setup before testing
4. Request Details: API endpoint, method, headers, parameters
5. Expected Response: Expected status code and response body
6. Actual Response: Captured response details
7. Pass/Fail Criteria: Whether the test passed based on expected vs. actual output
Example API Test Cases:
Test Case 1: User Login API
Test Case ID TC_001
Test Scenario Verify user login API
Method POST
Endpoint /api/login
Request Body { "username": "test", "password":
"pass123" }
Expected 200 OK, JSON with auth token
Response
Actual Response 200 OK, JSON with auth token
Pass/Fail Pass
Test Case 2: Fetch User Profile
Test Case ID TC_002
Test Scenario Verify user profile retrieval API
Method GET
Endpoint /api/user/profile
Headers Authorization: Bearer
token123
Expected 200 OK, JSON with user details
Response
Actual Response 200 OK, JSON with user details
Pass/Fail Pass
Test Case 3: Update User Email
Test Case ID TC_003
Test Scenario Verify email update API
Method PUT
Endpoint /api/user/email
Request Body { "email":
"
[email protected]" }
Expected 200 OK, JSON confirmation
Response
Actual Response 200 OK, JSON confirmation
Pass/Fail Pass
Test Case 4: Delete User Account
Test Case ID TC_004
Test Scenario Verify user account deletion API
Method DELETE
Endpoint /api/user/delete
Headers Authorization: Bearer
token123
Expected 204 No Content
Response
Actual Response 204 No Content
Pass/Fail Pass