Automated tests 邮递员pm.response.to.have.header未按预期工作 使用邮递员版本v6.1.3请参见

Automated tests 邮递员pm.response.to.have.header未按预期工作 使用邮递员版本v6.1.3请参见,automated-tests,postman,Automated Tests,Postman,第1部分 邮递员的测试示例包括此测试,以查看是否存在标题: pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); }); 我的响应标题包含以下内容: Content-Type →application/json 我希望上面的测试能通过,但失败了。为什么? 它说: 此外,由于上述测试失败,因此我希望此负面测试会成功,但它也失败了: pm.test("Con

第1部分

邮递员的测试示例包括此测试,以查看是否存在标题:

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
我的响应标题包含以下内容:

Content-Type →application/json
我希望上面的测试能通过,但失败了。为什么? 它说:

此外,由于上述测试失败,因此我希望此负面测试会成功,但它也失败了:

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
它说:

FAIL  Content-Type is present | AssertionError: expected response to not have header with key 'Content-Type'
请注意,这与上面的错误消息完全相同。他们都说“预期的反应是没有的”,这在我看来是错误的

第二部分

为了让整个体验更加混乱,测试键和值字符串是可行的。我是不是误解了“have.header”

它返回:

PASS  Content-Type is present
PASS  Content-Type is present
再一次,这个的阴性测试也通过了

它返回:

PASS  Content-Type is present
PASS  Content-Type is present
第三部分


救命

我的问题似乎与此错误有关:


“pm.response.to.not.be会导致以后的断言反向工作。”

您现在可能已经可以使用此功能了,但无论如何,以下是对我有效的方法:

pm.test("Content Type is present", function () {
    pm.expect(pm.response).to.have.header("Content-Type");
})
pm.test("Content Type is present", function () {
    pm.expect(pm.response).to.have.header("Content-Type");
})