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

Add Test Run results in a Test Plan

I am trying make a module which programmatically add results to a test run with results inside a test plan. The test run gets created but I don’t see the results getting posted.

How can I combine the apis

add_plan_entry
add_results_for_cases

to add a test run in test plan and also post results.

In current working process, I have a results array in hand which I used to add results in a newly created run using add_run. and then I would post results usingadd_results_for_cases. This is working probably because I have correct request payloads. Now I am trying to change the process to add runs inside a Testplan so that I don’t clutter the test runs section in the UI.

Any suggestions?

The key is to get the entry ID AND the Run ID in the plan at the time you add the Run to the Plan.

Reference the API endpoint get_plan() for a full desc of the data returned from the API endpoint add_plan_entry():

http://docs.gurock.com/testrail-api2/reference-plans#get_plan

To be clear add_plan_entry() ONLY returns the entry part of the plan, which makes getting these IDs easy at that time. But the get_plan() doc shows a complete Plan with entries and runs.

There you see an “entries” list which contains a “runs” list. This run list will contain a json object/hash/dict for each Run in the Plan. It is the id in each of these objects you need for add_result_for_case()

Please note one will have to loop thru the Runs to find the ID you want. Each Run has a “name” and “config_id” field to assist with this.

It is almost like Test Plans were an add on after Runs so the API is not as clean in this area. :stuck_out_tongue:

Here is example code from when I add the first Run to a Plan:

if self.run_id is None:
    # first test cases so add to Plan a test Run entry with these TR Case IDs 
    try:
        resp = self.testrail.add_plan_entry(self.plan_id, self.testsuite_id, self.run,
                case_ids=tr_case_ids, include_all=False, assignedto_id=self.user_id)
    except TestRailAPIError as e:
        self.logger.log('LISTENER FATAL ERROR: add plan entry error: [{}: {}]\n'.format(e.code, e.err
        self.signal_quit()

    # TR api response is just the entry in the Plan so this is the easiest time to
    # get the entry ID and Run ID.  Note this assumes for now that TR Configurations
    # are not used so there is only one Run in the Plan entry
    self.run_id = resp['runs'][0]['id']
    self.entry_id = resp['id']

The focus is on the last part:

TR api response is just the entry in the Plan so this is the easiest time to
get the entry ID and Run ID. Note this assumes for now that TR Configurations
are not used so there is only one Run in the Plan entry
self.run_id = resp[‘runs’][0][‘id’]
self.entry_id = resp[‘id’]

The entry_id is used if you want to update the Plan again for example in update_plan_entry()

The run_id is used in add_result_for_case() as you already know from using just Runs.

Here is the entry from the docs. Look below for <<<<<<<<<<<< THIS IS WHAT YOU NEED

{
	"assignedto_id": null,
	"blocked_count": 2,
	"completed_on": null,
	"created_by": 1,
	"created_on": 1393845644,
	"custom_status1_count": 0,
	"custom_status2_count": 0,
	"custom_status3_count": 0,
	"custom_status4_count": 0,
	"custom_status5_count": 0,
	"custom_status6_count": 0,
	"custom_status7_count": 0,
	"description": null,
	"entries": [
	{
		"id": "3933d74b-4282-4c1f-be62-a641ab427063",
		"name": "File Formats",
		"runs": [
		{
			"assignedto_id": 6,
			"blocked_count": 0,
			"completed_on": null,
			"config": "Firefox, Ubuntu 12",
			"config_ids": [
				2,
				6
			],
			"custom_status1_count": 0,
			"custom_status2_count": 0,
			"custom_status3_count": 0,
			"custom_status4_count": 0,
			"custom_status5_count": 0,
			"custom_status6_count": 0,
			"custom_status7_count": 0,
			"description": null,
			"entry_id": "3933d74b-4282-4c1f-be62-a641ab427063",
			"entry_index": 1,
			"failed_count": 2,
			"id": 81,  <<<<<<<<<<<< THIS IS WHAT YOU NEED
			"include_all": false,
			"is_completed": false,
			"milestone_id": 7,
			"name": "File Formats",
			"passed_count": 2,
			"plan_id": 80,
			"project_id": 1,
			"retest_count": 1,
			"suite_id": 4,
			"untested_count": 3,
			"url": "http://<server>/testrail/index.php?/runs/view/81"
		},
		{
			..
		}
		],
		"suite_id": 4
	}
	],
	"failed_count": 2,
	"id": 80,
	"is_completed": false,
	"milestone_id": 7,
	"name": "System test",
	"passed_count": 5,
	"project_id": 1,
	"retest_count": 1,
	"untested_count": 6,
	"url": "http://<server>/testrail/index.php?/plans/view/80"
}

thanks so much. this helps

1 Like