首页 > 解决方案 > 从 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": []
            },

有什么线索???

标签: postmannewman

解决方案


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

在请求生成器中访问变量


推荐阅读