The API binding for PHP can be used to access TestRail’s API from PHP. 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 the TestRail API bindings GitHub repository:
TestRail API binding for PHP (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 PHP can be located in the “php” sub-directory and is a single file (“testrail.php”). Please copy this file to a directory of your application or project where “require/include” can find it. You can then use the API binding as follows:
require 'testrail.php';
$client = new TestRailAPIClient('http:///testrail/');
$client->set_user('..');
$client->set_password('..');
..
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.
Note: 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 = new TestRailAPIClient('http:///testrail/');
$client->set_user('..');
$client->set_password('..');
$case = $client->send_get('get_case/1');
var_dump($case);
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 PHP key/value array and strings use the UTF-8 encoding.
An exception of type TestRailAPIException 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:
..
$result = $client->send_post(
'add_result_for_case/1/1',
array(
'status_id' = 1,
'comment' = 'This test worked fine!'
)
);
var_dump($result);
..
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 PHP key/value array. POST arguments can be added as second parameter as key/value array (here the status ID and a comment). The expected encoding for strings is UTF-8.
An exception of type TestRailAPIException 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:
$result = $client->send_get("get_attachment/1", "C:\\attachment.JPG");
When adding an attachment, the attachment path and filename must be included in the request:
$result = $client->send_post("add_attachment_to_result/1", "C:\\screenshot.JPG");