How to execute test using newman if tests are in package

Hi! I created my collection in postman and it works correctly, but I would like to execute it using newman. In my collection the tests are in a specific package, in each call the tests are called directly from the package: const getDs = pm.require(‘get_ds’);
At this point:

  • I proceed by exporting the collection in json format
  • I open the command line and execute: newman run collection.json

But the execution fails: TypeError in test-script.

If I check the json file of the collection, I see that in the specific calls the packages are called, but they are not part of the json.

If instead of using the packages, in the individual calls I write the test in each one and then export the json, newman works.

How can I make newman read, in addition to the json of the collection, also the package?

Hey @apfr1 :waving_hand:

Welcome to the Postman Community! :postman:

Packages are not support in Newman, these are supported in the Postman CLI though.

Hi @danny-dainton thank you for your reply, but it doesn’t solve the problem: I used postman cli to execute my collection, but the result is always the same; if I run the collection directly from postman, it is ok:

Loading packages...[0/1] [Error]
  1⠄ Error in test-script

Can you provide more details about the actual script in the package and also how you’re calling this in Postman, please?

A visual example of this is always better than trying to explain it only using words. :folded_hands:

Hi @danny-dainton yes, of course:
this is my script in the package:

function Status_200() {
	pm.test("Status test", function () {
	    if (pm.response.code === 200) {
        console.log("Status is OK");
    } else {
        throw new Error(`Expected status 200 but got ${pm.response.code}`);
    }
 });
}

// Call the test function
Status_200();

// Export the function to be used in other modules
module.exports = {
    Status_200
};

and I call it in each get as follow in the script section of each get:

const getDs = pm.require('get_ds');
1 Like

I just ran a basic example using your script:

This would work with the Postman CLI, if you were logged in, then the package id in the exported JSON file, would be read and used correctly in the run.

Do you see this output in your terminal?

Yes, I see this output when trying to load the packages (the output about the error, but the login is done successfully) - @danny-dainton

It’s likely that there is a different piece of information below, in the assertion summary about the results table.

The use a scripts is limited in the Postman CLI for specific plan types, I don’t know what plan you’re using but I suspect this is the case here.

1 Like