API Binding: Ruby - TestRail

API Binding: Ruby

The API binding for Ruby can be used to access TestRail’s API from Ruby. It provides the basic functionality to authenticate API requests, provides seamless JSON encoding/decoding and has generic support for read and write requests. Please see below for several examples on how to use the API binding.

The source code for the binding can be found on GitHub:

TestRail API binding for Ruby (ZIP)
The GitHub repository with the API bindings as ZIP download

Setup & installation

Please download the ZIP from the above location and extract it to a directory of your choice. The binding for Ruby can be located in the “ruby” sub-directory and is a single file (“testrail.rb”). Please copy this file to a directory of your application or project where “require” can find it. You can then use the API binding as follows:


require 'testrail'
 
client = TestRail::APIClient.new('http://{server-name}/testrail')
client.user = '..'
client.password = '..'
 
..

Please note: You should specify the same TestRail address for the API client you also use to access TestRail with your web browser (e.g., https://<your-name>.testrail.com/ or http://<server>/testrail/).

API Rate Limit

Please note that the API is rate-limited on TestRail Cloud to ensure optimal performance for all users and may throttle requests. TestRail might also return a 429 Too Many Requests response, which you are expected to handle. Such a response also includes a Retry-After header indicating how many seconds to wait before you are allowed to submit the next request.

To avoid rate limits on TestRail Cloud, try using bulk API endpoints (e.g. using as add_results_for_cases instead of add_results_for case), build a time delay into your API calls, or upgrade to TestRail Enterprise Cloud.

Rate limits for TestRail Cloud are as follows:

  • 180 Requests per instance, per minute for TestRail Cloud Professional subscriptions.
  • 300 Requests per instance, per minute for TestRail Cloud Enterprise subscriptions.

No API rate limits are built into TestRail Server installations.

Example: GET request

GET requests are used by read-only API methods and can be used to read or query data from TestRail. For example, the following code shows how to get the properties of a test case using the “get_case” API method:

client = TestRail::APIClient.new('http://{server}/testrail')
client.user = '..'
client.password = '..'
c = client.send_get('get_case/1')
puts c

The API binding provides a single method for GET requests named “send_get” which expects the name of the TestRail API method (here “get_case”) including any possible parameters (in this case the ID of the test case). The return value is a Ruby dict/hash and strings use the UTF-8 encoding.

An exception may be thrown in case of an error (connection issues, failed authentication, missing API arguments, etc.) and your code should be prepared to handle this.

Example: POST request

POST requests are used by write-based API methods and can be used to add or modify data. For example, the following code shows how to add a test result using the “add_result_for_case” API method:

..
r = client.send_post(
	'add_result_for_case/1/1',
	{ :status_id => 1, :comment => 'This test worked fine!' }
)
 
puts r
..

The “send_post” method has the same properties as “send_get” except that it uses POST instead of GET for the API requests. Like “send_get”, “send_post” expects the name of the TestRail API method (here “add_result_for_case”) including any possible parameters (in this case the IDs of the test run and case, respectively). The return value is a Ruby dict/hash. POST arguments can be added as second parameter as Ruby dict/hash (here the status ID and a comment). The expected encoding for strings is UTF-8.

An exception may be thrown in case of an error (connection issues, failed authentication, missing API arguments, etc.) and your code should be prepared to handle this.

Example: Attachment Request

When adding an attachment to TestRail or retrieving an attachment from TestRail, you can use our updated API bindings to make these requests.

When retrieving an attachment from TestRail, a download path and file name must be included in the request. This may overwrite a file of the same name, if it already exists. You can retrieve the existing file name with a ‘get_attachments_for_test’ or similar API method. If successful, response will contain the file path for the download:

r = client.send_get("get_attachment/1", "C:\\attachment.JPG")

When adding an attachment, the attachment path and filename must be included in the request:

r = client.send_post("add_attachment_to_result/1", C:\\screenshot.JPG")