Megan Barton
Megan Barton

Reputation: 71

Newman not using environment variables

I have a Postman collection that I am trying to get working with newman, but my environment variables aren't being used.

The request URL is simply {{url}} and then I have an environment variable of the same name. I am running my test with this command:

newman run <path to collection> -e <path to env file> -r json --reporter-json-export <report path>

I can see in the report that the environment file is being read correctly and contains the variable I want to use, but the request fails and the report shows that the request was "url": "https://{{url}}" instead of what I actually wanted.

Of course the request fails because "https://{{url}}" isn't a valid URL, and then all of my tests fail because the request failed. This all works fine when I run the collection directly from Postman - my request goes to the server I intended and my tests pass as expected.

Upvotes: 5

Views: 4009

Answers (3)

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23582

I had this issue as I had a multi-line script in my Azure DevOps pipeline file and forgot the \ on one of the preceding lines i.e.

newman run "xxx.postman_collection.json" \
        --folder "yyy" \
        -r cli,junit 
        --environment "zzz.postman_environment.json"

The missing \ was making the --environment not take effect (as it thought the command was over).

This worked:

newman run "xxx.postman_collection.json" \
        --folder "yyy" \
        -r cli,junit \
        --environment "zzz.postman_environment.json"

Upvotes: 0

Aleksi
Aleksi

Reputation: 540

Had a similar issue with an error message like

DELETE /my-api/some-endpoint
  DELETE {{apiRoot}}/my-api/some-endpoint [errored]
     getaddrinfo ENOTFOUND {{apiroot}}

Which was a bit weird as a had the environment variable in my JSON but I assumed that the environment json format is just key/value pairs but it should actually be in postman format like:

{
    "id": "12345678-1234-1234-1234-123456789012",
    "name": "Environment X",
    "values": [
        {
            "key": "apiRoot",
            "value": "localhost",
            "enabled": true
        }
    ],
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2021-01-01T00:00:00.000Z",
    "_postman_exported_using": "Postman/9.0.2"
}

Upvotes: 0

Megan Barton
Megan Barton

Reputation: 71

My environment variables for URL initially were each using the format subdomain1.domain.com. Just for the heck of it I changed the URL in my request to https://{{url}}.domain.com and changed the environment variables to use just the subdomain. Then I exported everything again and ran newman, and it worked.

Not sure if I stumbled onto a bug with newman or the environment variable usage in URLs is not correctly documented, but at least I got my setup working.

Upvotes: 2

Related Questions