Problem with manual test run creation using REST

Wednesday, August 9, 2023
Avatar

I am trying to create a test run using the rest api. After reading the documentation I have made a little code but I don't know what I'm doing wrong because when I do the PUT, it doesn't work. I think it may be related to the dictionary that is sent in the request but I'm not sure. The code is the following:

import requests
from datetime import datetime

# Set up the API endpoint URLs
api_url = "https://xyz.spiraservice.net/Services/v6_0/RestService.svc"
project_id = 9  # Replace with your project ID
release_id = 807

# Credentials and login
username = "xyz"
api_key = "{xyz-xyz-xyz-xyz-xyz}"
auth = (username, api_key)

# Petition header
headers = {
    "Content-Type": "application/json"
}

# Create a new test run shell dictionary
new_test_run = [2361]

# Construct the test run shell
test_run_create_endpoint = f"{api_url}/projects/{project_id}/test-runs/create?release_id={release_id}"

# Make a POST request to create the test run shell
response_post = requests.post(test_run_create_endpoint, headers=headers, auth=auth, json=new_test_run)

# Check if the request was successful
if response_post.status_code == 200:  # HTTP 201 Created
    test_run = response_post.json()
    print(test_run)
    # if "TestRunId" in test_run:
    #     test_run_id = test_run["TestRunId"]
    #     print(f"Test run created successfully with ID: {test_run_id}")
    # else:
    #     print("Error: Test run ID not found in the response")
else:
    print(f"Error creating test run: {response_post.status_code} - {response_post.text}")

# Update the test run 0001-01-01T00:00:00
test_run_data = [
    {"TestRunId": 0,},
    {"ExecutionStatusId" : 1},
    {"EndDate" : datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000")}
]

endpoint = f"{api_url}/projects/{project_id}/test-runs?end_date={test_run_data[1]['EndDate']}"
response = requests.put(endpoint, headers=headers, auth=auth, json=test_run_data)

# Check if the request was successful
if response.status_code == 200:  # HTTP 201 Created
    test_run = response.json()
    print(test_run)
    # if "TestRunId" in test_run:
    #     test_run_id = test_run["TestRunId"]
    #     print(f"Test run created successfully with ID: {test_run_id}")
    # else:
    #     print("Error: Test run ID not found in the response")
else:
    print(f"Error creating test run: {response.status_code} - {response.text}")

I'm quite new in this so the code must be improved a lot. I just want to make the test run first.

 

Thanks in advance, 

 

Javier.

1 Replies
Wednesday, August 9, 2023
Avatar
re: jquesadag Wednesday, August 9, 2023

Could you please log a help desk ticket with this?

In general for a PUT you need to first do  a GET of the whole object, make any changes and then send the entire object back. You cannot just send back a partial fragment with a PUT.

Thanks

David

Spira Helps You Deliver Quality Software, Faster and With Lower Risk

And if you have any questions, please email or call us at +1 (202) 558-6885

 

Statistics
  • Started: Wednesday, August 9, 2023
  • Last Reply: Wednesday, August 9, 2023
  • Replies: 1
  • Views: 294