TestRail integration with Robot Framework
By: Rahul Mishra
Tags: Testrail, API, Robot Framework, Python, Integration

Integrating TestRail with Robot Framework is a pretty simple and straightforward task. In terms of dependencies, in addition to a TestRail account and Robot Framework, all you need is

Requests Library

Once the TestRail account is created, the first action should be enabling the API. This can be done in Site Settings which is under Administration tab. Another thing is creating an API key which comes under My Settings tab; though this step is optional but it’s highly recommended.

Before moving ahead with the script, create some test cases and add a test run, if it hasn’t been done yet.

In this article, three API methods are discussed primarily because they are the ones most prominently used. Let’s move ahead with the first method, Get Case, which should fetch you all the details of the given test case.

On the scripting side, two files need to be created, one is a python file where the API methods will be there and another is a .robot file where the test case will be created.

For Get Case, the following method needs to be created.

import requests CONTENT_TYPE_HEADER = {'Content-Type': 'application/json'} class TestRailRequest(object): def __init__(self, url, username, apikey):

self.url = url

self.username = username

self.apikey = apikey def get_case(self, testrailid):

self.testrailid = testrailid

headers = CONTENT_TYPE_HEADER params = (

('/api/v2/get_case/{}'.format(self.testrailid), ''),

) response = requests.get(self.url,

headers=headers,

params=params,

auth=(self.username, self.apikey)) return response.text

In the above code, the get_case method fetches the details of the test case with the given testrailid. The testrailid is the id of the test case defined in TestRail. Now, this get_case method can be used as a keyword in your .robot framework.



Library TestRailRequest.py *** Settings ***Library TestRailRequest.py https:// url user_email apikey *** Test Cases ***

Test Case 1:

${data} Get Case 1

Log to Console ${data}

Since TestRailRequest class takes three arguments, these three arguments need to be passed while importing the TestRailRequest.py in the .robot file.

In the same manner, one can also add methods for post request. Let’s take a look at a method that allows modifying the test results in TestRail.

def add_result(self, testrailid, status_id):

self.testrailid = testrailid

self.status_id = status_id headers = CONTENT_TYPE_HEADER params = (

('/api/v2/add_result/{}'.format(self.testrailid), ''),

) data = '{ "status_id": %d }' % self.status_id response = requests.post(self.url,

headers=headers,

params=params,

data=data,

auth=(self.username, self.apikey))

print(response.text) return response

While calling this method from .robot file, user has to provide the both the test_id and status_id to make the modification. Status id range from 1 to 5 and each correspond to a specific status.

status_id 1: Pass

status_id 2: Blocked

status_id 3: Untested

status_id 4: Retest

status_id 5: Fail

In a similar manner, other things can be done on the TestRail through the API, such as adding comments, assigning tests to user, adding attachments, and so on.