Skip to content

Instantly share code, notes, and snippets.

@polster
Created November 29, 2019 11:41
Show Gist options
  • Save polster/483803327883423ed4a9977799b392f4 to your computer and use it in GitHub Desktop.
Save polster/483803327883423ed4a9977799b392f4 to your computer and use it in GitHub Desktop.
Postman collection execution script for ci-pipelines
#!/bin/bash
set -o pipefail
# #############################################################################
#
# Helper script used to execute Postman collections.
#
# #############################################################################
function usage(){
echo "executes postman collections using newman and generates test reports."
echo "Please follow the Postman standard naming convention for collection- and env-files:"
echo " - collection_name.postman_collection.json"
echo " - collection_name.environment.postman_environment.json"
echo "usage: $0 -p project id -r repo id -v version [-h]"
}
function error {
echo -e "$1" >&2
exit 1
}
while [[ "$#" -gt 0 ]]; do case $1 in
-e|--environment) environment="$2"; shift;;
-c|--collection-name) collection_name="$2"; shift;;
-p|--collection-path) collection_path="$2"; shift;;
-r|--report-path) report_path="$2"; shift;;
-h|--help) usage; exit 0;;
*) error "Unknown parameter passed: $1";;
esac; shift; done
[[ -z "$environment" ]] && error "Target environment not set, aborting!"
[[ -z "$collection_name" ]] && error "Collection name not set, aborting!"
[[ -z "$collection_path" ]] && error "Collection path not set, aborting!"
[[ -z "$report_path" ]] && error "Output path for reports not set, aborting!"
collection="$collection_path/$collection_name.postman_collection.json"
configuration="$collection_path/$collection_name.$environment.postman_environment.json"
output="$report_path/$collection_name.$environment.output.log"
report="$report_path/$collection_name.$environment.report"
newman run -k \
--reporters cli,json,html,junit \
--reporter-json-export "$report.json" \
--reporter-junit-export "$report.xml" \
--reporter-html-export "$report.html" \
"$collection" \
-e "$configuration" | tee "$output"
exitCode=$?
exit $exitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment