接口测试出现Assertion Error,如何修复使测试用例通过?
问题分析与解决
数据文件(data.json)
[ { "id": "trololo_id", "email": "nasir1985@hotmail.com", "roles": ["admin", "member"], "apiKey": ["c0ca4bf0-687a-4eed-a96d-4589bb0f7347"], "profile": { "dob": "1992-09-10", "name": "Rachel Sheppard", "about": "There are no rules of architecture for a castle in the clouds.;Gilbert K. Chesterton;imagination", "address": "11 Seagate", "company": "Verbus", "location": { "lat": -79.066, "lon": -85.69855 } }, "username": "rachel92", "createdAt": "2011-01-26T22:11:43.564Z", "updatedAt": "2011-01-26T22:11:43.564Z" } ]
请求信息
- 方法:POST
- URL:https://reqres.in/api/users
- 请求体:
{ "email": "{{email}}", "roles": ["{{roles}}"], "profile": { "name": "{{name}}", "address": "{{address}}", "company": "{{company}}" } }
前置脚本
pm.collectionVariables.set("name", data["profile"]["name"]); pm.collectionVariables.set("address", data["profile"]["address"]); pm.collectionVariables.set("company", data["profile"]["company"]);
测试脚本
pm.test("Status code is 201", function () { pm.response.to.have.status(201); }); pm.test("Verify data", function () { var jsonData = pm.response.json(); pm.expect(jsonData.email).to.eql(data['email']); pm.expect(jsonData.profile).to.eql(data['profile']); pm.expect(jsonData.profile.address).to.eql(data['profile']['address']); pm.expect(jsonData.profile.company).to.eql(data['profile']['company']); });
响应结果
{ "email": "nasir1985@hotmail.com", "roles": [ "admin,member" ], "profile": { "name": "Rachel Sheppard", "address": "11 Seagate", "company": "Verbus" }, "id": "209", "createdAt": "2023-09-17T09:23:11.682Z" }
断言错误原因
- Profile全字段匹配失败:测试脚本里
pm.expect(jsonData.profile).to.eql(data['profile'])要求响应的profile和data中的profile完全相等,但data的profile包含dob、about、location等额外字段,而接口响应仅返回了请求中提交的name、address、company三个字段,结构不一致直接触发断言错误。 - Roles变量处理异常(潜在问题):请求体中
roles写成["{{roles}}"],data里的roles是数组["admin", "member"],变量替换时会把数组转为逗号分隔的字符串"admin,member"再放入数组,最终请求的roles是["admin,member"],而非原数组结构。 - 前置脚本变量缺失:请求体使用了
{{email}}和{{roles}}变量,但前置脚本仅设置了name、address、company,虽email值在响应中正确,但roles的变量处理错误导致请求值不符合预期。
修改方案
1. 修复测试脚本的核心断言问题
移除profile全字段匹配的断言,只验证响应中实际存在的字段:
pm.test("Verify data", function () { var jsonData = pm.response.json(); pm.expect(jsonData.email).to.eql(data['email']); // 仅断言响应返回的profile字段 pm.expect(jsonData.profile.name).to.eql(data['profile']['name']); pm.expect(jsonData.profile.address).to.eql(data['profile']['address']); pm.expect(jsonData.profile.company).to.eql(data['profile']['company']); });
2. 修正Roles的请求与变量设置(可选,若需正确传递数组)
- 更新前置脚本,添加缺失的变量并正确处理数组:
pm.collectionVariables.set("name", data["profile"]["name"]); pm.collectionVariables.set("address", data["profile"]["address"]); pm.collectionVariables.set("company", data["profile"]["company"]); pm.collectionVariables.set("email", data["email"]); // 将数组转为JSON字符串,避免变量替换时被转成字符串 pm.collectionVariables.set("roles", JSON.stringify(data["roles"]));
- 修改请求体的roles写法,直接使用变量作为数组:
{ "email": "{{email}}", "roles": {{roles}}, "profile": { "name": "{{name}}", "address": "{{address}}", "company": "{{company}}" } }
若需验证roles,可在测试脚本中添加:
pm.expect(jsonData.roles).to.eql(data['roles']);
内容的提问来源于stack exchange,提问作者Kifsif
相关产品推荐
相关产品推荐

