Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Priyanka Gupta September 19, 2023

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 - Devoteam
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.
September 20, 2023

HI @Priyanka Gupta 

Xray provides an API.

See the documentation around importing test execution results here

Priyanka Gupta September 20, 2023
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
Priyanka Gupta September 20, 2023
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
Priyanka Gupta September 20, 2023

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

Like Marc - Devoteam likes this
Marc - Devoteam
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.
September 20, 2023

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

Priyanka Gupta September 20, 2023

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}')
Priyanka Gupta September 20, 2023

another way i tried: 

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

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 - Devoteam
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.
September 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

Priyanka Gupta September 21, 2023

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.

Priyanka Gupta September 22, 2023

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