邮递员测试以在响应数组中查找关键字
Postman test to find a keyword in a response array
我正在邮递员中创建一个测试,以检查 JSON 中每个 "name" 字段中的关键字 "pregnancy"。如果 JSON 中的每个 'name' 字段都包含关键字,则通过测试,否则失败。
在下面找到我尝试使用的脚本
var jsonData = pm.response.json();
var resultCount = jsonData.length;
for (i=0;i<resultCount;i++){
var modelString = jsonData[i].name;
if(modelString.indexOf("Pregnancy") > 0)
{
tests["Each organisation name field " +i+ " contains \"Pregnancy\""] = modelString.has("Pregnancy");
}
else
{
pm.expect.fail("failed");
}
}
console.log("")
[
{
"id": "1-116992830",
"name": "British Pregnancy Advisory Service (BPAS)",
"numberOfLocations": 78
},
{
"id": "1-1560082724",
"name": "PAMS Pregnancy Ultrasound Centre (PAMS 3D & 4D Baby Imaging)",
"numberOfLocations": 1
},
{
"id": "1-2458518720",
"name": "Pregnancy Ultrasound Ltd (Babyface4d)",
"numberOfLocations": 1
},
{
"id": "1-101728376",
"name": "National Unplanned Pregnancy Advisory Service Limited",
"numberOfLocations": 23
},
{
"id": "1-3578030817",
"name": "Private Pregnancy Ultrasound Services Ltd trading as Expectancy Scanning Studios Ltd (Expectancy Scanning Studios Ltd)",
"numberOfLocations": 2
},
{
"id": "1-1412821832",
"name": "Foundation For Life (Salisbury) (Pregnancy Advice Salisbury)",
"numberOfLocations": 1
},
{
"id": "1-2028907839",
"name": "Miscarriage Clinic Limited (Centre for Reproductive Immunolgy and Pregnancy)",
"numberOfLocations": 1
},
{
"id": "1-744810951",
"name": "Foundation For Life (Tyneside Pregnancy Advice Centre)",
"numberOfLocations": 1
}
]
我期望的是,如果每个数组字段中缺少 'pregnancy',它应该无法通过测试
我将你的 JSON 添加到 https://api.myjson.com/bins/13qh7i 以创建一个 API 以在 Postman 下进行测试,并在数组中添加了一个名称不包含单词 "pregnancy" 以便您可以看到正在运行的测试。
如果您遍历并检查 objects
name
属性 中的单词 "pregnancy",并存储在 hasPregnancy
array
,然后您可以检查 array
的 length
与 API 响应 object array
的 length
是否每个 object
确实包含关键字"pregnancy"。
下面是我在测试中使用的JS代码。我还将您的 indexOf
更新为 includes
,因为您应该寻找 > -1 with indexOf
。我还使 modelString
变量在 name
上应用 toLowerCase()
以确保检查不关心大小写等
// Get response
var jsonData = pm.response.json();
var resultCount = jsonData.length;
// Test arrays
var hasPregnancy = [];
var doesntHavePregnancy = [];
// Loop through and set arrays with matching data
for (i = 0; i < resultCount; i++) {
var id = jsonData[i].id;
var modelString = jsonData[i].name.toLowerCase();
if (modelString.includes("pregnancy")) {
hasPregnancy.push({
"id": id,
"hasPregnancy": modelString.has("pregnancy")
});
} else {
doesntHavePregnancy.push({
"id": id
});
}
}
// Check that each object in response contained keyword and length matches from test
pm.test("Expect response to contain pregnancy in each object", function() {
console.log(hasPregnancy);
console.log(doesntHavePregnancy);
pm.expect(hasPregnancy.length).to.equal(resultCount);
});
使用 ES6 reduce 函数在迭代响应时累积每个项目的名称是否包含 "Pregnancy" 的结果。
const jsonData = pm.response.json();
tests[`Each organisation name field contains \"Pregnancy\"`] = jsonData
.reduce((acc, org) => acc && org.name.toLowerCase().includes("pregnancy"));
- 你会发现像 map 和 reduce 这样的 ES6 函数通常更具表现力
- 使用
inlcudes
比indexOf
更清楚你的意图
- 我添加了
toLowerCase()
所以匹配不区分大小写
我正在邮递员中创建一个测试,以检查 JSON 中每个 "name" 字段中的关键字 "pregnancy"。如果 JSON 中的每个 'name' 字段都包含关键字,则通过测试,否则失败。
在下面找到我尝试使用的脚本
var jsonData = pm.response.json();
var resultCount = jsonData.length;
for (i=0;i<resultCount;i++){
var modelString = jsonData[i].name;
if(modelString.indexOf("Pregnancy") > 0)
{
tests["Each organisation name field " +i+ " contains \"Pregnancy\""] = modelString.has("Pregnancy");
}
else
{
pm.expect.fail("failed");
}
}
console.log("")
[
{
"id": "1-116992830",
"name": "British Pregnancy Advisory Service (BPAS)",
"numberOfLocations": 78
},
{
"id": "1-1560082724",
"name": "PAMS Pregnancy Ultrasound Centre (PAMS 3D & 4D Baby Imaging)",
"numberOfLocations": 1
},
{
"id": "1-2458518720",
"name": "Pregnancy Ultrasound Ltd (Babyface4d)",
"numberOfLocations": 1
},
{
"id": "1-101728376",
"name": "National Unplanned Pregnancy Advisory Service Limited",
"numberOfLocations": 23
},
{
"id": "1-3578030817",
"name": "Private Pregnancy Ultrasound Services Ltd trading as Expectancy Scanning Studios Ltd (Expectancy Scanning Studios Ltd)",
"numberOfLocations": 2
},
{
"id": "1-1412821832",
"name": "Foundation For Life (Salisbury) (Pregnancy Advice Salisbury)",
"numberOfLocations": 1
},
{
"id": "1-2028907839",
"name": "Miscarriage Clinic Limited (Centre for Reproductive Immunolgy and Pregnancy)",
"numberOfLocations": 1
},
{
"id": "1-744810951",
"name": "Foundation For Life (Tyneside Pregnancy Advice Centre)",
"numberOfLocations": 1
}
]
我期望的是,如果每个数组字段中缺少 'pregnancy',它应该无法通过测试
我将你的 JSON 添加到 https://api.myjson.com/bins/13qh7i 以创建一个 API 以在 Postman 下进行测试,并在数组中添加了一个名称不包含单词 "pregnancy" 以便您可以看到正在运行的测试。
如果您遍历并检查 objects
name
属性 中的单词 "pregnancy",并存储在 hasPregnancy
array
,然后您可以检查 array
的 length
与 API 响应 object array
的 length
是否每个 object
确实包含关键字"pregnancy"。
下面是我在测试中使用的JS代码。我还将您的 indexOf
更新为 includes
,因为您应该寻找 > -1 with indexOf
。我还使 modelString
变量在 name
上应用 toLowerCase()
以确保检查不关心大小写等
// Get response
var jsonData = pm.response.json();
var resultCount = jsonData.length;
// Test arrays
var hasPregnancy = [];
var doesntHavePregnancy = [];
// Loop through and set arrays with matching data
for (i = 0; i < resultCount; i++) {
var id = jsonData[i].id;
var modelString = jsonData[i].name.toLowerCase();
if (modelString.includes("pregnancy")) {
hasPregnancy.push({
"id": id,
"hasPregnancy": modelString.has("pregnancy")
});
} else {
doesntHavePregnancy.push({
"id": id
});
}
}
// Check that each object in response contained keyword and length matches from test
pm.test("Expect response to contain pregnancy in each object", function() {
console.log(hasPregnancy);
console.log(doesntHavePregnancy);
pm.expect(hasPregnancy.length).to.equal(resultCount);
});
使用 ES6 reduce 函数在迭代响应时累积每个项目的名称是否包含 "Pregnancy" 的结果。
const jsonData = pm.response.json();
tests[`Each organisation name field contains \"Pregnancy\"`] = jsonData
.reduce((acc, org) => acc && org.name.toLowerCase().includes("pregnancy"));
- 你会发现像 map 和 reduce 这样的 ES6 函数通常更具表现力
- 使用
inlcudes
比indexOf
更清楚你的意图
- 我添加了
toLowerCase()
所以匹配不区分大小写