PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How to extract test case results using Jira Rest API

 July 23, 2022     automated-tests, jira-rest-api, jira-xray, json, python-jira     No comments   

Issue

When I use the Rest API to extract the json file, I am able to view all the issues and fields. However, none of these fields contain any information about the tests run. I am able to see the tests and their result on my Jira software online. Could someone help me with trying to locate where I could get the information of the test cases and executions in the JSON file?


Solution

Xray for Jira Cloud, which is a different product from Xray for Jira server/datacenter, provides both a REST API and a GraphQL API. To obtain the test results associated with a given Test Execution, you need to use the GraphQL API.

First, you need to authenticate using the REST API and client id/secret pair, that will give you a token. Having the token, you can make the GraphQL request. You can either use the getTestExecution method, but it will require you to know the Test Execution issue id. Instead, you can use getTestExecutions which allows you to provide a JQL even if that JQL just returns a single Test Execution issue based on its issue key. In GraphQL you can specify what you want to return... like the "test" object, or the "testRuns" object. The "tests" object only provides info about the related Test issues and the respective Jira fields. In this case, what you want is the "testRuns" object instead.

I've built an example of a Python script using the gql graphql client library, doing either a simple request or a more detailed request. The simple may be good enough for what you need. You need to update the values for:

  • the client id and client secret used for authenticating the request
  • the Test Execution issue key that you want to obtain info from
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
#from gql.transport.requests import RequestsHTTPTransport
import requests
import json
import os

xray_cloud_base_url = "https://xray.cloud.getxray.app/api/v2"
client_id = os.getenv('CLIENT_ID', "215FFD69FE4644728C72182E00000000")
client_secret = os.getenv('CLIENT_SECRET',"1c00f8f22f56a8684d7c18cd6147ce2787d95e4da9f3bfb0af8f02ec00000000")

# endpoint doc for authenticating and obtaining token from Xray Cloud: https://docs.getxray.app/display/XRAYCLOUD/Authentication+-+REST+v2
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
auth_data = { "client_id": client_id, "client_secret": client_secret }
response = requests.post(f'{xray_cloud_base_url}/authenticate', data=json.dumps(auth_data), headers=headers)
auth_token = response.json()
print(auth_token)


xray_cloud_graphql_url = "https://xray.cloud.getxray.app/api/v2/graphql"

# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(
  url=xray_cloud_graphql_url,
  headers={'Authorization': f'Bearer {auth_token}'}
)


# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)

# detailed query to return info about the test runs, related tests.. 
detailed_query = gql(
    """
query
{
    getTestExecutions(jql: "key=CALC-141", limit: 1) {
        results{
          issueId
          jira(fields: ["key"])
    
          testRuns(limit: 100){
            results{
              id
              status{
                name
                description
              }
              comment
              testType{
                name
              }
              evidence{
                filename
              }
              defects
              executedById
              startedOn
              finishedOn
              assigneeId

              steps {
                  id
                  action
                  data
                  result
              }

              scenarioType
              gherkin
              examples {
                  id
                  status {
                      name
                      description
                  }
                  duration
              }

              unstructured
              
              customFields {
                  id
                  name
                  values
              }

              preconditions(limit:10) {
                results{
                    preconditionRef {
                        issueId
                        jira(fields: ["key"])
                    }
                    definition
                }
              }
              test {
                  issueId
                  jira(fields: ["key"])
                    projectId
                    testType {
                        name
                        kind
                    }
              }
              
            }
        }

    }
  }
}


"""
)


# simple query, to obtain the test runs, and their status, for a given Test Execution 
simple_query = gql(
    """
query
{
    getTestExecutions(jql: "key=CALC-141", limit: 1) {
        results{

          testRuns(limit: 100){
            results{

              test {
                  jira(fields: ["key"])
              }

              status{
                name
              }

            }
        }

    }
  }
}


"""
)

# Execute the query on the transport
result = client.execute(simple_query)
print(json.dumps(result, indent=4))

Side note: there is a GH repository with some Postman collections that you can import, with some examples that you can try out and adjust to your needs.



Answered By - Sérgio
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing