Newman + Postman tests in CircleCI

alexandlazaris

Alexander Lazaris

Posted on February 9, 2021

Newman + Postman tests in CircleCI

Running Postman tests in CircleCI

Ready the collection

  1. stored all urls + auth values as environment variables, ensuring each hardcoded value is replaced by it's respective variable
  2. added validations after each request to test status code + pass through response values
  3. export postman collection to a new folder. Hint: This new folder will become the git repo folder
var jsonData = JSON.parse(responseBody);
if (jsonData.access_token) {
    pm.environment.set("user_access_token", jsonData.access_token);
}

pm.test("Status code is 200");
pm.response.to.have.status(200);
Enter fullscreen mode Exit fullscreen mode

Newman ....

  1. npm init
  2. npm install newman
  3. test run the collection using ./node_modules/.bin/newman run collectionfile.json
  4. each env variable won't have a value at this point. To populate them, append --env-var envVariableName=envVariableValue to the run cmd above. These will later be added + stored in CircleCI.
  5. add npm test which contains the full run cmd above
  6. stored all of the above files in a git repo and pushed

`"test": "newman run 'postman_collection.json' --env-var --reporters junit,cli"

Is it coming together now?

  1. sign up to circleci using GitHub
  2. create project linked to github repo
  3. create config.yml file which contains circleci instructions. Use a Node template provided circleci or create your own.
  4. create env variables in CircleCI project, matching the exact name they had in Postman AND in npm test

Alt Text

had to include screenshot of config.yml contents as I don't know how to retain indenting in markdown

Time to trigger the tests

  1. the lovely feature of building for each PR commit is enabled by default in circleci
  2. final directory files + folders is clean & minimal. Mine is: Alt Text
  3. trigger a build by committing to the repo

End result:

Alt Text

Well, apart from the failed test due to bad test data, it all worked as expected!

💖 💪 🙅 🚩
alexandlazaris
Alexander Lazaris

Posted on February 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Newman + Postman tests in CircleCI
circleci Newman + Postman tests in CircleCI

February 9, 2021