OiO.lk Blog python Link defect to a test execution via API in Python
python

Link defect to a test execution via API in Python


I was trying to create a Python script which can update test execution status and link defect in Zephyr. However, the script can change the test execution status but cannot link defect.

This is my code snippet.

def read_and_update_from_excel(file_path):
    user_input = pd.read_excel(file_path, sheet_name="Sheet1")
    lookup = pd.read_excel(file_path, sheet_name="Lookup Value")

    # Create a mapping of status text to code
    status_code_map = dict(zip(lookup['Status'], lookup['Code']))

    BASE_URL = 'https://prod-api.zephyr4jiracloud.com/connect'
    RELATIVE_PATH = '/public/rest/api/1.0/executions'
    CANONICAL_PATH = f'POST&{RELATIVE_PATH}&'

    for index, row in user_input.iterrows():
        execution_id = str(row['ExecutionId'])  # Ensure ID is a string
        status_text = row['Status']
        defects = row['Defect Link (SR)']

        # Map status to code
        status_code = status_code_map.get(status_text)

        if status_code is None:
            print(f"Invalid status: {status_text} for Execution ID: {execution_id}")
            continue

        # Generate JWT token
        token = generate_jwt_token(CANONICAL_PATH)
        print("tokennn: ", token)
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'JWT {token}',
            'zapiAccessKey': access_key
        }

        # Prepare the data payload
        data = {
            "executions": [execution_id],  # Ensure it's a list of strings
            "status": status_code,
            "clearDefectMappingFlag": False,
            "testStepStatusChangeFlag": False,
            "stepStatus": -1,
            "defects": [{"key": defect.strip()} for defect in defects.split(',')]  # Example : 
        }


        # Convert the data to JSON and print for debugging
        json_data = json.dumps(data, indent=2)  # Pretty print for readability
        print("Request Payload:")
        print(json_data)

        # Send the API request
        response = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, data=json_data)

        # Print the response for debugging
        print(f"Execution ID: {execution_id}, Status: {status_text}, Defects: {defects}")
        print(f"Response: {response.status_code} - {response.text}")

        if response.status_code == 200:
            print(f"Successfully updated Execution ID: {execution_id}")
        else:
            print(f"Failed to update Execution ID: {execution_id}")

        BASE_URL = 'https://prod-api.zephyr4jiracloud.com/connect'
        RELATIVE_PATH = '/public/rest/api/1.0/executions/linkdefects'
        CANONICAL_PATH = f'POST&{RELATIVE_PATH}&'

        data = {
            "executions": [execution_id],
            "defects": [{"key": defect.strip()} for defect in defects.split(',')]
        }

        token = generate_jwt_token(CANONICAL_PATH)
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'JWT {token}',
            'zapiAccessKey': access_key
        }

        response = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, data=json.dumps(data))
        print(f"Response: {response.status_code} - {response.text}")

Example of the real payload when run the code

{
  "executions": [
    "xxx-xxx-xxx"
  ],
  "status": 2,
  "clearDefectMappingFlag": false,
  "testStepStatusChangeFlag": false,
  "stepStatus": -1,
  "defects": [
    {
      "key": "XX-350520"
    }
  ]
}

The code is working – got status 200.
The status in Test Execution was changed but the defect I’ve input won’t link to the Test Execution.



You need to sign in to view this answers

Exit mobile version