为了实现这个班级管理系统的测试用例,我们可以使用 Python 测试框架,如pytest,以及 REST 客户端库 requests 来模拟客户端与服务器的交互。
- 定义客户端类
首先,我们需要定义一个 Client 类,用于封装 HTTP 请求操作:
import requests
class Client:
def __init__(self, url):
self.url = url
def post(self, path, json_data):
response = requests.post(f"{self.url}{path}", json=json_data)
return response.json()
def get(self, path):
response = requests.get(f"{self.url}{path}")
return response.json()
def put(self, path, json_data):
response = requests.put(f"{self.url}{path}", json=json_data)
return response.json()
def delete(self, path):
response = requests.delete(f"{self.url}{path}")
return response.json()
- 编写测试用例
接着,我们可以编写测试用例:
- 创建新班级:
def test_create_classroom(client): data = { "name": "class1"} response = client.post("/classroom", data) assert response["name"] == data["name"]
- 删除班级:
def test_delete_classroom(client): classroom_id = client.post("/classroom", { "name": "class2"})["id"] client.delete(f"/classroom/{classroom_id}") response = client.get(f"/classroom/{classroom_id}") assert response["code"] == 404
- 更新班级:
def test_update_classroom(client): classroom_id = client.post("/classroom", { "name": "class3"})["id"] updated_data = { "name": "new_name"} client.put(f"/classroom/{classroom_id}", updated_data) response = client.get(f"/classroom/{classroom_id}") assert response["name"] == updated_data["name"]
- 查询学生信息:
def test_query_students(client): students = client.get("/students") for student in students: student_id = student["id"] response = client.get(f"/student/{student_id}") assert response["name"] == f"student{student_id}"
- 添加学生:
def test_add_student(client): student_data = { "name": "student5", "age": 50, "score": 250} student_id = client.post("/student", student_data)["id"] response = client.get(f"/student/{student_id}") assert response["name"] == student_data["name"]
- 更新学生:
def test_update_student(client): student_data = { "name": "student6", "age": 60, "score": 300} student_id = client.post("/student", student_data)["id"] updated_data = { "name": "new_name"} client.put(f"/student/{student_id}", updated_data) response = client.get(f"/student/{student_id}") assert response["name"] == updated_data["name"]
- 删除学生:
def test_remove_student(client): student_data = { "name": "student7", "age": 70, "score": 350} student_id = client.post("/student", student_data)["id"] client.delete(f"/student/{student_id}") response = client.get(f"/student/{student_id}") assert response["code"] == 404
以上就是基本的测试用例,你可以根据你的业务需求来修改和完善这些测试用例。