Search code examples
postmanpostman-collection-runnernewman

Total number of assertions in a folder run by newman


I am using newman to run my postman collections. Since there are multiple collections I run the whole folder using newman. The problem I have is that newman displays the assertion count separately for each collection so I have to add them up manually to get the total number of assertions.

Is there anyway that I can get the total count of assertions for the entire folder ?


Solution

  • If you are referring to npm script that runs all collections inside a folder then something like this would work. It is a very primitive solution but I think that there is no other way because we call newman.run multiple times.

    #!/usr/bin/env node
    
    var newman = require('newman'),
        fs = require('fs');
    
    fs.readdir('./collections', function (err, files) {
        if (err) { throw err; }
    
        files = files.filter(function (file) {
            return (/^((?!(package(-lock)?))|.+)\.json/).test(file);
        });
        
        console.log(files);
    
        var failedTests = 0;
        var completed = 0;
        files.forEach(function (file) {
            newman.run({
                collection: require(`./collections/${file}`),
                reporters: 'cli',
            }, function (err) {
                if (err) { throw err; }
            }).on('done', function (err, summary) {
                var collectionName = summary.collection.name;
                summary.run.failures.forEach(failure=>{
                    failedTests++;
                });
                completed++;
                if(completed == files.length){
                    if(failedTests != 0){
                        console.log("Run completed!");
                        console.log("Tests failed: "+ numFailedTests);
                    }else{
                        console.log("No failed tests");
                    }
                }
            });
        });
    });