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 testrail.py 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:
testrail.env
TESTRAIL_URL=https://.testrail.com [email protected] TESTRAIL_PASSWORD=test
We then created a file that wraps around TestRail’s API called TestRail.py. 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: try: result = client.send_post( 'add_result_for_case/%s/%s'%(run_id,case_id), {'status_id': status_id, 'comment': msg }) except Exception,e: print 'Exception in update_testrail() updating TestRail.' print 'PYTHON SAYS: ' print e else: 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() project_id=None projects = client.send_get('get_projects') for project in projects: if project['name'] == project_name: project_id = project['id'] #project_found_flag=True break return project_id def get_run_id(test_run_name,project_name): "Get the run ID using test name and project name" run_id=None client = get_testrail_client() project_id = get_project_id(project_name) try: 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 else: for test_run in test_runs: if test_run['name'] == test_run_name: run_id = test_run['id'] break 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.
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" else: msg = "Failed to update the example form" Test_Rail.update_testrail(case_id,test_run_id,result_flag,msg=msg) |
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!
My journey as a tester started at Sun Microsystems (now Oracle). I was part of the testing and sustaining team for the Portal Server and Identity Management products. My first assignment was to test the Rewriter module. I enjoyed understanding the big picture, writing test cases, finding bugs and sometimes suggesting the fix too! I was hooked onto testing. Testing felt natural and intuitive to me. I am technically inclined and can write automation in Java, C++, Perl and Python. I am well versed with SilkTest, Selenium, Appium and Selendroid. I am a Computer Science graduate from BITS-Pilani. I love travelling and listening to music.
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?
Hi,
Anita, you can refer to this python library https://pypi.org/project/robotframework-testrail/
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?
Hi, since the Testcase ID is generated and picked up from TestRail, we cannot entirely fetch it dynamically. However, we have updated the code to place all the TestCase IDs in a conf file and fetching it from there in the code. Please refer to our github repo for complete code – https://github.com/qxf2/qxf2-page-object-model
I found this library Behave-testrail-reporter (https://giters.com/rklyne/behave-testrail-reporter) but I cant’t running propertly
Hello,
Thanks for your details explanation. Am using PyTest Framework for Automation. Is there a way to send TestCases Results to TestRails using TestCase ID & Run ID? We ave already created TestRuns in TestRails just passing the RunID from the command line how can we parse the TestCase Results to TestRails using PyTest?
Hello Ravi Tej,
We have another blog which is Reporting test result to TestRail using test case id and the test run id
Please refer the blog for more details
https://qxf2.com/blog/reporting-to-testrail-using-python/
Hi. Can you please tell me how to add an attachment along with the “send_post” method?
Hello,
Please look at official test rail API
https://support.gurock.com/hc/en-us/articles/7077135088660#01G68HT7AY2V00PXJJXZ3EAVDR