Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

How to update test case status in XRAY cloud through python?

I am writing a python script which will parse the results xml and create a dict for all test case names mapped to their "PASSED" or "Failed" status. 

I want to send request in jira xray to update execution status of these test cases?

is there any api in xray cloud that supports this or any other method to do that?

1 answer

0 votes
Marc Koppelaar
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Sep 20, 2023

HI @Priyanka Gupta 

Xray provides an API.

See the documentation around importing test execution results here

import requests
import json
import os

xray_cloud_base_url = "https://xray.cloud.getxray.app/api/v2"
client_id =  < provided this>
client_secret = "provided this"

# 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()

with open(xml_file_path, 'r') as file:
junit_xml_content = file.read()

root = ET.fromstring(junit_xml_content)

# Assuming component information is available in each <testcase> element
for testcase in root.iter('testcase'):
component = '"QA"' # Replace with actual component information
# Create a custom element for component and add it to the <testcase>
component_element = ET.Element("components")
component_element.text = component
testcase.append(component_element)

# Serialize the modified XML back to string
junit_xml_content = ET.tostring(root).decode()
print(junit_xml_content)
headers = {'Authorization': 'Bearer ' + auth_token, 'Content-Type': 'application/xml'}
# Send the POST request
response = requests.post(f'{xray_cloud_base_url}/import/execution/junit', headers=headers, data=junit_xml_content, params={'projectKey': 'DX', 'testExecKey': 'DX-79900'})

if response.status_code == 200:
print('Test execution results successfully imported.')
else:
print(f'Failed to import test execution results. Status code: {response.status_code}')
print(f'Response: {response.text}')
This code gives me this error : Failed to import test execution results. Status code: 400
Response: {"error":"Error creating issues in Jira! - components: Components is required."}
Firstly why it is creating new JIRA issue? also why it is failing
import requests
import json
import os

xray_cloud_base_url = "https://xray.cloud.getxray.app/api/v2"
client_id =  < provided this>
client_secret = "provided this"

# 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()

with open(xml_file_path, 'r') as file:
junit_xml_content = file.read()

root = ET.fromstring(junit_xml_content)

# Assuming component information is available in each <testcase> element
for testcase in root.iter('testcase'):
component = '"QA"' # Replace with actual component information
# Create a custom element for component and add it to the <testcase>
component_element = ET.Element("components")
component_element.text = component
testcase.append(component_element)

# Serialize the modified XML back to string
junit_xml_content = ET.tostring(root).decode()
print(junit_xml_content)
headers = {'Authorization': 'Bearer ' + auth_token, 'Content-Type': 'application/xml'}
# Send the POST request
response = requests.post(f'{xray_cloud_base_url}/import/execution/junit', headers=headers, data=junit_xml_content, params={'projectKey': 'DX', 'testExecKey': 'DX-79900'})

if response.status_code == 200:
print('Test execution results successfully imported.')
else:
print(f'Failed to import test execution results. Status code: {response.status_code}')
print(f'Response: {response.text}')
This code gives me this error : Failed to import test execution results. Status code: 400
Response: {"error":"Error creating issues in Jira! - components: Components is required."}
Firstly why it is creating new JIRA issue? also why it is failing

Is there a way that we can update same test execution rather than creating new one?

Like Marc Koppelaar likes this
Marc Koppelaar
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Sep 20, 2023 • edited

Hi @Priyanka Gupta 

What do you currently have in Jira?

Do you use the xray issue types Test and test execution?

You need to create a test, and a test has test executions. in the test execution the referenced test is set to passed via the API.

Based on the field configuration in your project it seems that the component field is required on creating an issue.

Edit:

Yes, specify in the API call the key of the test execution

In JIRA, i have test execution and test cases added to it. I am also passing component field.

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()

with open(xml_file_path, 'r') as file:
junit_xml_content = file.read()
headers = {'Authorization': 'Bearer ' + auth_token, 'Content-Type': 'application/xml'}
params = (('projectKey', 'DX'), ('testExecKey', 'DX-79900'), ('Components', '[{"name": "QA"}]'))
payload = {
"fields": {
"project": {
"key": "DX"
},
"summary": "Update Test execution",
"issuetype": {
"key": "DX-79900"
},
"components": [{"name": "QA"}],
}
}
# Send the POST request
response = requests.post(f'{xray_cloud_base_url}/import/execution/junit', headers=headers, data=junit_xml_content, params = params)


if response.status_code == 200:
print('Test execution results successfully imported.')
else:
print(f'Failed to import test execution results. Status code: {response.status_code}')
print(f'Response: {response.text}')

another way i tried: 

response = requests.post(f'{xray_cloud_base_url}/import/execution/junit', headers=headers, data=junit_xml_content, params = params)

Tried every way, but it gives me "Failed to import test execution results. Status code: 400
Response: {"error":"Error creating issues in Jira! - components: Components is required."}"

Marc Koppelaar
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Sep 21, 2023

Hi @Priyanka Gupta 

That means that the component field is mandatory on creating an issue in the project where you want to create the test execution issue.

You will need to check the configuration of the Jira project.

Check the Field Configuration Scheme or Workflow

But why it is trying to create the Test execution issue, when i am already passing the key.

And also, same component field i am using to create Test Cases and that is working.

Hi,

I am able to update existing test execution using this 

response = requests.post(f'{xray_cloud_base_url}/import/execution/junit', params=params, data=report_content, headers=headers).
But the issue is that it is creating new test cases in the same execution every time. How can i avoid that?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events