gpt4 book ai didi

javascript - postman 测试以在响应数组中查找关键字

转载 作者:行者123 更新时间:2023-11-29 17:36:22 24 4
gpt4 key购买 nike

我正在 postman 中创建一个测试,以检查 JSON 中每个“名称”字段中的关键字“怀孕”。如果 JSON 中的每个“名称”字段都包含关键字,则通过测试,否则失败。

在下面找到我尝试使用的脚本

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
}
]

我期望的是,如果每个数组字段中缺少“怀孕”,它应该无法通过测试

最佳答案

我将您的 JSON 添加到 https://api.myjson.com/bins/13qh7i创建一个 API 以在 Postman 下进行测试,并在数组中添加一个额外的对象,其名称中没有“怀孕”一词,以便您可以看到测试的运行情况。

如果您遍历并检查 objects name 属性中的单词“pregnancy”,并将找到的那些存储在 hasPregnancyarray,然后您可以根据 API 响应 object 的 length 检查 arraylength array 以查看每个 object 是否确实包含关键字“pregnancy”。

下面是我在测试中使用的JS代码。我还将您的 indexOf 更新为 includes,因为您应该使用 indexOf 寻找 > -1。我还使 modelString 变量在 name 上应用 toLowerCase() 以确保检查不关心大小写等。

enter image description here

// 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);
});

关于javascript - postman 测试以在响应数组中查找关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56189525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com