Newman using external parameter files

Required software

  • Postman (the GUI makes everything simpler)
  • NodeJS (recommended: latest LTS version)
  • Newman package, plus custom reporters (e.g. htmlextra)
    1
    2
    3
    npm install -g newman
    # install some extra reporter if you like
    npm install -g newman-reporter-htmlextra

The Postman collection script

You will work with Postman as usual. The Postman collection runner is behind the Workspaces which require a Postman account. However, you may run it on Newman without any need for account.

Format

Item Format
Environment variables "{{MESSAGE}}"
Script variable data.message

Parameters on request data

  • You can have parameteres on Body -> Raw.

    1
    2
    3
    {
    "text": "{{MESSAGE}}"
    }
  • We are getting that parameter from an environment variable. We configure that on a request script, in order to redirect it to the external data file.

    1
    pm.environment.set("MESSAGE", data.message);

Parameters on test scripts

  • We can pass the parameters directly to this code excerpts. Postman provides snippets to make this task faster.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    });
    pm.test("Check if the text was properly sent", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.contents.text).to.eql(data.message);
    });
    pm.test("Check if translation was correct", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.contents.translated).to.eql(data.expected_translation);
    });

Full exported code

  • Go to the collection menu, and select export. You will get a json file.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    {
    "info": {
    "_postman_id": "2d07d2fd-5350-4959-8f41-a1d091826933",
    "name": "Fun translations",
    "description": "A few funny translations, like talking as Yoda",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
    {
    "name": "Yoda",
    "event": [
    {
    "listen": "test",
    "script": {
    "exec": [
    "pm.test(\"Status code is 200\", function () {\r",
    " pm.response.to.have.status(200);\r",
    "});\r",
    "pm.test(\"Check if the text was properly sent\", function () {\r",
    " var jsonData = pm.response.json();\r",
    " pm.expect(jsonData.contents.text).to.eql(data.message);\r",
    "});\r",
    "pm.test(\"Check if translation was correct\", function () {\r",
    " var jsonData = pm.response.json();\r",
    " pm.expect(jsonData.contents.translated).to.eql(data.expected_translation);\r",
    "});"
    ],
    "type": "text/javascript"
    }
    },
    {
    "listen": "prerequest",
    "script": {
    "exec": [
    "pm.environment.set(\"MESSAGE\", data.message);"
    ],
    "type": "text/javascript"
    }
    }
    ],
    "request": {
    "method": "POST",
    "header": [],
    "body": {
    "mode": "raw",
    "raw": "{\r\n \"text\": \"{{MESSAGE}}\"\r\n}\r\n",
    "options": {
    "raw": {
    "language": "json"
    }
    }
    },
    "url": {
    "raw": "https://api.funtranslations.com/translate/yoda",
    "protocol": "https",
    "host": [
    "api",
    "funtranslations",
    "com"
    ],
    "path": [
    "translate",
    "yoda"
    ]
    },
    "description": "Post query to talk like Yoda.\n- It needs to fill the body of the request"
    },
    "response": []
    }
    ]
    }

The external file

CSV

Format

  • The first line will contain the name of the fields.
  • The following lines wil contain the values of those fields for each iteration.

Full Code

1
2
3
message, expected_translation
"This is a message", "A message, this is"
"This is a dummy", "A dummy, this is"

Json

Format

  • It is represented as a list of Json objects.
  • Each object must have its attributes defined as key-value pairs.

Full Code

1
2
3
4
5
6
7
8
9
10
[
{
"message": "This is a message",
"expected_translation": "A message, this is"
},
{
"message": "This is a dummy",
"expected_translation": "A dummy, this is"
}
]

Run it

You can run the Newman command from the command line.

1
2
newman run FunTranslations.postman_collection.json -d data-translate.csv -r cli,htmlextra
newman run FunTranslations.postman_collection.json -d data-translate.json -r cli,htmlextra