【问题标题】:Test exported from Postman fails in Newman从 Postman 导出的测试在 Newman 中失败
【发布时间】: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 中的工作方式与您引用变量的方式一样。

标签: postman newman


【解决方案1】:

在您的 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 参数、标头、授权、请求正文和标头预设上以这种方式使用它们。

Accessing Variables in the Request Builder

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 2016-04-02
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2015-10-12
    • 2021-06-28
    相关资源
    最近更新 更多