如何测试 Postman 中是否缺少(可能)嵌套的 JSON 属性?
How to test for absence of (potentially) nested JSON properties in Postman?
我想测试是否没有嵌套 属性 "x"
如果响应如下所示,则测试必须失败
甲:
{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"x": 1,
"y": 2
}
}
}
但下面的例子必须通过:
乙:
{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"y": 2
}
}
}
C:
{
"first": 1,
"second": {
"one": 1,
"two": 2
}
}
D:
{
"first": 1
}
当然可以。我可以使用 pm.expect(object).to.not.have.property("x")
来测试缺席。但这并非对所有情况都有帮助。
例如,我的 PostMan 测试代码:
pm.test("(nested)property 'x' not available", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.second.three).to.not.have.property("x")
});
适用于 A 和 B 情况,但不适用于 C 和 D,因为 属性 的父级 "second" 或 "three" 可以未定义。但是我不想测试它们的缺失,因为它不是这个特定测试的目标。
是否有任何 BDD Chai 函数提供此功能,或者我是否被迫为这种情况实现递归辅助函数?
这不是您要找的东西,但我希望它对任何感兴趣的人都有用。
首先,将 JSON 字符串化为一个字符串,然后搜索带有双引号的关键字以精确查找确切的词(属性)。
当然,这只是一个概念,但我认为如果一切都失败了,您可以尝试一下。
我明白了,还有改进的余地。因此,如果需要,请随时进行调整。
const cases = [{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"x": 1,
"y": 2
}
}
},
{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"y": 2
}
}
},
{
"first": 1,
"second": {
"one": 1,
"two": 2
}
},
{
"first": 1,
"example": "notx"
}
]
const property = "x" // Property that you are looking for its absence
cases.forEach(c => {
console.log(JSON.stringify(c).includes('"' + property + '"'))
// True = property is present | False = property is absent
})
您可以使用内置的 Lodash
函数对数据进行更多细分,而不是尝试在 pm.expect() 语句中完成所有操作。
_.get()
函数可能是一个有用的探索函数 - https://lodash.com/docs/4.17.11#get
我的最终解决方案:
var a = {
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"x": 1,
"y": 2
}
}
};
var b = {
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"y": 2
}
}
};
var c = {
"first": 1,
"second": {
"one": 1,
"two": 2
}
};
var d = {
"first": 1
};
pm.test("(nested)property 'x' NOT available in 'a'", function () { //must fail for a
var jsonData = a;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
pm.test("(nested)property 'x' NOT available in 'b'", function () {
var jsonData = b;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
pm.test("(nested)property 'x' NOT available in 'c'", function () {
var jsonData = c;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
pm.test("(nested)property 'x' NOT available in 'd'", function () {
var jsonData = d;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
我想测试是否没有嵌套 属性 "x"
如果响应如下所示,则测试必须失败
甲:
{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"x": 1,
"y": 2
}
}
}
但下面的例子必须通过:
乙:
{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"y": 2
}
}
}
C:
{
"first": 1,
"second": {
"one": 1,
"two": 2
}
}
D:
{
"first": 1
}
当然可以。我可以使用 pm.expect(object).to.not.have.property("x")
来测试缺席。但这并非对所有情况都有帮助。
例如,我的 PostMan 测试代码:
pm.test("(nested)property 'x' not available", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.second.three).to.not.have.property("x")
});
适用于 A 和 B 情况,但不适用于 C 和 D,因为 属性 的父级 "second" 或 "three" 可以未定义。但是我不想测试它们的缺失,因为它不是这个特定测试的目标。
是否有任何 BDD Chai 函数提供此功能,或者我是否被迫为这种情况实现递归辅助函数?
这不是您要找的东西,但我希望它对任何感兴趣的人都有用。
首先,将 JSON 字符串化为一个字符串,然后搜索带有双引号的关键字以精确查找确切的词(属性)。
当然,这只是一个概念,但我认为如果一切都失败了,您可以尝试一下。
我明白了,还有改进的余地。因此,如果需要,请随时进行调整。
const cases = [{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"x": 1,
"y": 2
}
}
},
{
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"y": 2
}
}
},
{
"first": 1,
"second": {
"one": 1,
"two": 2
}
},
{
"first": 1,
"example": "notx"
}
]
const property = "x" // Property that you are looking for its absence
cases.forEach(c => {
console.log(JSON.stringify(c).includes('"' + property + '"'))
// True = property is present | False = property is absent
})
您可以使用内置的 Lodash
函数对数据进行更多细分,而不是尝试在 pm.expect() 语句中完成所有操作。
_.get()
函数可能是一个有用的探索函数 - https://lodash.com/docs/4.17.11#get
我的最终解决方案:
var a = {
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"x": 1,
"y": 2
}
}
};
var b = {
"first": 1,
"second": {
"one": 1,
"two": 2,
"three": {
"y": 2
}
}
};
var c = {
"first": 1,
"second": {
"one": 1,
"two": 2
}
};
var d = {
"first": 1
};
pm.test("(nested)property 'x' NOT available in 'a'", function () { //must fail for a
var jsonData = a;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
pm.test("(nested)property 'x' NOT available in 'b'", function () {
var jsonData = b;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
pm.test("(nested)property 'x' NOT available in 'c'", function () {
var jsonData = c;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});
pm.test("(nested)property 'x' NOT available in 'd'", function () {
var jsonData = d;
pm.expect(_.get(jsonData, "second.three.x", undefined)).to.be.undefined;
});