Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.

API - add_run and add_result_for_case


My colleagues (developers) have created a bunch of unit tests, for a project.
And I have been assigned as a test manager on it, and are using Testrail to build the test suites and cases for the manuel testing.

I would like to gather both the manuel and unit test, test result in testrail.

The unit tests are made with Nunit, wich gives me certain options to pull out the result of each unit test via the [Teardown] method, basically I put in this line:

TestRailAPIPost(String.Format("{0}", TestContext.CurrentContext.Result.Status), TestContext.CurrentContext.Test.FullName, 189, 6668);

The method looks like this:

public void TestRailAPIPost(string status, string comment, int testrun, int testcase)
Gurock.TestRail.APIClient client = APIClientCall();

        int sta_id;
        if (status == "Passed")
            sta_id = 1;
            sta_id = 5;
        var data = new Dictionary<string, object>
            //1 = Passed, 2 = Blocked, 4 = Retest, 5 = Failed
          { "status_id", sta_id },
          { "comment", comment }
        **JObject p = (JObject)client.SendPost("add_result_for_case/" + testrun + "/" + testcase, data);**

And this works.

The next step in my plan is to make the test run on the fly by using the “add_run” command, and then somehow get the result back so I can add that in the above JObject call.
This is not working for me.

First of I have a hard time figuring out if I’m using the correct project id. Am I correct in assuming that I get it from the URL:

Project id = 17 ?

If thats the case something else is wrong when I try and write the following:

JObject c = (JObject)client.SendPost(“add_run/” + 17, “blabla”);

I get the following error in Nunit:

TearDown : Gurock.TestRail.APIException : TestRail API returned HTTP 400 (“Invalid or incomplete JSON string in API request.”)

According to your documentation Error 400 is “Invalid or unknown project”

I know that the data needs to contain more than “blabla” but I need to make sure that I call the correct project before I create the data



Thanks for your posting. Yes, that’s one way of determining the project ID and the ID is also shown on the project overview page next to the project name (it’s written as P# and the API expects the numeric part only).

400 responses are often returned by TestRail’s API for invalid arguments (such as a unknown project) but this status code is also used for more generic errors such as the one you see. This is caused by the dummy text you submitted to SendPost and this method expects a standard object/array which is then serialized to JSON (just like the add_result_for_case call).

I hope this helps!



Exellent, thanks for the quick reply, that explains it abit, I will continue my work on it next week.


You are welcome and that’s great to hear!



Quick question

I’ve managed to get the “add_run” up and running, but is there anyway I can return the run id to my code, so that I can use it in my “add_result_for_case” function?



Yes, the new ID is returned as part of the HTTP response. This is also made available by the API bindings and you can see a full example on the respective binding page, e.g.:

add_run returns a JObject object and you can access the ID as follows:

JObject c = (JObject)client.SendPost("add_run/", ...);    
c["id"] ..



Exellent, I got it to work now, many thanks for the quick and good support


That’s great to hear and you are welcome! :smile: