1

在给定的响应片段中,“parentName”类型有时null或有时string。如何检查/编写测试用例以检查 typeofstring以及null一次。

tests["Verify parentName is string"] = typeof(jsonData.parentName) === "string" || null;

tests["Verify parentName is string"] = typeof(jsonData.parentName) === "string" || "null";
"demo": [
            {
                "id": 68214,
                "specializationId": 286,
                "name": "Radiology",
                "parentName": null,
                "primary": true
            }
        ],

如何在邮递员中处理这种情况(空字符串)。

4

1 回答 1

0

我不建议在 Postman 测试用例中使用if elseif 。Postman 具有用于模式检查的内置功能,您可以使用它并且可以在没有 if else 的情况下获得相同的结果。

首先,我正在考虑以下作为回应:

{
    "demo": [{
        "id": 68214,
        "specializationId": 286,
        "name": "Radiology",
        "parentName": null,
        "primary": true
    }]
}

邮递员测试应该是:

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
    "properties": {
        "parentName": {
            "type":["string", "null"]
        }
    }
};

pm.test('Verify parentName is string', function() {    
    var resParentName =  pm.response.json().demo[0].parentName;
    pm.expect(ajv.validate(schema, {parentName: resParentName})).to.be.true;
});

编辑:验证完整响应,而不仅仅是第一项。还要检查parentName响应中是否存在。

var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
    "properties": {
        "demo":{
            "type": "array",
            "items": {
                 "properties": {
                     "id":{ "type": "integer" },
                     "specializationId":{ "type": "integer" },
                     "name":{"type": "string"},
                     "parentName":{
                         "type":["string", "null"]
                     },
                     "primary":{"type": "boolean"}
                 },
                 "required": [ "parentName"]
            }
        }
    }
};

pm.test('Validate response', function() {
    pm.expect(ajv.validate(schema, pm.response.json())).to.be.true;
});
于 2019-11-08T07:45:32.343 回答