powershell - 是否可以使用 Rest api Powershell 脚本执行 Azure 测试计划
问题描述
我是 Rest api 和 Powershell 脚本的新手。我想知道我们是否可以使用 Powershell 脚本执行 Azure 测试计划。我的计划是编写一个 powershell 脚本来执行我项目中的一组现有测试计划。然后在 Pipeline 中使用该 powershell 脚本并安排管道在特定时间运行。请帮我。我知道我们可以使用 Rets api 获取测试计划列表,是否也可以执行它们?
解决方案
测试计划不支持运行,只能执行测试点。
如果您想在测试计划中使用 Rest API 执行自动测试,您可以参考以下步骤:
以下是以下步骤:
Step1:您可以在 Release Pipeline 中定义一个变量。
Step2:在VSTest任务中添加这个变量:
注意:测试运行的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)}
}
结果:
发布完成后,会更新试运行状态
推荐阅读
- laravel - 使用 Sanctum 和常规 Laravel 身份验证
- php - 用文字搜索
- javascript - React Router 更改 URL,但组件未呈现
- c# - 标题:使用 Xamarin.Forms 项目 c# 连接到 LDAP 服务器时出现问题
- c# - Unity2D ScrollView UI 在 Android 构建中不显示其内容
- r - tidyverse 中的重命名功能无法按预期工作
- db2 - 清除 DB2 中的事务数据
- python - 当用户输入为空时Python引发错误
- excel - 如何在 VBA 中定义下一个循环的范围
- windows - 如何写入 BitLocker 加密卷的物理扇区?