Overview

Skill Level: Any Skill Level

Slack is a communication tool and widely used for all team/project communications. One of the best way to post test results is to slack so that all the stack holders can watch the test results in real-time.

Ingredients

  • Slack
  • Automation (written in any language)
  • Basic knowledge of python (python is used in this recipe)
  • CICD Tools (Jenkins or GoCD) - CICD workflow is used as example and below code can be used with simple automation as well.

Step-by-step

  1. Ideal High Level CICD workflow with integrated automation

    Typical example for the High Level CICD workflow where automation is integrated. On each code commit/deployment, automation is trigerred and results will be posted to configured slack channel.

     

    Screenshot-2021-01-05-at-3.59.02-PM

  2. Steps

    Pre-requisites :

    a. “Slack BOT” to upload the files and

    b. “Incoming Webhooks” to post the results.

    If you have the slack admin access, both can be created with simple steps. If not, you need to request slack admin for help.

    Steps for “Slack BOT” :

    1) Ceate a “Slack BOT”, note down the API Token which is provided with Slack BOT.
    2) Make a post API call with API Token and json body[required information] which will post the test results to the mentioned Slack channel

    slack_bot 

     

     

    Steps for “Incoming Webhooks” :

    1) Configure “Incoming Webhooks”, note down the URL which is provided with “Incoming Webhooks”
    2) Make a post API call with URL which will upload the reports to the mentioned Slack channel.

     

    webhook

  3. Code Snippet to post test results to Slack

     The below code is specific to the sample file TestSuite.txt. You can generate such sunmmary with any automation script and use the below code to post it on slack. This is sample code with which you read and post the test summary. Results can be posted with more details including the Failure details, logs, test case summary.

     

    Sample TestSuite.txt

    ==============================

    ——————————————————————————-
    Test set: TestSuite
    ——————————————————————————-
    Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 63.54 sec – in TestSuite

    ==============================

     

     
    Note : Filepath=’target/TestSuite.txt’

     

    def post_auto_result(channel,Filepath):

    url = <webhook-url>
    titleUI=””
    testexec=”Total Time Taken for Execution : “
    with open(Filepath) as fp:

    line = fp.readlines()
    print (len(line))
    testf=””
    totalt=””
    totalp=””

    for i in range(0, len(line)):

    if line[i].__contains__(‘Failures’):

    j = re.search(‘Skipped: (\d+)’, line[i])
    tests= j.group(1)
    m = re.search(‘Failures: (\d+)’, line[i])
    testf= m.group(1)
    n = re.search(‘Tests run: (\d+)’, line[i])
    totalt=n.group(1)
    testp= str(int(totalt)-int(testf)- int(tests))
    o= re.search(‘Time elapsed: (\d*\.\d{2})’, line[i])
    testexec=”Total Time Taken for Execution : ” +o.group(1)

    titleUI = “UI Smoke Test Results” + “\n”

    execSummary = “Total Tests: ” +str(totalt)+ “; Passed: “+str(int(testp))+”; Failed: “+str(testf)+”; Skipped: “+str(tests)+”\n” +testexec+” sec”

    body = {
    “channel”: channel,
    “username”: “CBSCAMBot”,
    “icon_emoji”: “:mega:”,
    “attachments” :[

    {
    “pretext”: titleUI,
    “color”: “#29343c”
    },
    {
    “color”: “#36a64f”,
    “title”: “Passed Tests”,
    “text”:testp
    },
    {
    “color”: “#f65109”,
    “title”: “Failed Tests”,
    “text”:testf
    },
    {
    “color”: “#607D8B”,
    “title”: “Skipped Tests”,
    “text”:tests
    },
    {
    “color”: “#29343c”,
    “title”: “Test Execution Summary\n”,
    “text”:execSummary
    }
    ]

    };

    resp= requests.post(url, json.dumps(body), headers={‘accept’: ‘application/json’, ‘Content-Type’: ‘application/json’})
    return resp

     

     

     

  4. Code Snippet to upload test reports to Slack

    Note : In the below code snippet, file “testreports.zip” is posted to slack.

     

    def log_upload():

    channel = ‘#channel-name’
    token = <token>
    requests.post(

    url=’https://slack.com/api/files.upload’,
    data={‘channels’: channel, ‘token’: token},
    files={‘file’: open(‘testreports.zip’, ‘rb’)})

  5. Slack Test Report

    Note: We can either send the test result summary or the individual test results based on the project requirement.

     

    Slack_Report

  6. Reference

    https://api.slack.com/messaging/webhooks

    https://api.slack.com/methods/files.upload

     

Join The Discussion