newman - 邮递员模式验证到记者-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!');
});
有没有办法获得有关验证失败的更多信息?我尝试了一些快速的谷歌搜索,但没有找到任何有意义的东西
解决方案
这不是我想要的,但我设法用这样的东西来修复它:
// 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 以输出模式路径中的每个单独的错误
推荐阅读
- c - 无法在屏幕上打印文件内容
- sql - 试图从不包含值的 ID 中恢复结果
- ios - NativeScript / iOS 标签未在最后一个 StackLayout 中完全显示
- time-complexity - 下面检查一个点是否位于多边形内的时间复杂度是多少?是 O(n) 还是别的什么?
- c# - 如何在 JSON 中编写 POST 请求以发布新产品?
- html - 仅使用 CSS 和 HTML 来激活
标签? - c# - ASP.NET MVC Core API 将枚举序列化为字符串
- python - LinearSVC(scikit-learn)没有取得任何进展
- python - 如何从 OpenTURNS 中具有用户定义梯度的 Python 函数估计泰勒分解方差?
- arrays - 通过数组的 switch 语句中的错误