How add test results to slack notification ?
Thank you for sharing your question on our forum!
There is no default functionality to accomplish this with Slack Orb, but you can get the test information via our api and then pass it to the Slack orb inside of a custom template.
It would look something like this:
- In a step after your test results are uploaded but before the Slack notification is sent, you can send a request to CircleCI API to get the test meta data.
- Then you can parse out the information you need (such as number of passing vs failed tests) from the json response, and then save that in an environment variable.
- Lastly, you can then use that variable in your custom Slack template to show the data in your Slack notification.
For the first step, you could send an api request like the following (example using curl):
TEST_RESULTS=$(curl --request GET \ --url https://circleci.com/api/v2/project/gh/orgname/projectname/$CIRCLE_BUILD_NUM/tests \ --header 'circle-token: $CIRCLECI_TOKEN' | jq)
You will need to hardcode the org name and project name, but the environment variable
$CIRCLE_BUILD_NUM will give you the number of the job.
$CIRCLECI_TOKEN also needs to be a personal api token.
This will store the test results from that job in an environment variable called
Following that, you can do additional parsing with a tool like
jq to get the number of failed and passed tests.
Lastly, you will need to store that data in an environment variable. Due to how steps work in CircleCI jobs, you will also need to pass the variable to
Assuming the test text you want to send in your Slack notification from above is in a variable called
FORMATTED_TEST_DATA, you could do it like this:
- run: command: echo 'export SLACK_TEST_DATA=$FORMATTED_TEST_DATA' >> $BASH_EN
Lastly, you could use the reference
$SLACK_TEST_DATA in your custom Slack orb template.
More information on the Slack orb can be found on the following page in our documentation:
I know this was a lot of information, but if you would like me to delve deeper into it, or if you have any additional questions, please feel free to ask.
thank you for your solution for me.
I solve it in different way by creating result file during the test and then I read the file and sending to slack message.