postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.88k stars 1.16k forks source link

Feature Request: Allow for iterating over environments #1132

Closed andrewowen closed 7 years ago

andrewowen commented 7 years ago

Simply put, it would be nice when running a collection via node to allow for an environments key that can take multiple environments so that a single test can be iterated over with different environments. At the moment, it looks like you can only specify one environment at a time.

newman.run({
    collection: require('mycollection.json'),
    reporters: ['cli', 'teamcity', 'html'],
    environment: 'myenvironment.json'
}, function(err){
    if (err) {throw err;}
    console.log('collection run complete!');
});
kunagpal commented 7 years ago

@andrewowen What you've described is possible with data variables, and can be done as follows:

newman.run({
    collection: require('mycollection.json'),
    reporters: ['cli', 'teamcity', 'html'],
    iterationData: [{foo: 'bar'}, {alpha: 'beta'}] // array of objects of variables
}, function(err){
    if (err) {throw err;}
    console.log('collection run complete!');
});
andrewowen commented 7 years ago

@kunagpal thank you for response, however this doesn't seem to work properly with data that is put in environment json files. If I run my node module with the environment set to a postman_environment.json file it works fine, but if I take that data out of the file and try to put it into an array of environments, the tests fail.

kunagpal commented 7 years ago

If you're looking to use variables from environment files directly, you can do something like this:

var fs = require('fs'),
    newman = require('newman'),
    environments = ['env1.json', 'env2.json'];

newman.run({
    collection: require('./var-test.postman_collection'),
    reporters: ['cli', 'teamcity', 'html'],
    iterationData: environments.map(function (env) {
        var variables = {};

        try {
            variables = require('./' + env).values.reduce(function (result, variable) {
                if (variable && variable.hasOwnProperty && (!variable.hasOwnProperty('enabled') || variable.enabled)) {
                    result[variable.key] = variable.value;
                }

                return result;
            }, {});
        } catch (err) {
            console.error(err.message);
        }
        finally {
            return variables;
        }
    })
}, function(err) {
    if (err) { throw err; }
    console.log('collection run complete!');
});
andrewowen commented 7 years ago

@kunagpal that worked wonderfully! thanks! is there any chance of implementing this in a less DIY sense? I think it would make a great addition to the library

kunagpal commented 7 years ago

@andrewowen Well, there's no plan to implement something like this presently. The idea of an environment is to be singular with respect to a collection run. Iteration data, on the other hand, is meant to let a collection run cycle through a list of variable sets, as is the case here.

Feel free to let us know about any other suggestions/concerns/issues you may have. 😄