Stumped by 412 response when importing CI test results

BarryTice
Messages : 14
Inscription : lun. mai 09, 2022 7:10 pm

Stumped by 412 response when importing CI test results

Message par BarryTice »

I'm somewhat flummoxed by a 412 error while experimenting in preparation for having my CI report test runs to SquashTM.

Consider, please, this Python code:

Code : Tout sélectionner

#! /bin/python3

CID = 2  # A known-good ID of a campaign

import requests
import json
import constants
from datetime import datetime

USER = constants.CONSTANTS.USER
PASSWORD = constants.CONSTANTS.PASSWORD
G_AUTH = (USER, PASSWORD)

URL = constants.CONSTANTS.BASE_URL

DS = datetime.now().strftime("%y%m%d%H%M%S")

new_iteration = {"_type": 'iteration', 'name': f"Reported automation: {DS}", 'reference': DS, 'parent': {'_type': 'campaign', 'id': CID} }

url = URL + f"campaigns/{CID}/iterations"
response = requests.post(url, json=new_iteration, auth=G_AUTH)
if response.status_code != 201:
    print(f"Failed to make iteration: {response.status_code}")
    exit(1)
else:
    response = response.json()
    iteration_id = response['id']

url = URL + f'iterations/{iteration_id}/test-plan'
plan = {
    '_type': 'test-plan-item',
    'test_case': {
        '_type': 'scripted-test-case',
        'id': 353
    }
}
response = requests.post(url, json=plan, auth=G_AUTH)
if response.status_code != 201:
    print(f"Failed to connect first test case: {response.status_code}")
    exit(1)

plan['test_case']['id'] = 354
response = requests.post(url, json=plan, auth=G_AUTH)
if response.status_code != 201:
    print(f"Failed to connect second test case: {response.status_code}")
    exit(1)

report = {
    'tests': [
        {
            'reference': 'ZMSA-T-1',
            'status': 'SUCCESS'
        }, {
            'reference': 'ZMSA-T-2',
            'status': 'FAULURE'
        }
    ]
}

url = URL + f"import/results/{iteration_id}"
response = requests.post(url, json=report, auth=G_AUTH)

if response.status_code != 204:
    print(f"Failed to report results: {response.status_code}")
else:
    print("Apparent success")
This code successfully creates an iteration in the campaign and then connects two automated test cases (353 and 354) to the new iteration as test plan items. However it fails when making the last call to apply the test results to the test plan items, giving a 412 error, indicating that preconditions have failed.

I've been back through the documentation several times and have tried to trace the code, but I am coming up blank. Can anyone else see what I'm doing wrong?

Thanks.
Répondre

Revenir à « Pilotage de tests automatisés / Management of automated test execution »