Send Newman API Test Results to Slack in CI/CD
To send Newman console aggregate results to Slack from Jenkins, you can follow these steps:
Prerequisites
- Jenkins: Make sure you have Jenkins installed and configured.
- Newman: Ensure you have Newman installed globally. You can install it using:
npm install -g newman
3. Slack Webhook: Set up an Incoming Webhook in Slack to post messages. You can create one by following Slack’s official documentation.
Slack’s official documentation.
Step-by-Step Guide
- Create a Jenkins Job: Create or open the Jenkins job where your Postman collections are being run.
2. Configure Jenkins to Run Newman:
- In the Jenkins job configuration, add a build step to run a shell script (or use a Windows batch command if on Windows).
- Use the following script as an example to run your Postman collections and save the results:
newman run your-collection.json -e /path/to/env.json -r cli,html --reporter-html-export newman-results.html > newman-console.log
3. Parse the Newman Console Output: You might want to parse and format the Newman console output to send it to Slack. You can use a script to extract the necessary information.
4. Send Results to Slack: Add another build step in Jenkins to send the parsed results to Slack using curl
or any other preferred method. Here's an example using curl
:
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/your/slack/webhook/url"
NEWMAN_RESULTS=$(cat newman-console.log)
curl -X POST -H 'Content-type: application/json' --data "{
\"text\": \"Newman Test Results:\n\`\`\`$NEWMAN_RESULTS\`\`\`\"
}" $SLACK_WEBHOOK_URL
Full Script Example
Combining the steps above, your full Jenkins build script might look like this:
#!/bin/bash
# Run Newman tests
newman run your-collection.json -r cli,html --reporter-html-export newman-results.html > newman-console.log
# Read the Newman console output
NEWMAN_RESULTS=$(cat newman-console.log)
# Send results to Slack
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/your/slack/webhook/url"
curl -X POST -H 'Content-type: application/json' --data "{
\"text\": \"Newman Test Results:\n\`\`\`$NEWMAN_RESULTS\`\`\`\"
}" $SLACK_WEBHOOK_URL
Additions and Customizations
- Formatting: You can customize the formatting of the message sent to Slack.
- Attachments: You can send the HTML report as an attachment to Slack using services like Slack file upload APIs.
- Error Handling: Add error handling in your script to manage failures gracefully.
Here’s a JavaScript-based approach using Node.js to extract and send the Newman console output summary to Slack.
Format Results to Slack
1. Install Required Packages: Make sure you have the necessary Node.js packages installed (axios
and fs
is a built-in module). Run the following command to install axios
:
npm install axios
2. Create the JavaScript Script: Ensure you have the following script saved as send_newman_summary_to_slack.js
in the correct directory
const fs = require('fs');
const axios = require('axios');
const SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/your/slack/webhook/url';
// Function to remove ANSI color codes
const removeAnsiCodes = (str) => {
return str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
};
// Read and parse the Newman console output
const data = fs.readFileSync('newman-console.log', 'utf8');
const lines = data.split('\n');
let summaryResults = {};
let insideSummary = false;
lines.forEach(line => {
const cleanLine = removeAnsiCodes(line).trim();
if (cleanLine.includes('iterations')) {
insideSummary = true;
}
if (insideSummary) {
if (cleanLine.includes('iterations') ||
cleanLine.includes('requests') ||
cleanLine.includes('test-scripts') ||
cleanLine.includes('prerequest-scripts') ||
cleanLine.includes('assertions') ||
cleanLine.includes('total run duration') ||
cleanLine.includes('total data received') ||
cleanLine.includes('average response time')) {
const parts = cleanLine.split('│').map(part => part.trim()).filter(Boolean);
if (parts.length >= 3) {
const key = parts[0].trim().replace(/ /g, '-').replace(':', '');
const value = parts[1].trim();
summaryResults[key] = value;
} else {
const keyValue = cleanLine.split(':').map(part => part.trim());
if (keyValue.length === 2) {
const key = keyValue[0].trim().replace(/ /g, '-');
const value = keyValue[1].trim();
summaryResults[key] = value;
}
}
}
}
});
// Format summary results
const formattedSummary = Object.keys(summaryResults).map(key => `${key.replace(/-/g, ' ')}: ${summaryResults[key]}`).join('\n');
// Send formatted summary to Slack
axios.post(SLACK_WEBHOOK_URL, {
text: `Newman Test Summary:\n\`\`\`${formattedSummary}\`\`\``
})
.then(response => {
console.log('Message posted to Slack successfully');
})
.catch(error => {
console.error('Error posting message to Slack:', error);
});
3. Create run.sh script
Run and verify Locally
#!/bin/bash
# Run Newman tests
newman run path/to/your-collection.json -e path/to/env.json -r cli,html --reporter-html-export newman-results.html > newman-console.log
# Run the Node.js script to parse the console output and send it to Slack
node path/to/send_newman_summary_to_slack.js
Jenkins Test: Once verified locally, run the Jenkins job to ensure the script executes correctly within the Jenkins environment.
stage('Send Reports to Slack') {
steps {
script{
sh 'node slack_report_sender.js'
}
}
}
This should help you integrate Newman test results into Slack via Jenkins effectively. If you need further customizations or run into any issues, feel free to ask!