Skip to content

Instantly share code, notes, and snippets.

@russianryebread
Last active June 24, 2024 22:22
Show Gist options
  • Save russianryebread/bc98e58a7957755eece7d930b49cec03 to your computer and use it in GitHub Desktop.
Save russianryebread/bc98e58a7957755eece7d930b49cec03 to your computer and use it in GitHub Desktop.
Postman notifications on test failure
pm.test("Successful status code [200, 201, 204]", function () {
const goodStatusCodes = [200, 201, 204]
// If there is a non-2xx failure, notify Slack:
if(goodStatusCodes.indexOf(pm.response.code) === -1) {
pm.sendRequest({
url: 'https://hooks.slack.com/services/<<SLACK_URL_ID>>',
method: 'POST',
header: 'Content-type: application/json',
body: {
mode: 'raw',
raw: JSON.stringify({ text: ':rotating_light: "Postman Collection" test step failure:\n\n*' + pm.info.requestName + '* with a response code of `' + pm.response.code + '`' + '\n\n```' + pm.response.text() + '```' })
}
});
}
pm.expect(pm.response.code).to.be.oneOf(goodStatusCodes)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment