postmanlabs / newman

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

[Question] Is there an option to make the newman execution fail if it did not run any tests? #3164

Open sudhirbits opened 10 months ago

sudhirbits commented 10 months ago

Is there an option to make the newman execution fail if it did not run any tests?

AnshSinghSonkhia commented 7 months ago

Hey @sudhirbits, Newman doesn't have a built-in option to fail the execution explicitly if no tests are run. However, you can achieve this by incorporating some scripting or checking mechanisms in your workflow.

So, after Newman execution we can use a script to check the number of executed tests from the report. If zero tests were run, set a non-zero exit code for the script, indicating failure.

Something like this:

# Run Newman and save the result in a variable
NEWMAN_RESULT=$(newman run your_collection.json -e your_environment.json)

# Check if there are any test failures in the Newman result
if echo "$NEWMAN_RESULT" | grep -q "failures="; then
  echo "Tests failed."
  exit 1  # Exit with a non-zero status to indicate failure
else
  echo "Tests passed."
fi

Another option could be:

While using a CI/CD tool, configure it to fail the build based on the Newman exit code or specific keywords in the output (e.g., "0 tests executed").

hope it helps :)