【问题标题】:Is it possible to execute Azure Test Plans using Rest api Powershell Script是否可以使用 Rest api Powershell 脚本执行 Azure 测试计划
【发布时间】:2021-10-11 02:47:49
【问题描述】:

我是 Rest api 和 Powershell 脚本的新手。我想知道我们是否可以使用 Powershell 脚本执行 Azure 测试计划。 我的计划是编写一个 powershell 脚本来在我的项目中执行一组现有的测试计划。然后在 Pipeline 中使用该 powershell 脚本并安排管道在特定时间运行。 请帮我。 我知道我们可以使用 Rets api 获取测试计划列表,是否也可以执行它们?

【问题讨论】:

    标签: powershell azure-pipelines rest azure-devops-rest-api


    【解决方案1】:

    测试计划不支持运行,只能执行测试点。

    如果您想在测试计划中使用Rest API执行自动测试,您可以参考以下步骤:

    以下是以下步骤:

    Step1:您可以在 Release Pipeline 中定义一个变量。

    Step2:在VSTest Task中添加这个变量:

    注意:测试运行的id需要和release一一对应,然后测试运行的状态才会更新。

    这是一个例子:

    param (
        [string]$token="",
        [string]$collection="",
        [string]$projectName ="",
        [int] $planIdStatic =947 ,
        [int] $suiteIdStatic =1086 ,
        [int] $testcaseID =79,
        [int] $releasedefinitionid =96,
        [int] $buildid =62903
    
    
    )
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $token)))
    $response = Invoke-RestMethod "https://dev.azure.com/$collection/$projectName/_apis/test/plans?api-version=5.0" -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
    foreach( $val in $response.value)
    {
    
    #PLAN ID 
    Write-Host $val.id
     # $planId = [convert]::ToInt32($val.id)
     [int] $planId = $planIdStatic
      $suites = "https://dev.azure.com/$collection/$projectName/_apis/test/plans/$planId/suites?api-version=5.0"
     $listofSuites = Invoke-RestMethod $suites -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
     #define Suite ID
    
     [int] $suiteId = $suiteIdStatic
     $suitename = "https://dev.azure.com/$collection/$projectName/_apis/test/plans/$planId/suites/$suiteId/points?api-version=5.0"
     $listofSuites = Invoke-RestMethod $suitename -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
    
    
     #Define TestCaseID
    
     [int] $tcID = $testcaseID
    
     $tc = "https://dev.azure.com/$collection/$projectName/_apis/test/plans/$planId/suites/$suiteId/points?testCaseId=$tcID&api-version=5.0"
     $testcaseapi = Invoke-RestMethod $tc -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
     
     #Define PointID
    
    [int] $pointID =    $testcaseapi.value[0].id 
     $runName = "Test RUN Setup"
    
     #PayLoad
    
     $payload = @"
     
     {
       "name":"test",
        "automated":true,
        "pointIds":[$pointID],
        "state":"NotStarted",
        "dtlTestEnvironment":{"id":"vstfs://dummy"},
        "plan":{"id":"$planId"},
        "filter":{"sourceFilter":"*.dll","testCaseFilter":""}
    }
    "@;
    
    #Initiate the RUN
    
    $tcRun = "https://dev.azure.com/$collection/$projectName/_apis/test/runs?api-version=5.0"
     $testRun = Invoke-RestMethod $tcRun -Method 'POST'  -ContentType "application/json"  -Body $payload -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} 
    Write-Host "Test Run Status is ...."
    
    $testrunid = $testRun.id
    
    echo $testrunid
    
    
    $url = "https://vsrm.dev.azure.com/$collection/$projectName/_apis/Release/definitions/$($releasedefinitionid)?api-version=5.0-preview.3"
    Write-Host "URL: $url"
    $pipeline = Invoke-RestMethod -Uri $url -Method Get  -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} 
    
    Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
    
    
    $pipeline.variables.testrunid.value = "$testrunid"
    
    ####****************** update the modified object **************************
    $json = @($pipeline) | ConvertTo-Json -Depth 99
    
    
    $updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} 
    
    
    
    $releaseRunurl ="https://vsrm.dev.azure.com/$collection/$projectName/_apis/Release/releases?api-version=6.1-preview.8"
    
     $releasebody = @"
     
     {
         "definitionId": $releasedefinitionid
    
    }
    "@;
    
    $ReleaseRun = Invoke-RestMethod $releaseRunurl -Method 'POST'  -ContentType "application/json"  -Body $releasebody -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} 
    
    $Releaseid=$ReleaseRun.id
    
    echo $Releaseid
    
    $ReleaseEnvID=$ReleaseRun.environments.id
    
    echo $ReleaseEnvID
    
    $updateTestrun="https://dev.azure.com/$collection/$projectName/_apis/test/Runs/$($testrunid)?api-version=6.1-preview.3"
    
    $updatebody = @"
     
     {
        "build":
        {
            "id":"$buildid"
        },
        "releaseEnvironmentUri":"vstfs:///ReleaseManagement/Environment/$ReleaseEnvID","releaseUri":"vstfs:///ReleaseManagement/Release/$Releaseid"
    
    }
    "@;
    
    
    $UpdateRun = Invoke-RestMethod $updateTestrun -Method 'PATCH'  -ContentType "application/json"  -Body $updatebody -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} 
    
    
     
    
    }
    

    结果:

    发布完成后,会更新试运行状态

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多