Since Iām a ānew memberā Iām unable to post links/screenshots so bare with meā¦
Hi All,
Iāve shown this to a few people starting out with Postman in the Slack channel and thought it might have some user to others who are new to Javascript/Postman (Iām sure the more experienced users can suggest a better way of doing this )
What This Does:
- Every test run I have a āpassedā and āfailedā variable which gets set to 0 at the start of each test run.
- Every time a test passes or fails, +1 is added to the passed or failed postman variables.
- At the end of the test run, Postman checks if the failed variable value is greater than 0
- If itās not, I use the postman.setNextRequest(null) command to end the test on that request.
- If the failed variable is greater than 0, Postman will automatically go to the next request which is a Slack hook.
- This will send a message saying that the particular test collection has just ran and it contained X failures.
How To Do This:
- I have a āsetupā request which I use to clean and setup the environment for testing. This involves clearing all environment variables that may have been left over from the previous test run. This will also set the āpassedā and āfailedā variables to 0.
// This is a GET request calling https://postman-echo.com/delay/0
// Place the below in the Pre-Request Script tab
postman.clearEnvironmentVariables();
postman.setEnvironmentVariable("passed", 0);
postman.setEnvironmentVariable("failed", 0);
- In the same request, in the Tests tab, I have the below code. Donāt crucify me for using evals!
// Place the below in the Tests tab in the same request
// Instructions on what's happening below:
// Creates a postman variable called 'testResultCounter'
postman.setEnvironmentVariable("testResultCounter", () => {
// Creates an array which holds true or false values for tests that pass or fail
var test_results = Object.values(tests);
// It will loop through the above array:
for(var i = 0; i < test_results.length; i++) {
if(test_results[i] === true) {
// If it finds a true value, it converts the "passed" Postman Variable variable into an actual number so we can do maths with it. Postman Variables are converted to "string" when saved.
//A variable called thisPassed is created and it equals the number stored in the "Passed" Postman Variable. If this is the first time running, it will be 0 obviously.
thisPassed = parseInt(postman.getEnvironmentVariable("passed"));
// This will then get the value from the above "thisPassed" variable and add one to it. So if it's the first time running, 0 + 1 = 1.
//Then it'll overwrite the current postman variable by adding one to it
postman.setEnvironmentVariable("passed", thisPassed+ 1);
//This will keep repeating for every passed test by adding 1 to the "passed" Postman Variable.
} else {
// Now, if it finds a false in the array. It'll do the same as above but for the "failed" variables.
// So basically, every time "false" is found in the array +1 is added to the "failed" postman variable
thisFailed = parseInt(postman.getEnvironmentVariable("failed"));
postman.setEnvironmentVariable("failed", thisFailed + 1);
}
}
});
Since we used a ādirtyā eval we can now re-use the above code in all our tests by just calling ātestResultCounterā.
Re-usable code in Postman!? Nice!
- Now, in the Tests tab for all your future requests, you can just include the one line code to call the above code.
//Every test failure, +1 to the "failed" postman variable.
//Every test passed, +1 to the "passed" postman variable.
eval(environment.testResultCounter)();
- The second-to-last request in my collection is always a āteardownā phase. This will clear all environment variables, with the exception of the passed/failed variables.
// Once again, I'm just hitting the GET "https://postman-echo.com/delay/0" request.
// Place the below in the Pre-Request Script tab
// Store the passed and failed Postman Variables in memory so they're not truely "deleted"...yet.
var passed = environment.passed;
var failed = environment.failed;
postman.clearEnvironmentVariables();
//Re-Create the Postman Variables and give them the values saved in memory :)
postman.setEnvironmentVariable("passed", passed);
postman.setEnvironmentVariable("failed", failed);
If you find the above code ugly, feel free to upvote my Feature Request
- In the Tests tab in the same request:
if(environment.failed > 0) {
// If there were Test Failures go to the next request as planned.
postman.setNextRequest("EXACT NAME OF SLACK REQUEST WILL GO HERE");
} else {
// If no failures were found, don't go to the next request as usual. Just end the test here :)
postman.setNextRequest(null);
}
- Go to https://api.slack.com/apps and create an account/register your app.
-
Donāt feel intimated by the word āappā. This will just allow you to use POST API calls in Postman to send messages to either yourself or a channel in Slack.
-
Once registered, go to Your Apps at the top right.
-
Click your āapp nameā
-
Then click āIncoming Webhooksā
-
Make sure the button is toggled to ON for āActive Incoming WebHooksā at the top right.
-
Click āAdd New WebHook To Workspaceā and then select the channel or user you want to be notified from the dropdown.
-
I just selected myself as I personally want to be āpingedā on Slack whenever the API tests fail.
-
Once youāve confirmed everything youāll be given a URL that you can now use in Postman
-
Click copy and go back to Postman and create a POST request to the WebHook URL.
-
In the Body, click the ārawā radio button and select 'JSON(application/json) from the menu to the right of it.
-
Now, in the POST body create a JSON object which has the ātextā field and whatever value that field has will be displayed in Slack.
{
"text": "Postman:\n {{failed}} Tests have failed PANIC!!!"
}
- Send the POST request and you should be notified in Slack!
- The Slack request is the last request in my collection and Iāve named it āSlack - Postman Test Reporterā
- In the second-to-last request (which I called teardown) Iāll rename the setNextRequest command to use the Slack Request.
if(environment.failed > 0) {
postman.setNextRequest("Slack - Postman Test Reporter");
} else {
postman.setNextRequest(null);
}
- Now, if you run the collection through the Postman Runner or Newman, itāll use the setNextRequest() depending if any failures were found.
Feel free to learn more about the Slack integration at the below URL (such as adding the Postman icon and much more)
https://api.slack.com/incoming-webhooks
Itās been a long time since I configured the Slack integration so apologies if the Slack section of the tutorial was a bit bareā¦
Hopefully you get some value from this
Advantages:
- As soon as a Postman Monitor contains a failure you/the relevent team are notified imediately on Slack (I know emails are sent from the Monitor serivce, however itās harder to avoid an IM than an email)
- Youāre notified imediately if a test fails in your CI (TeamCity etc.)
- If youāre running a larger test (100ās of iterations/requests) in the Postman Runner, you can minimise the window and let the test run and be notified once itās finished and found any failures.
Disadvantages:
- If youāre sharing the collection with your team, if a team member runs the test locally and has made changes to the tests on their machine - If a failures occur from their test run the notification is sent to the Slack room which may cause some false alarms