admin管理员组文章数量:1390775
I want to integrate Postman/ Newman API tests into CICD, so the test results should always be passed (or skipped). Therefor I want to use conditional tests, dependent on the data of the response.
I tried the method described on GitHub, but the condition in my case is very different.
So if the json body of the response contains an empty array, tests should be skipped. If not, perform tests...
Empty data
{
"data": []
}
Testable data
{
"data": [
{
"key1": "value1",
"key2": {
"amount": 1357,
"unit": "units"
},
"from": "2019-08-01",
"to": "2019-08-31",
}
]
}
Test script
let response = JSON.parse(responseBody);
pm.test("Status code is 200", function() {
pm.expect(pm.response.code).to.equal(200);
});
(pm.expect(pm.response.json().data).to.be.empty === true ? pm.test.skip : pm.test)('Body is empty', function () {
pm.environment.set("key2Amount", response.data[0].key2.amount);
var key2Amount = pm.environment.get("key2Amount");
pm.test("Response includes corresponding amount", function () {
pm.expect(pm.response.json().data[0].key2.amount).to.eql(key2Amount);
});
});
Empty data: TypeError: Cannot read property 'key2' of undefined
.
Testable data: AssertionError: expected [ Array(1) ] to be empty
.
I've also tried it with
(pm.expect([]).to.be.an('array').that.is.empty ? pm.test : pm.test.skip)
Testable data: Tests performed positive.
Empty data: TypeError: Cannot read property 'key2' of undefined
. Why not skipped?
Further
(pm.expect([]).to.be.empty ? pm.test.skip : pm.test)
Empty data: skipped tests
Testable data: skipped tests
What would be the correct condition on the array to make the tests run or skipped?
I want to integrate Postman/ Newman API tests into CICD, so the test results should always be passed (or skipped). Therefor I want to use conditional tests, dependent on the data of the response.
I tried the method described on GitHub, but the condition in my case is very different.
So if the json body of the response contains an empty array, tests should be skipped. If not, perform tests...
Empty data
{
"data": []
}
Testable data
{
"data": [
{
"key1": "value1",
"key2": {
"amount": 1357,
"unit": "units"
},
"from": "2019-08-01",
"to": "2019-08-31",
}
]
}
Test script
let response = JSON.parse(responseBody);
pm.test("Status code is 200", function() {
pm.expect(pm.response.code).to.equal(200);
});
(pm.expect(pm.response.json().data).to.be.empty === true ? pm.test.skip : pm.test)('Body is empty', function () {
pm.environment.set("key2Amount", response.data[0].key2.amount);
var key2Amount = pm.environment.get("key2Amount");
pm.test("Response includes corresponding amount", function () {
pm.expect(pm.response.json().data[0].key2.amount).to.eql(key2Amount);
});
});
Empty data: TypeError: Cannot read property 'key2' of undefined
.
Testable data: AssertionError: expected [ Array(1) ] to be empty
.
I've also tried it with
(pm.expect([]).to.be.an('array').that.is.empty ? pm.test : pm.test.skip)
Testable data: Tests performed positive.
Empty data: TypeError: Cannot read property 'key2' of undefined
. Why not skipped?
Further
(pm.expect([]).to.be.empty ? pm.test.skip : pm.test)
Empty data: skipped tests
Testable data: skipped tests
What would be the correct condition on the array to make the tests run or skipped?
Share Improve this question asked Sep 25, 2019 at 9:29 ZenoZeno 531 gold badge1 silver badge3 bronze badges1 Answer
Reset to default 2Could you use something like this:
let response = pm.response.json();
pm.test("Status code is 200", function() {
pm.expect(pm.response.code).to.equal(200);
});
let skipTest = (response.data === undefined || response.data.length === 0);
(skipTest ? pm.test.skip : pm.test)('Body is empty', function () {
pm.environment.set("key2Amount", response.data[0].key2.amount);
pm.test("Response includes corresponding amount", function () {
pm.expect(response.data[0].key2.amount).to.eql(pm.environment.get("key2Amount"));
});
});
本文标签: javascriptPostman conditional tests if json body array is emptyskip testsStack Overflow
版权声明:本文标题:javascript - Postman conditional tests if json body array is empty = skip tests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753810a2623339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论