[英]How can I use multiple pm.expect in one pm.test in postman tests
I want to check if request has a string then response has to contain this string in postman.我想检查请求是否有一个字符串,然后响应必须在 postman 中包含这个字符串。 I write a test like this but it doesn't work for me.
我写了一个这样的测试,但它对我不起作用。 I'm not sure I can use multiple pm.expect in one test with condition.
我不确定我是否可以在有条件的一项测试中使用多个 pm.expect。 My code is
我的代码是
pm.test("test",function(){
var hasA = data.hasOwnProperty('A');
var hasB = data.hasOwnProperty('B');
if(hasA === true){
pm.expect(response.A).to.eql(data.A);
}
if(hasB === true){
pm.expect(response.B).to.eql(data.B);
}});
Request can contain A and B both so I cannot use if..else statement.请求可以同时包含 A 和 B,所以我不能使用 if..else 语句。 When I run this test if hasA flag true then code doesnt go into for second if even hasB option is true.
当我运行此测试时,如果 hasA 标志为真,那么即使 hasB 选项为真,代码也不会 go 进入第二个。 How can I handle this problem.Can anyone help?
我该如何处理这个问题。有人可以帮忙吗?
expect is hard assertion so if one fails then it won't goto next one so you can use any of the below approach: expect 是硬断言,所以如果一个失败,那么它不会转到下一个,所以你可以使用以下任何一种方法:
try if conditions like:尝试以下条件:
pm.test("test", function () {
var hasA = data.hasOwnProperty('A');
var hasB = data.hasOwnProperty('B');
var hasBoth = hasA && hasB
if (hasBoth) {
pm.expect([response.A, response.B]).to.eql([data.A, data.B]);
} else if (hasA) {
pm.expect(response.A).to.eql(data.A);
} else if (hasB){
pm.expect(response.B).to.eql(data.B);
}
});
or或者
try catch like:尝试像这样:
pm.test("test", function () {
var hasA = data.hasOwnProperty('A');
var hasB = data.hasOwnProperty('B');
let error = {};
try {
if (hasA) {
pm.expect(response.A).to.eql(data.A);
}
} catch (e) {
error.A = e
}
try {
if (hasB) {
pm.expect(response.A).to.eql(data.A);
}
} catch (e) {
error.B = e
}
if(Object.keys(error).length){
pm.expect.fail(JSON.stringify(error))
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.