testrail

How to get CaseIDs of Failed test cases in a test run using testrail API


This is my test run in test rail : enter image description here

How do I get the list of Case IDs of the test cases which are failed status through testrail API?

What I tried based on the documentation https://support.testrail.com/hc/en-us/articles/7077819312404-Results :

curl -s -u $TESTRAIL_EMAIL:$TESTRAIL_PW -X GET "$TESTRAIL_URL/index.php?/api/v2/get_results_for_run/$TESTRAIL_RUN_ID" | jq 'group_by(.test_id) | map(max_by(.created_on))' | jq '[.[] | select(.status_id != 1)]'

(Explanation : Get all results of a testrun. Group it by test_id. From Each group take the latest result only. Filter by status_id != 1 (ie, status is not pass))

Output :

[
  {
    "id": 966943,
    "test_id": 2559942,
    "status_id": null,
    "created_on": 1684909080,
    "assignedto_id": 531,
    "comment": null,
    "version": null,
    "elapsed": null,
    "defects": null,
    "created_by": 546,
    "custom_step_results": null,
    "attachment_ids": []
  },
  {
    "id": 967944,
    "test_id": 2559944,
    "status_id": 5,
    "created_on": 1684936836,
    "assignedto_id": null,
    "comment": null,
    "version": null,
    "elapsed": null,
    "defects": null,
    "created_by": 546,
    "custom_step_results": null,
    "attachment_ids": []
  }
]

But this results doesn't have the Case ID of the test cases. Also this doesn't include untested tests. Also the i am only able to filter with .status_id and not the actual status like Pass Failed Untested etc

Is there a way to achieve my need?


Solution

  • If you wish to utilize the 'get_results_for_run' endpoint, obtaining the Case IDs is not feasible since the current response format does not include the 'Case_id.'

    However, an alternative endpoint to retrieve the data you've submitted is available, which involves using the 'get_tests' endpoint.

    With the ‘get_tests’ endpoint, your request looks like this :

    GET index.php?/api/v2/get_tests/{run_id}

    and the response looks like the below :

    {
       "assignedto_id": 1,
       "case_id": 1,
       "custom_expected": "..",
       "custom_preconds": "..",
       "custom_steps_separated": [
           {
               "content": "Step 1",
               "expected": "Expected Result 1"
           },
           {
               "content": "Step 2",
               "expected": "Expected Result 2"
           }
       ],
       "estimate": "1m 5s",
       "estimate_forecast": null,
       "id": 100,
       "priority_id": 2,
       "run_id": 1,
       "status_id": 5,
       "title": "Verify line spacing on multi-page document",
       "type_id": 4
    }
    
    
    

    From here you will find the Case IDs for the tests that are available in the run and you can further filter the response based on the status_id.

    For example:

    GET index.php?/api/v2/get_tests/{run_id}&status_id=5

    This filter will refine the results to include tests with the status "Failed".

    Kindly be aware that in the TestRail API, it's not feasible to filter tests based on the status name (e.g., Pass, Fail). We must always employ the 'status_id' for filtering purposes.

    Reference : https://support.testrail.com/hc/en-us/articles/7077935129364-Statuses#getstatuses