
2 minute read
Using environments in Newman
from POSTMAN API
by danzzersdev
Understanding Newman run options
Now that you have Newman installed and running, it's time to look at how to use it in a variety of cases. You have already seen how to run it with a simple collection, but what about if you have environment variables or data-driven tests? In this section, I will show you how to run Postman collections, including collections that contain environment variables and data-driven tests. I will also show you some of the options that you can use to control Newman's behavior.
Advertisement
Using environments in Newman
Often, a collection will have variables defined in an environment. Requests in that collection will need those variables when they are run, but the values don't get exported with the collection. To see this in practice, you can modify the Test GET request in the Newman Test collection that you made earlier by following these steps:
1. Create a new environment called Newman Test Env. 2. Modify the URL field of the Test GET request to set the https://postmanecho.com part to a variable called baseUrl.
Save that variable in the environment that you just created. 3. Save the request and then export the collection to a file (you can use the same filename we used previously and just overwrite it).
If you go to the command line and run that collection with Newman, you will see an rror message similar to the following, since it can't resolve the baseUrl variable that you created:
Figure 8.3 – Error resolving variable
In order to correct this error, you can manually specify the environment variable by using the --env-var flag. To do that, execute the following command:
newman run TestCollection.json --env-var "baseUrl=https:// postman-echo.com"
This tells Newman that it should set the baseUrl environment variable so that it's equal to the specified URL. With that information, the collection can run correctly. However, it is a lot of work to manually type this in. If you have multiple environment variables, you can call this flag multiple times, but you would still have to type each of them in, which would get very tedious. Instead of doing that all manually, let's see how you can save yourself some work by using an environment that's been exported from Postman. You can export an environment by following these steps:
1. Click on the Environments tab on the left-hand side of the navigation panel, as shown in the following screenshot:
Figure 8.4 – Navigating to Environments 2. Click on Newman Test Env in the navigation tree.