Reporting automation results to TestRail using Python

We have been hard at work integrating our Python based GUI automation framework (that uses the Page Object pattern) with a test case management tool for reporting purposes. In this post we will show you how to report the results of your automated checks to a visual test case management tool – TestRail. TestRail offers a free trial – so you can sign up for free and follow this post.

Why this post?

In most automated checks, we are able to judge the outcome as ‘pass’ or ‘fail’ by comparing the expected result to the actual result. Tracking, reporting and sharing the test results is important. Excel and email rarely scale and need a lot of maintenance. Continuous Integration servers are great for engineers – but non-technical stakeholders prefer something more visual. At Qxf2, we love TestRail – a visual test case management software. We got you started with TestRail in an earlier post. In this post, we are going to show you how automation can update test results directly on to TestRail.

Report automation results to TestRail using Python

We’ll need to perform the following steps to report test results automatically to TestRail
1. Get the TestRail API
2. Connect (authentication) to your TestRail instance
3. Write the logic to update test results to TestRail
4. Identify your test case id on TestRail
5. Modify your test script to report to TestRail
1. Get the TestRail API
TestRail provides a useful REST API for automated tests to directly report results on to TestRail. The API provides the functionality to authenticate API requests, provides seamless JSON encoding/decoding and has generic support for read and write requests. We will write some useful wrappers around TestRail’s official API binding for Python.
Before we proceed, please download the from here and follow the installation instructions.

2. Connect to your TestRail instance
We prefer separating any credentials we use into separate files. For this tutorial, we have created testrail.env that looks something like this:

[email protected]

We then created a file that wraps around TestRail’s API called In this file we implemented a method to connect to the TestRail instance.

TestRail integration
import dotenv,os,testrail,Conf_Reader
def get_testrail_client():
    "Get the TestRail account credentials from the testrail.env file"
    testrail_file = os.path.join(os.path.dirname(__file__),'testrail.env')
    #Get the TestRail Url
    testrail_url = Conf_Reader.get_value(testrail_file,'TESTRAIL_URL')
    client = testrail.APIClient(testrail_url)
    #Get and set the TestRail User and Password
    client.user = Conf_Reader.get_value(testrail_file,'TESTRAIL_USER')
    client.password = Conf_Reader.get_value(testrail_file,'TESTRAIL_PASSWORD')
    return client

Now we have an object that can connect to your TestRail instance!

3. Write the logic to update test results to TestRail
TestRail needs to know two things to go an update a test case result:
a. the test run id
b. the test case id

We need both because we can have multiple test runs for the same set of test cases. So our script should be able to update the test results into TestRail for a particular test run and specific test case. For this we need to know the test case id and the test run id. We implemented the update_testrail method. This method can be called from the test script with appropriate parameters so that the test results are recorded in TestRail for the specific case id and run id.

def update_testrail(case_id,run_id,result_flag,msg=""):
    "Update TestRail for a given run_id and case_id"
    update_flag = False
    #Get the TestRail client account details
    client = get_testrail_client()
    #Update the result in TestRail using send_post function. 
    #Parameters for add_result_for_case is the combination of runid and case id. 
    #status_id is 1 for Passed, 2 For Blocked, 4 for Retest and 5 for Failed
    status_id = 1 if result_flag is True else 5
    if run_id is not None:
            result = client.send_post(
                {'status_id': status_id, 'comment': msg })
        except Exception,e:
            print 'Exception in update_testrail() updating TestRail.'
            print 'PYTHON SAYS: '
            print e
            print 'Updated test result for case: %s in test run: %s with msg:%s'%(case_id,run_id,msg)
    return update_flag

NOTE: Test case ids stay constant across test runs in TestRail. However each test run is assigned a unique id. To automatically obtain your run id based on your test run name, you can implement something similar to this code snippet:

    def get_project_id(project_name):
        "Get the project ID using project name"
        client = get_testrail_client()
        projects = client.send_get('get_projects')
        for project in projects:
            if project['name'] == project_name: 
                project_id = project['id']
        return project_id
    def get_run_id(test_run_name,project_name):
        "Get the run ID using test name and project name"
        client = get_testrail_client()
        project_id = get_project_id(project_name)
            test_runs = client.send_get('get_runs/%s'%(project_id))
        except Exception,e:
            print 'Exception in update_testrail() updating TestRail.'
            print 'PYTHON SAYS: '
            print e
            return None
            for test_run in test_runs:
                 if test_run['name'] == test_run_name:
                     run_id = test_run['id']
            return run_id

4. Identify your test case id on TestRail
To obtain the test case id, simply visit your project on TestRail, navigate to Test Cases section and look for the integer in the ID column next to your test case.

TestRail case ids

5. Modify your test script to report to TestRail
Now you can modify your existing test scripts to report to TestRail. Here is a code sample that shows you how to use the method you wrote earlier:
Test Script

#Code to automate test case goes here
#Update TestRail
    case_id = 125 #Case id of your test case 
    if (result_flag):
        msg = "Successfully updated the example form"
        msg = "Failed to update the example form"

Hope this post helps you offload the manual task of updating the test results in TestRail! Get all this code for free in our open-sourced Python test automation framework based on the page object pattern. We strongly recommend you check it out!

Subscribe to our weekly Newsletter

View a sample

45 thoughts on “Reporting automation results to TestRail using Python

  1. Hello! Thank you for the very informative instructions! I use Robot framework to run my test cases. How will the example test case look like in this case?

  2. Thank you Vrushali for this amazing article. Is there a way we can avoid updating the script with the test case IDs? Can that somehow be done dynamically?

Leave a Reply

Your email address will not be published.