Overview:
Test automation is not just automating the application. I would say even sharing the test results programmatically with the team is part of Test automation. So that , team does not depend on any individual to get the results. Team can also take further action based on the test results without any delay.
In this article, we are going to see how we can share our performance test results in Slack.
Slack comes with many Apps & APIs for easy integration with other applications. Check here for more information.
Slack – Jenkins CI App – Integration:
I assume most of us use Jenkins for the Continuous automated testing. So in this article, I have provided the steps to integrate Jenkins and Slack. If you use other tools like Team City, Bamboo etc, the process will still be almost same.
Slack:
- Go to https://<team-domain>.slack.com/apps
- Browse for ‘Jenkins’ app
- Install Jekins CI App in your slack
- Click on the ‘Add Configuration’
- Make a note of team domain and Token
- Select the channel where we want to pose the results
Jenkins:
- Go to Manage Jenkins -> Manage Plugins -> Available Tab
- Install Slack Notification Plugin
- Update Slack domain and token => Manage Jenkins -> Configure System -> Global Slack Notifier Settings
- Click on the ‘Test Connection’ – You should see ‘success’ message in Jenkins and below message in slack channel.
Jenkins – Performance Test Job:
- I create a Jenkins job for Performance Test (A step-by-step tutorial on this is here )
- Update the post build actions as shown here – with the custom message you want
- If you need to share the Grafana dashboard URL – then add the URL in the below format.
- Once the Performance Test is complete, you will be notified in slack as shown here.
Sharing Results using Slack HTTP API:
The above Jenkins plugin is very easy to use. However it does not allow us to upload files / share images etc. Lets see How it can be done using Slack HTTP API.
- First we add ‘Incoming WebHook’ in Slack
- Slack generates a HTTP Post URL . Any message sent to the URL -following this format – will be visible in your slack channel.
- This approach provides a lot of flexibility for us to customize the message.
- We could use this approach to share performance test results – charts in the slack channel.
- To share the image – HTTP POST request body data should be in the below format.
{
"attachments": [
{
"fallback": "Required plain-text summary of the attachment.",
"pretext": "Optional text that appears above the attachment block",
"image_url": "http://my-website.com/path/to/image.jpg"
}
]
}
Note: Please ensure that link for your image is available in public. You might not be able to use this approach to share if images are within the private network. In this case, you need to upload them to a site – like Amazon S3 – then use public URL of the site before sharing via Slack.
- Assuming performance test result charts are available in a public site…. Install this HTTP Request Plugin in Jenkins.
- Update the URL and Request Body as shown here
- We could see the chart in slack as soon as the Jenkins job is executed
Slack – File Upload:
When your images are not available in public, Slack provides a file upload API to upload images & other files.
- Go to this site – https://api.slack.com/methods/files.upload/test
- Generate a token for your account
- Upload a simple file in the preferred channel using the site.
- Make sure you receive the image in your slack channel.
- The easiest option is to invoke a CURL command in the below format to upload the file.
curl -F file=@BytesThroughputOverTime.png -F channels=#perf-test -F token=my-token https://slack.com/api/files.upload
- We can also use java/groovy – Lets create a simple java util to upload the images
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
public class SlackUtil {
public static void main(String[] args) throws ClientProtocolException, IOException {
String url = "https://slack.com/api/files.upload?token=mytoken&channels=mychannel";
String filePath = "BytesThroughputOverTime.png";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", new FileBody(new File(filePath))).build();
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
//check the response
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
}
- Once this is done, export this as a jar file – Invoke this as ANT target / place it in jenkins classpath – call this as part of post build actions.
Summary:
As part of Continuous Performance Testing process we run the performance test using Jenkins periodically. Only you, as a performance test engineer, mostly know the results. Other team member might not be aware of performance test results. Using JMeter command line tool, we can create beautiful charts and using Slack API we can share the results programmatically.
Happy Testing & Subscribe 🙂
Love reading your posts, Thanks for taking the time and putting these together..
Just an observation, but is there any reason for not using the slack upload plugin, which provides the ability to upload files into slack without having to hit the API?
I noticed that there was a slack upload plugin. I checked the source code here – https://github.com/jenkinsci/slack-uploader-plugin/blob/master/src/main/java/SlackUploader.java
They execute curl command in bash shell. So if you have a windows slave, this plugin would not work.
Ahh ok, didn’t look into that..