软件测试实验5
实验项目名称:实验五
一、 实验目的
1、了解什么是接口测试。
2、理解HTTP工作原理。
3、掌握接口测试用例设计。
4、掌握接口测试工具Postman的使用
二、 实验内容
对iHRM人力资源管理系统中的登录模块和员工管理模块进行接口测试,包括:
1.登录模块的接口测试。
2.员工管理模块的接口测试(员工管理列表、添加员工、查询员工)。
三、 实验步骤及结果
- 登录模块接口测试
1.1成功测试
请求信息:
请求方法:POST
请求地址:http://ihrm-java.itheima.net/api/sys/login
请求头:Content-Type: application/json
请求体参数:
{
"mobile": "13800000002",
"password": "929itheima.CN032@.20250429"
}
结果:
状态码:200
返回数据:
{
"success": true,
"code": 10000,
"message": "操作成功!",
"data": "0ff7a13d-2f4e-4fef-83ea-d1e686a55e76"
}
断言代码:
// 1. 断言HTTP状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 2. 断言响应体success字段为true
pm.test("Response success is true", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.success).to.eql(true);
});
// 3. 断言响应体code字段为10000(操作成功)
pm.test("Response code is 10000", function () {
pm.expect(pm.response.json().code).to.eql(10000);
});
// 4. 断言响应体message字段为"操作成功!"
pm.test("Response message is correct", function () {
pm.expect(pm.response.json().message).to.eql("操作成功!");
});
// 5. 断言data字段存在且为字符串(token)
pm.test("Token exists in response", function () {
pm.expect(pm.response.json().data).to.be.a('string');
});
// 6. 将返回的token存入环境变量(供后续接口使用)
pm.environment.set("token", "Bearer " + pm.response.json().data);
截图:
1.2 登录失败测试(用户名包含特殊字符)
请求信息:
请求方法:POST
请求地址:http://ihrm-java.itheima.net/api/sys/login
请求头:Content-Type: application/json
请求体参数:
{
"mobile": "1338@asd",
"password": "929itheima.CN032@.20250429"
}
结果:
状态码:200
返回数据:
{
"success": false,
"code": 20001,
"message": "用户名或密码错误",
"data": null
}
断言:
// 1. 断言HTTP状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 2. 断言success字段为false
pm.test("Response success is false", function () {
pm.expect(pm.response.json().success).to.eql(false);
});
// 3. 断言code字段为99999(用户名或密码错误)
pm.test("Error code is 99999", function () {
pm.expect(pm.response.json().code).to.eql(99999);
});
// 4. 断言message字段包含"用户名或密码错误"
pm.test("Error message is correct", function () {
pm.expect(pm.response.json().message).to.include("用户名或密码错误");
});
截图:
2. 员工管理模块接口测试
2.1 员工管理列表查询
请求信息:
请求方法:GET
请求地址:http://ihrm-java.itheima.net/api/sys/user?page=2&size=1
请求头:Content-Type: application/json
Authorization: 2a841d60-c748-4efd-a5d9-c6b94babd2b5
结果:
状态码:200
返回数据:
{"success":true,"code":10000,"message":"操作成功!","data":{"total":22,"rows":[{"id":"1066370498633486336","mobile":"12000000693","username":"孙财","password":"14af10ffa3798486632a79cbbf469376","enableState":1,"createTime":null,"companyId":"1","companyName":"传智播客","departmentId":"1175311466846683136","timeOfEntry":"2018-11-04T08:00:00.000+0000","formOfEmployment":1,"workNumber":"111","formOfManagement":null,"workingCity":null,"correctionTime":"2018-11-20T00:00:00.000+0000","inServiceStatus":1,"departmentName":"市场部","level":"user","staffPhoto":"http://pkbivgfrm.bkt.clouddn.com/1066370498633486336?t=1200000069318"}]}}
2.1 添加员工
请求信息:
请求方法:POST
请求地址:http://ihrm-java.itheima.net/api/sys/user?page=2&size=1
请求头:Content-Type: application/json
Authorization: 2a841d60-c748-4efd-a5d9-c6b94babd2b5
请求体参数
{
"username": "小明",
"mobile": "172123488881",
"workNumber": "220419",
"timeOfEntry": "2022-07-12",
"formOfEmployment": 1,
"departmentName": "柯克家",
"departmentId": "142424225735664777",
"correctionTime": "2022-12-12T16:08:00.0082"
}
结果:
状态码:200
返回数据:
{
"success": true,
"code": 10000,
"message": "操作成功!",
"data": null
}
断言:
// 1. 断言HTTP状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 2. 断言返回数据包含分页信息
pm.test("Response has pagination data", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data).to.have.property('total');
pm.expect(jsonData.data).to.have.property('rows');
});
// 3. 断言rows数组长度为1(因为size=1)
pm.test("Page size is correct", function () {
pm.expect(pm.response.json().data.rows.length).to.eql(1);
});
截图:
2.3 查询员工
请求信息:
请求方法:GET
请求地址:http://ihrm-java.itheima.net/api/sys/user/1066370498633486336
请求头:Authorization: 2a841d60-c748-4efd-a5d9-c6b94babd2b5
结果:
状态码:200
断言:
// 1. 断言HTTP状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 2. 断言返回数据包含必要字段
pm.test("Response has required fields", function () {
var jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('success');
pm.expect(jsonData).to.have.property('code');
pm.expect(jsonData).to.have.property('message');
pm.expect(jsonData).to.have.property('data');
});
// 3. 断言data对象包含必要字段
pm.test("Data object has required fields", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data).to.have.property('id');
pm.expect(jsonData.data).to.have.property('mobile');
pm.expect(jsonData.data).to.have.property('username');
pm.expect(jsonData.data).to.have.property('password');
pm.expect(jsonData.data).to.have.property('enableState');
pm.expect(jsonData.data).to.have.property('companyId');
pm.expect(jsonData.data).to.have.property('companyName');
pm.expect(jsonData.data).to.have.property('departmentId');
pm.expect(jsonData.data).to.have.property('timeOfEntry');
pm.expect(jsonData.data).to.have.property('formOfEmployment');
pm.expect(jsonData.data).to.have.property('workNumber');
pm.expect(jsonData.data).to.have.property('inServiceStatus');
pm.expect(jsonData.data).to.have.property('departmentName');
pm.expect(jsonData.data).to.have.property('roleIds');
pm.expect(jsonData.data).to.have.property('staffPhoto');
});
截图:
生成测试报告:
- 导出环境变量
- 导出集合
- 生成测试报告
四、 个人体会
通过本次实验,我深入理解了接口测试的流程和重要性,掌握了Postman工具的基本操作,包括请求发送、参数设置、断言编写等。同时,我学会了如何设计全面的测试用例,覆盖正常和异常场景。生成测试报告的过程也让我认识到自动化测试的高效性。未来,我将进一步学习接口自动化测试框架,提升测试效率和质量。