How to filter the specific testcases from Testrail suite and add those into testrun

665 Views Asked by At

I'm implementing the script to integerate the Testcomplete and Testrail to update the results. I have successfully created the Testrun and can add the specific testcases to testrun by giving the caseIds. I have used Testrail API for this.

Qs: Now I want to add only specific testcases to testrun which is marked as "can_be_automated = yes" in the testrail suite, I stuck up on this part. Implemented some script for this but it is not taking up the specific testcases. Filtering "can_be_automated = yes" is not working. Using Javascript

Can someone help me on this please? If anyone have script, please share it will be helpful

Thanks in advance

1

There are 1 best solutions below

1
TGavan On

Based on the description of your problem, it can be divided into two distinct tasks:

In your scenario, you have a test suite, and you want to identify particular test cases within this suite that have been marked with the customized filed "can_be_automated = true" Once you've identified these specific test cases, the second part of your problem involves adding them to an already existing test run.

For the first part, to filter and retrieve the specific test cases from your test suite, you can utilize the 'get_cases' API endpoint. The corresponding API query would appear as follows:

GET index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}

Upon executing this query, the response you receive will be in JSON format. Within this JSON response, you can find the custom field named "custom_can_be_automated".

"cases":[
      {
         "id":22478,
         "title":"Test Case Steps (Text)",
         "section_id":2347,
         "template_id":1,
         "type_id":5,
         "priority_id":4,
         "milestone_id":null,
         "refs":null,
         "created_by":36,
         "created_on":1691415817,
         "updated_by":36,
         "updated_on":1692281184,
         "estimate":null,
         "estimate_forecast":null,
         "suite_id":196,
         "display_order":4,
         "is_deleted":0,
         "case_assignedto_id":null,
         "custom_automation_type":6,
         "custom_can_be_automated":true,
         "custom_preconds":"Test Case Step *TEXT* Precondition",
         "custom_steps":"%Var3 \r\n%Var4\r\n",
         "custom_testrail_bdd_scenario":"",
         "custom_expected":" ![](index.php?\/attachments\/get\/d8d1d9e0-fe4c-43c8-b894-dfd83d9c160c) ",
         "custom_steps_separated":null,
         "custom_mission":null,
         "custom_goals":null,
         "comments":[
            
         ]
      },
      {
         "id":22494,
         "title":"Automated Checkout",
         "section_id":2347,
         "template_id":9,
         "type_id":3,
         "priority_id":3,
         "milestone_id":null,
         "refs":null,
         "created_by":36,
         "created_on":1691679382,
         "updated_by":32,
         "updated_on":1694640912,
         "estimate":null,
         "estimate_forecast":null,
         "suite_id":196,
         "display_order":5,
         "is_deleted":0,
         "case_assignedto_id":null,
         "custom_automation_type":0,
         "custom_can_be_automated":false,
         "custom_preconds":null,
         "custom_steps":null,
         "custom_testrail_bdd_scenario":"",
         "custom_expected":null,
         "custom_steps_separated":null,
         "custom_mission":null,
         "custom_goals":null,
         "comments":[
            
         ]
      },

Based on the provided response, you can employ a filter to extract the case IDs based on the 'custom_can_be_automated' attribute.To exemplify the API calls, I'll use the well-known "curl" utility ahead; then you can adapt these to your Javascript environment, where you may be using specific libraries for performing the HTTP request. You can achieve this by using the following curl with the jq command.

curl -H "Content-Type: application/json" -u "$TESTRAIL_EMAIL:$TESTRAIL_PASS" "$TESTRAIL_URL/index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}" | jq '[.[] | select(.custom_can_be_automated == true)]'

This curl command will fetch the relevant cases based on the 'custom_can_be_automated' attribute.

After obtaining all of these case IDs, you can proceed to add them to the test run using the 'update_run' endpoint. The request for this operation appears as follows:

POST index.php?/api/v2/update_run/{run_id}

The request body should be structured as shown below: { "include_all": true, "case_ids": [1, 2, 3, 5, 8] }

The POST request with “curl” utility looks like below:

curl -X POST   "https://$TESTRAIL_URL/index.php?/api/v2/update_run/{run_id}" 
-H "Content-Type: application/json"  
-u "$TESTRAIL_EMAIL:$TESTRAIL_PASS" 
-d '{"include_all": true, "case_ids": [1, 2, 3, 5, 8]}'

Once the cases have been successfully added, you will receive a response code of 200 to confirm the operation's success.

Links to the endpoints

https://support.testrail.com/hc/en-us/articles/7077874763156-Runs#updaterun https://support.testrail.com/hc/en-us/articles/7077292642580-Cases#getcases