【问题标题】:postman schema validation into reporter-htmlextra邮递员模式验证到记者-htmlextra
【发布时间】:2019-11-26 11:34:13
【问题描述】:

我目前正在与邮递员一起运行一些测试,在那里我得到一个架构并尝试根据它验证我的结果。 我知道架构与我得到的响应不一致,但我想知道如何扩展结果以提供更多信息。

例如,如果我有这样的请求:

GET /OBJ/{ID}

它只是因反馈而失败:

Schema is valid:
expected false to be true

我希望能够在我的纽曼报告中获得更多反馈

这是我的测试示例:

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});

// only preform tests if response is successful 
if (pm.response.code === 200) {
  var jsonData = pm.response.json();

  pm.test("Data element contains an id", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.id).eql(pm.environment.get("obj_id"));
  });

  pm.test('Schema is valid', function() {
    pm.expect(tv4.validate(jsonData, pm.globals.get("objSchema"))).to.be.true;
  });
}

这就是我运行测试的方式:

const newman = require('newman');

newman.run({
    insecure: true,
    collection: require('../resources/API.postman_collection.json'),
    environment: require('../resources/API.postman_environment.json'),
    reporters: 'htmlextra',
    reporter: {
        htmlextra: {
            export: './build/newman_report.html',
            logs: true,
            showOnlyFails: false,
            darkTheme: false
        }
    }
}, function (err) {
    if (err) { 
      throw err; 
    }
    console.log('collection run complete!');
});

有没有办法获得有关验证失败的更多信息? 我尝试了一些快速的谷歌搜索,但没有找到任何有意义的东西

【问题讨论】:

    标签: newman postman-testcase


    【解决方案1】:

    这不是我想要的,但我设法用这样的方法修复它:

    // pre-check
    
    var schemaUrl = pm.environment.get("ocSpecHost") + "type.schema";
    
    pm.sendRequest(schemaUrl, function (err, response) {
      pm.globals.set("rspSchema", response.json());
    });
    
    // test
    
    var basicCheck = () => {
      pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
      });
    
      pm.test("Response time is less than 200ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(200);
      });
    };
    
    // create an error to get the output from the item validation
    var outputItemError = (err) => {
      pm.test(`${err.schemaPath} ${err.dataPath}: ${err.message}`, function () {
        pm.expect(true).to.be.false; // just output the error
      });
    };
    
    var itemCheck = (item, allErrors) => {
      pm.test("Element contains an id", function () {
        pm.expect(item.id).not.eql(undefined);
      });
      var Ajv = require('ajv');
      ajv = new Ajv({
        allErrors: allErrors,
        logger: console
      });
      var valid = ajv.validate(pm.globals.get("rspSchema"), item);
      if (valid) {
        pm.test("Item is valid against schema", function () {
          pm.expect(valid).to.be.true; // just to output that schema was validated
        });
      } else {
        ajv.errors.forEach(err => outputItemError(err));
      }
    };
    
    // check for individual response
    var individualCheck = (allErrors) => {
      // need to use eval to run this section
      basicCheck();
      // only preform tests if response is successful 
      if (pm.response.code === 200) {
        var jsonData = pm.response.json();
    
        pm.test("ID is expected ID", function () {
          var jsonData = pm.response.json();
          pm.expect(jsonData.id).eql(pm.environment.get("nextItemId"));
        });
        itemCheck(jsonData, allErrors);
      }
    }
    individualCheck(true);
    

    只需创建一个函数来进行项目测试,我会在其中执行愚蠢的 assert.false 以输出架构路径中的每个单独错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-23
      • 2020-04-30
      • 2018-12-14
      • 2019-04-22
      • 2017-04-15
      • 2021-09-15
      • 2014-10-13
      • 1970-01-01
      相关资源
      最近更新 更多