Forum Discussion

mayonaes's avatar
mayonaes
New Member
17 days ago

Use Zephyr Scale Cloud API to add existing test cases to and newly created test cycle.

I am attempting to do this via powershell script in teamcity but I am getting a 404 when it attempts to add the testcase to the testcycle. I cannot for the life of me figure out why it is not working. any help is appreciated.

# -----------------------------
# Create test cycle
# -----------------------------

try {
    $createResp = Invoke-RestMethod -Uri $TestCycleEndpoint -Method POST -Headers $headers -Body ($body | ConvertTo-Json -Depth 5)
    $testCycleKey = $createResp.key
    Write-Host "✅ Created Zephyr Scale test cycle: $testCycleKey (linked to $($env:ZS_LINKED_ISSUE))"
} catch {
    $errResponse = $_.Exception.Response
    if ($errResponse -ne $null) {
        $reader = New-Object System.IO.StreamReader($errResponse.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Error "❌ Failed to create Zephyr Scale test cycle: $($_.Exception.Message)`nResponse: $responseBody"
    } else {
        Write-Error "❌ Failed: $($_.Exception.Message)"
    }
    exit 1
}

# -----------------------------
# Add mapped test cases to the cycle
# -----------------------------
$cycleTestCases = @()

foreach ($rxName in $TestCaseMapping.Keys) {
    $cycleTestCases += @{ testCaseKey = $TestCaseMapping[$rxName] }
}

$addCasesEndpoint = "https://api.zephyrscale.smartbear.com/v2/testcycles/$testCycleKey/testcases"
$addCasesBody = @{
    items = $cycleTestCases
}

try {
    Invoke-RestMethod -Uri $addCasesEndpoint -Method POST -Headers $headers -Body ($addCasesBody | ConvertTo-Json -Depth 5 -Compress)
    Write-Host "✅ Added $($cycleTestCases.Count) test cases into cycle $testCycleKey"
} catch {
    Write-Error "❌ Failed to add test cases into cycle ${testCycleKey}: $($_.Exception.Message)"
    exit 1
}

1 Reply

  • MisterB's avatar
    MisterB
    Icon for Champion Level 3 rankChampion Level 3

    I'm not a user of the API, so can't verify this before suggesting, but to add test cases to a test cycle, I think you need to use the Create Test Execution endpoint, and add test cases with an execution status of Not Executed.  This would mimic what happens when adding test cases to test cycles in the UI, because what actually happens behind the scenes is that a test execution is created - in Zephyr, a test case can have multiple test executions, so technically, when you add a test case to any test cycle you are actually creating a test execution record for that test case.

    I hope this helps - let me know if not?  And if you feel like posting your updated code when you have a solution that would be awesome for the community to have available!

    Good luck.