首页 > 解决方案 > 邮递员模式验证到记者-htmlextra

问题描述

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

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

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!');
});

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

输出

标签: newmanpostman-testcase

解决方案


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

// 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 以输出模式路径中的每个单独的错误


推荐阅读