'How to add a test run to ALM using REST API

I searched high and low trying to figure out how to create a test run using the REST API for ALM.

If anyone has found a better way then I would love to see it!

Just figured this out last night.

I used the tool called Postman to figure this out.

This is how to get your testcycl-id via the REST API. First go to the Test Lab page and create a test set and then add a test case (or multiple test cases) to it. In the execution grid you can add the columns for "Test ID" and "ID".

Test ID = test-id

ID = test-config-id

Next to the Execution Grid there is another button called, "Details". If you click that you can find the Test Set ID.

Test Set ID = cycle-id

Once you have the cycle-id and the test-id you can perform a GET and pull out the whole record for your test set that will include the testcycl-id.

GET https://SERVER/qcbin/rest/domains/DOMAIN/projects/PROJECT/test-instances?query={cycle-id[123];test-id[4567]}

Once you have this information you'll get back an xml with all the test instance details including the testcycl-id though in the xml it's called simply, "id".

You can then use that for creating a test run that both creates a test run on the Test Runs page and also updates a test case in a test set in the Test Lab page.

POST https://SERVER/qcbin/rest/domains/DOMAIN/projects/PROJECT/runs

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Entity Type="run">
<Fields>
<Field Name="test-config-id"><Value>8901</Value></Field>
<Field Name="cycle-id"><Value>123</Value></Field>
<Field Name="test-id"><Value>4567</Value></Field>
<Field Name="testcycl-id"><Value>THIS IS THE VALUE THAT YOU RAN THE GET TO FIND</Value></Field>
<Field Name="build-revision"><Value>1</Value></Field>
<Field Name="name"><Value>MyRun</Value></Field>
<Field Name="owner"><Value>johnsmith</Value></Field>
<Field Name="status"><Value>Passed</Value></Field>
<Field Name="subtype-id"><Value>hp.qc.run.external-test</Value></Field>
<Field Name="duration"><Value>5</Value></Field>
<Field Name="execution-date"><Value>2016-09-23</Value></Field>
<Field Name="execution-time"><Value>08:01:07</Value></Field>
<Field Name="status"><Value>Passed</Value></Field>
</Fields>
</Entity>

Best of luck to those of you that are looking for this!



Solution 1:[1]

The easiest way to add a test run to ALM using REST API is by putting a payload with status to a test instance. The way I have done it is using automation, which changes the status of my test instance to blocked, which automatically generates a new fast_run in test-runs module. Then my automated script changes the status of the newly created fast_run which ultimately changes the status of the test-instance it is linked to.

Here's the Ruby(With Rest-Client and Nokogiri gems) Code:

 test_instances_url = "http://ALM-SERVER/qcbin/rest/domains/#{@alm_domain_name}/projects/#{@alm_project_name}/test-instances"
    puts "Test Instance URL >> #{test_instances_url} "
    @report.puts "Test Instance URL >> #{test_instances_url} "
    test_instance_addition_doc = "
    <Entity Type=\"test-instance\">
      <Fields>
        <Field Name=\"order-id\">
          <Value>1</Value>
        </Field>
        <Field Name=\"test-id\">
          <Value>#{test_id}</Value>
        </Field>
        <Field Name=\"subtype-id\">
          <Value>hp.qc.test-instance.MANUAL</Value>
        </Field>
        <Field Name=\"cycle-id\">
          <Value>#{test_set_id}</Value>
        </Field>
      </Fields>
    </Entity>"

    puts "Test instance Addition XML  >> #{test_instance_addition_doc} "
    @report.puts "Test instance Addition XML  >> #{test_instance_addition_doc} "

    RestClient.post(URI.escape(test_instances_url), test_instance_addition_doc, :content_type => 'application/xml', :cookies => {:QCSession => @session_cookie, :LWSSO_COOKIE_KEY => @token} )
    new_test_instances_url = "http://ALM-SERVER/qcbin/rest/domains/#{@alm_domain_name}/projects/#{@alm_project_name}/test-instances?query={cycle-id[#{test_set_id}]}"
    puts "New Test Instances URL >>  #{new_test_instances_url}"
    @report.puts "New Test Instances URL >>  #{new_test_instances_url}"

    test_instance_id_doc = Nokogiri::XML(RestClient.get(URI.escape(new_test_instances_url), :cookies => {:QCSession => @session_cookie, :LWSSO_COOKIE_KEY => @token}))
    test_instance_id = test_instance_id_doc.xpath("//Field[@Name='status']/Value[text()='No Run']/../preceding-sibling::Field[@Name='id']/Value").text
    puts "Test Instance ID  >>  '#{test_instance_id}'  "
    @report.puts "Test Instance ID  >>  '#{test_instance_id}'  "

    new_test_instance_url = "http://ALM-SERVER/qcbin/rest/domains/#{@alm_domain_name}/projects/#{@alm_project_name}/test-instances/#{test_instance_id}"
    test_instance_update_doc  = "<Entity Type='test-instance'><Fields><Field Name='status'><Value>Blocked</Value></Field></Fields></Entity>"
    RestClient.put(URI.escape(new_test_instance_url), test_instance_update_doc, :content_type => 'application/xml', :cookies => {:QCSession => @session_cookie, :LWSSO_COOKIE_KEY => @token} )
    sleep(2)
    runs_doc = Nokogiri::XML(RestClient.get(URI.escape("http://ALM-SERVER/qcbin/rest/domains/#{@alm_domain_name}/projects/#{@alm_project_name}/runs?query={cycle-id[#{test_set_id}]}"),:cookies => {:QCSession => @session_cookie, :LWSSO_COOKIE_KEY => @token}))
    run_id = runs_doc.xpath("//Field[@Name='cycle-id']/Value[text()='#{test_set_id}']/../following-sibling::Field[@Name='id']/Value").last.text
    puts "Run ID  >>  '#{run_id}' "
    @report.puts "Run ID  >>  '#{run_id}' "

Solution 2:[2]

The way to create a new test execution in the test lab for a given test, is not easy and has to go through 2 API calls in sequence to succeed, and with precise constraints that I will sum up below.

  1. the first API call is a httpPost that will create a test run. But it will not be visible in the test lab ! -don't ask me why. It will be visible in the test execution history of the test only. Furthermore, the api call has, beyond the minimal mandatory fields (test id, cycle id , test set id and a few others...) A SPECIFIC TEMPORARY STATUS that needs to be put : "Not Completed".

  2. the second API call is a httpPut. the real test's status can be put here. This one will make the test result appear in the test lab (and update the test history execution detail).

This solution has been kindly described by mr lobster here : https://lobsterautomation.wordpress.com/2017/01/18/hp-alm-rest-api/ and it has all the details to run successfully. Please praise him for his help !

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bastien Gallienne