API Binding: .NET

The API binding for .NET can be used to access TestRail's API from .NET (for example, C# or VB.NET). It provides the basic functionality to authenticate API requests, provides seamless JSON and UTF-8 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 .NET (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 .NET can be located in the “dotnet” sub-directory. Please add the contained project to your application or project in Visual Studio. The API binding also has a separate, external dependency on the Json.NET library which can downloaded here:

http://james.newtonking.com/json

You can then use the API binding as follows:

using System;
using System.Collections.Generic;
using Gurock.TestRail;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
class Program
{
	static void Main(string[] args)
	{
		APIClient client = new APIClient("http://<server>/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/).

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_caseAPI method:

APIClient client = new APIClient("http://<server>/testrail/");
client.User = "..";
client.Password = "..";
JObject c = (JObject) client.SendGet("get_case/4");
Console.WriteLine(c["title"]);

The API binding provides a single method for GET requests named “SendGet” 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 simple object (an instance of either JObject or JArray) and provides easy access to the API response via indexers (very similar to how you can access values in a Dictionary).

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_caseAPI method:

..
var data = new Dictionary<string, object>
{
	{ "status_id", 1 },
	{ "comment", "This test worked fine!" }
};
 
JObject r = (JObject) client.SendPost("add_result_for_case/1/1", data);
..

The “SendPost” method has the same properties as “SendGet” except that it uses POST instead of GET for the API requests. Like “SendGet”, “SendPost” 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 simple object again (an instance of either JObject or JArray). POST arguments can be added as second parameter as serializable object, e.g. an instance of Dictionary<string, object> (here the status ID and a comment).

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.