testingenvironment-variablespostmannewman

How to override env file on newman run?


How to rewrite variables in Newman? I have a file with my environment variables that are rewritten after a request is executed, everything works in Postman (RUN FOLDER), but in Newman all tests crash. Many of my variables do not have initial values, they only acquire them when a test passes. For example there is a POST request that adds a user, the body of the response will return to me:

{
    "id": null,
    "result": {
        "id": "c9497eb1-e4ea-47f0-a00e-a46cf8ef07a7"
    },
    "success": true
}

I take the value of the ID, and override the variable with the following command:

var jsonData = pm.response.json();
pm.environment.set('newUserId', jsonData.result.id);

When running Newman, everything crashes. Please help, if you would be so kind.

What key should I use when starting Newman? Or how to achieve positive results in NEWMAN to be like in Postman, is there such an option for processing variables?

Many tests drops when I use env value in URL path, like https://some-url/check/{createdUserId}.


Solution

  • You can export also environment variable too. The 'newman' running no problem if using exported environment variable

    How to export environment variable.

    Select 'environment quick look' - right/top icon in Postman

    enter image description here

    Select 'edit' menu

    enter image description here

    Select See more action (...)

    enter image description here

    Run newman with exported environment.json

    newman run my.postman_collection.json  -e my.postman_environment.json
    

    Demo

    #1 running a local API server for adding user and getting user

    #2 postman call

    #3 running collection

    #4 newman running

    #1 running a local API server

    Save as 'server'js'

    const express = require('express');
    const cors = require('cors');
    const uuid = require('uuid');
    
    const app = express();
    const port = 3000;
    
    app.use(express.json());
    app.use(cors());
    
    const users = {}; // Memory storage for user data
    
    // API 1: Create(Add) user by POST
    app.post('/createUser', (req, res) => {
      const { first_name, last_name, email } = req.body;
      const userId = uuid.v4(); // Generate a new UUID for the user
    
      users[userId] = { first_name, last_name, email }; // Store user data in memory
    
      res.status(201).json({
        id: null, // You can set this to any specific value if needed
        result: {
          id: userId,
        },
        success: true,
      });
    });
    
    // API 2: Get user by GET
    app.get('/getUser/:user_id', (req, res) => {
      const userId = req.params.user_id;
      const user = users[userId];
    
      if (!user) {
        return res.status(404).json({ error: 'User not found' });
      }
    
      res.json({
        first_name: user.first_name,
        last_name: user.last_name,
        email: user.email,
      });
    });
    
    app.listen(port, () => {
      console.log(`Server is running on port ${port}`);
    });
    

    Install dependencies

    npm install express cors uuid
    

    Running server

    node server.js
    

    enter image description here

    #2 postman call

    Create User 1 and get User 1, checked user information (name and email) Key is using user-id environment variable for POST (Adding)/GET(Getting) user pair.

    If not matched Adding user/ Getting user, something wrong.

    enter image description here

    POST returned

    {
        "id": null,
        "result": {
            "id": {"uuid"}
        },
        "success": true
    }
    

    GET user

    http://localhost:3000/getUser/{{user-id : uuid}}
    

    Response

    {"first_name":"Steve","last_name":"Jobs","email":"steve_jobs@example.com"}
    

    It can confirmed created user and get user is same or not by calling GET with uuid

    This format

    Create User1

    Tests tab for adding user

    POST call

    http://localhost:3000/createUser
    
    var jsonData = JSON.parse(responseBody);
    postman.setEnvironmentVariable("user-id", jsonData.result.id);
    console.log(pm.environment.get("user-id"));
    

    enter image description here

    Body

    {
        "first_name": "Steve", 
        "last_name":"Jobs", 
        "email": "steve_jobs@example.com"
    }
    

    enter image description here

    Get User1

    Tests tab for adding user

    GET call

    http://localhost:3000/getUser/{{user-id}}
    
    var jsonData = JSON.parse(responseBody);
    console.log(jsonData);
    
    pm.expect(jsonData.first_name).to.eql("Steve");
    pm.expect(jsonData.last_name).to.eql("Jobs");
    pm.expect(jsonData.email).to.eql("steve_jobs@example.com");
    

    enter image description here

    enter image description here

    Add user 2 with different user information

    enter image description here

    #3 running collection on Postman

    enter image description here

    #4 newman running on terminal

    newman run 1-1-demo.postman_collection.json  -e demo.postman_environment.json
    

    enter image description here