postman - 从 Postman 导出的测试在 Newman 中失败
问题描述
我们从 Postman 导出了一组测试(使用带有测试的 JSON 文件和带有环境变量的单独 JSON 文件),尝试像newman run tests.json -e environment.json
使用该集合一样运行。
输出令人困惑:
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 12 │ 6 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 6 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 28 │ 0 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 819ms │
├───────────────────────────────────────────────┤
│ total data received: 9.8KB (approx) │
├───────────────────────────────────────────────┤
│ average response time: 156ms │
└───────────────────────────────────────────────┘
通过的断言数量 (28) 表明所有测试都正常。但是......实际上只有 6 个请求在集合中,所以似乎请求被执行了两次,其中一个执行失败了,因为没有以某种方式扩展变量......
这就是输出的内容(6 次):
1. Error Invalid URI "http:///%7B%7Bendpoint%7D%7D/products/4"
at request inside ""
所以这意味着这些请求中没有填写环境变量。
tests.json 文件的片段如下所示:
{
"name": "AppStore BackEnd BAKERY_PRODUCT 4 test",
"event": [
{
"listen": "test",
"script": {
"id": "0e074806-1248-4446-865d-9e0f3d733ba2", "exec":[
"pm.sendRequest(\"http://{{endpoint}}/products/4\", function (err, response) {",
" ",
" pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
" });",
" ",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://{{endpoint}}/products/4",
"protocol": "http",
"host": [
"{{endpoint}}"
],
"path": [
"products",
"4"
]
}
},
"response": []
},
有什么线索???
解决方案
在您的选项卡中尝试此Tests
操作,然后导出集合:
pm.sendRequest(`${pm.environment.get('endpoint')}/products/4`, (err, response) => {
pm.test("Status code is 200", () => {
pm.response.to.have.status(200)
})
})
在该选项卡中使用{{endpoint}}
不知道要引用什么。
您只能在 URL、URL 参数、标头、授权、请求正文和标头预设上以这种方式使用它们。
推荐阅读
- tensorflow - 如何在 Tensorflow 自定义层中为输出张量初始化 None 形状?
- python - 如何使用 beautifulsoup 查找特定表列标题的值?
- ajax - 使用ajax获取用户数据
- javascript - Extensions do not load after the second time in Forge Viewer
- kendo-ui - 读取剑道网格数据时如何忽略空行?
- python - 如何更新全局计数器
- python - 在 python 中添加 headers 和 body 作为 dict 的soap请求
- excel - 当用户键入“-”字符时锁定列中的单元格
- python - 邮政和邮政地址 python 库之间有区别吗?
- dart - Dart 最佳实践,向下转换时,哪一个是局部变量的首选 `as` 或类型注释