0% found this document useful (0 votes)
0 views4 pages

Devops Lab Task11

The document outlines the process of developing automated test cases for APIs, emphasizing the importance of API testing in validating business logic and error handling. It details the need for automation, common testing frameworks, and provides examples in Python and Java for writing test cases. Additionally, it includes instructions for a lab exercise, prerequisites, and expected deliverables for students to demonstrate their understanding of automated API testing.

Uploaded by

sathwickram4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Devops Lab Task11

The document outlines the process of developing automated test cases for APIs, emphasizing the importance of API testing in validating business logic and error handling. It details the need for automation, common testing frameworks, and provides examples in Python and Java for writing test cases. Additionally, it includes instructions for a lab exercise, prerequisites, and expected deliverables for students to demonstrate their understanding of automated API testing.

Uploaded by

sathwickram4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab Task 11 – Develop Test Cases for

Simple APIs in the Language of Choice

Concept Notes
1. What is API Testing?

API Testing validates the business logic, data responses, and error handling of an
application’s backend.
Unlike UI testing, it directly interacts with the server endpoints using HTTP methods such
as GET, POST, PUT, DELETE, PATCH.

A typical API test checks:

 Correct response codes (200, 201, 404 etc.)


 Accurate response data
 Proper request/response headers
 Performance (latency and throughput)

2. Need for Automated API Testing

Manual testing (Postman) is useful for exploration, but automation ensures:

 Faster feedback during CI/CD pipelines


 Repeated testing after every build
 Early bug detection before deployment

Automation is usually done using JUnit (Java), PyTest (Python), or Mocha/Chai


([Link]).

3. Test Case Design for APIs

Each API endpoint should have tests for:

Type Purpose Example


Positive Test Valid input returns correct output GET /api/users → 200 OK
Negative Test Invalid input handled gracefully GET /api/users/999 → 404
Validation Test Input format validation POST /api/login without password
Performance Test Response time check Response < 2 s
Security Test Auth token verification Access denied without token
4. Common Testing Frameworks

Language Framework Library/Tool


Java JUnit / TestNG RestAssured
Python PyTest Requests
JavaScript Mocha / Chai Supertest / Axios

5. Sample Test Case Structure

1. Setup: Define base URL and endpoints.


2. Action: Send HTTP request.
3. Assertion: Check response status and body.
4. Teardown: Close connections / cleanup if required.

Lab Exercise
Learning Objectives

 Write automated test cases for APIs using a language of choice.


 Validate HTTP responses, status codes, and payloads programmatically.
 Integrate automated API tests into CI/CD pipelines.

Prerequisites

 Basic knowledge of Python, Java, or JavaScript.


 Access to a working API endpoint (from Task 1 or Task 10).
 Postman or local server running for API testing.
 IDE (Pycharm, VS Code, Eclipse etc.) installed.

Instructions

Example in Python (PyTest + Requests)

1. Install dependencies:
2. pip install pytest requests
3. Create file: test_api.py
4. import requests
5.
6. BASE_URL = "[Link]
7.
8. def test_get_users():
9. response = [Link](f"{BASE_URL}/users")
10. assert response.status_code == 200
11. assert len([Link]()) > 0
12.
13. def test_create_post():
14. data = {"title": "API Test", "body": "Testing API via PyTest",
"userId": 1}
15. response = [Link](f"{BASE_URL}/posts", json=data)
16. assert response.status_code == 201
17. assert [Link]()["title"] == "API Test"
18. Run tests:
19. pytest -v

Example in Java (JUnit + RestAssured)

import [Link];
import [Link];
import static [Link].*;
import static [Link].*;

public class APITest {


@Test
public void testGetUsers() {
[Link]("[Link]
.then()
.statusCode(200)
.body("size()", greaterThan(0));
}
}

Compile and run tests using Maven or Gradle.

Assignment / Deliverables

Students must submit:

1. Source code for their API test cases (PyTest/JUnit/Mocha).


2. Screenshot of successful test execution (summary showing passed tests).
3. Short note (150 words) on how automated API testing helps ensure continuous
quality.

Expected Output

 Test cases run successfully and validate HTTP responses.


 Students understand assertion logic and test structure.
 Demonstrate automated testing of REST APIs.

🎥 Recommended Video Lectures (≤ 15 minutes)


For Python (Python Requests + PyTest)

Video: API Testing with Python and PyTest in 10 Minutes (~10 min)
Moodle Intro Text:

Watch this 10-minute tutorial to see how API tests are written and executed using PyTest.
Learn assertions, status code validation, and response data checks.
After watching, implement two API tests on your own endpoint and run them locally.

For Java (RestAssured + JUnit)

Video: REST API Automation Testing using RestAssured & JUnit (12 Minutes) (~12 min)
Moodle Intro Text:

This video demonstrates writing JUnit test cases using RestAssured for REST APIs.
Observe how to validate status codes, responses, and JSON paths programmatically.
Recreate the example and share your results with screenshots.

You might also like