【发布时间】:2019-05-05 22:30:41
【问题描述】:
我们从 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 文件的 sn-p 如下所示:
{
"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": []
},
有什么线索???
【问题讨论】:
-
Postman UI 中的测试是什么样的?
{{endpoint}}是来自环境文件吗?如果您有 6 个请求并且每个请求中都有一个pm.sendRequest- 那不是 12 个吗? -
我不明白
pm.sendRequest在 UI 中的工作方式与您引用变量的方式一样。