简体   繁体   English

如何将tv4.validateMultiple中的所有故障记录到具有正确失败状态的“测试结果”选项卡

[英]How can I log all failures in tv4.validateMultiple to Test Results tab with correct fail status

I have been able to get the error messages produced from tv4.validateMultiple to show in the Test Results, but I cannot figure out how to set them to show fail status. 我已经能够从tv4.validateMultiple中获取错误消息以显示在测试结果中,但我无法弄清楚如何将它们设置为显示失败状态。

var jsonData = JSON.parse(responseBody);
var schema = {...contains multiple error}
var results = tv4.validateMultiple(jsonData, schema);

if(results.valid){
    pm.test('Response has valid schema') = true;

} else {
    for (var i = 0; i < results.errors.length; i++) {
        pm.test("Contract: JSON Response has invalid schema in path [" + results.errors[i].dataPath + " | " + results.errors[i]); 
    }
}

Multiple messages are logged in the Test Results tab, but have a status of pass. “测试结果”选项卡中记录了多条消息,但状态为“通过”。 With the 'old style' Postman tests[], you can set it = false. 使用'旧式'邮差测试[],你可以设置它= false。 But that can't be used in a for loop because the test will end on first fail. 但是这不能在for循环中使用,因为测试将在第一次失败时结束。 pm.test will continue but the status is pass. pm.test将继续,但状态通过。

I tried placing the pm.test('Schema is valid', function() {pm.expect...} in a for loop but get the message "Don't make functions in a loop" message. So I tried pulling the function part outside, which didn't work either. 我尝试在for循环中放置pm.test('Schema is valid',function(){pm.expect ...}但是得到消息“不要在循环中生成函数”消息。所以我尝试拉动外面的功能部分,也没用。

I've also tried Ajv and setting allErrors: true. 我也试过Ajv并设置allErrors:true。

All I want to do is validate an entire response and report all failures in the Test Tab and xtrareports. 我想做的就是验证整个响应并报告测试选项卡和xtrareports中的所有失败。

I'm sure this can be improved, but the following works. 我确信这可以改进,但以下工作。

var jsonData = JSON.parse(responseBody);

var schema = {
    "type": "object",
    "properties": {
        "data": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "field1": { "type": "string", "pattern": uuid_format },
                "field2": { "type": "string", "pattern": date_format },
                "field3": { "type": "string" },
                "field4": { "type": "string", "enum": ["ACTIVE", "INACTIVE"]},
            }
        },
        "pages": { "type": ["string", "null"]
        }
    }
};

var result = tv4.validateMultiple(jsonData, schema);

if (result.valid){
    pm.test("Contract: JSON Response has valid schema"), setTestStatus;

} else {
    for (var i = 0; i < result.errors.length; i++) {
        pm.test('JSON Response is INVALID ' + result.errors[i].dataPath +" " + result.errors[i].message, setTestStatus);

    }

}

function setTestStatus() {
    pm.expect(result.valid).to.be.true;
}

I was unable to get the correct pass/fail status with ajv, which I would like to use instead of tv4. 我无法使用ajv获得正确的通过/失败状态,我想使用它而不是tv4。 I'm sure it has something to do with my regex or some detail like that even though I tested the regex pattern/response value and the schema with online validators. 我确定它与我的正则表达式或类似的细节有关,即使我使用在线验证器测试了正则表达式模式/响应值和模式。 But here it is anyway. 但无论如何它在这里。

var uuid_format = '/^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/i' 

var Ajv = require('ajv'),
ajv = new Ajv({logger: console, allErrors: true}),

var jsonData = JSON.parse(responseBody);
var schema = {...see above...}

var valid = ajv.validate(schema, jsonData);

if(valid){
    pm.test('Contract: JSON Response is valid', setTestStatus);

} else {
    for (var i = 0; i < ajv.errors.length; i++) {
        pm.test('JSON Response is INVALID ' + ajv.errors[i].dataPath +" " + ajv.errors[i].message, setTestStatus);
    }
}

function setTestStatus() {
    pm.expect(result.valid).to.be.true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM