Unit testing 使用Azure DevOps REST API在构建上运行测试

Unit testing 使用Azure DevOps REST API在构建上运行测试,unit-testing,azure-devops,automated-tests,azure-devops-rest-api,Unit Testing,Azure Devops,Automated Tests,Azure Devops Rest Api,是否有一个RESTAPI来获取一个构建运行/失败的测试数量 我知道有一个用于获取的API,但是除非我缺少一些明显的东西,否则我无法找到一个API来获取一个构建运行的测试数量 看起来所有可用于获取测试结果的API都需要一个测试runid,但我只有一个buildid。我不确定是否有专用于测试的端点,但您可以从日志中获取测试 $AzureDevPSAuthenticationHeader=@{Authorization='Basic'+[Convert]::ToBase64String([Text.E

是否有一个RESTAPI来获取一个构建运行/失败的测试数量

我知道有一个用于获取的API,但是除非我缺少一些明显的东西,否则我无法找到一个API来获取一个构建运行的测试数量


看起来所有可用于获取测试结果的API都需要一个测试runid,但我只有一个buildid。

我不确定是否有专用于测试的端点,但您可以从日志中获取测试

$AzureDevPSAuthenticationHeader=@{Authorization='Basic'+[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(:$(System.AccessToken)”)}
$uri=”https://dev.azure.com/$(DevPSAccount)/$(projectName)/\u api/build/builds/$(build.BuildId)/logs/$(logId)?api版本=5.1“
写入主机$uri
#调用REST调用
$result=Invoke RestMethod-Uri$Uri-Method Get-Headers$AzureDevOpsAuthenicationHeader
写入主机$result
$lines=$result.Split([Environment]::NewLine)
$passed=0;
$failed=0;
foreach($行中的行){
如果($line-match“通过:.(\d+)){
$passed=$matches[1]
}
如果($line-match”失败:。(\d+){
$failed=$matches[1]
}
}
echo$passed
echo$失败
你需要传递你的构建id和日志id。要获得日志id,请获取所有日志并仔细查看它们,然后找到与测试任务相关的日志

就buildId和runId而言,它们是相同的。我的意思是buildId=runId。使用较新的语法时,术语发生了变化。

您应该尝试使用API。传递可选的生成id

GET https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate={minLastUpdatedDate}&maxLastUpdatedDate={maxLastUpdatedDate}&buildIds={buildIds}&api-version=6.0
值得注意的是,通过在管道的任务步骤上设置,您可以自定义运行的标题(包括内部版本号)。

您可以使用Matt提到的Api。但是我发现
buildId
查询参数可能不起作用。您可能需要按buildId过滤api结果。请参见以下示例:

$connectionToken="Personal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?minLastUpdatedDate=2020-10-20&maxLastUpdatedDate=2020-10-22&api-version=6.0"

$results= Invoke-RestMethod -Uri $trurl -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

$run = $results.value | Where-Object{$_.buildConfiguration.id -eq $buildId}

$runId = $run.id
你也可以退房。我发现buildId总是附加到运行标题中。您可以按运行名称筛选api结果。见下文:


通过Runs-Query传递buildid似乎是可行的,感谢使用测试运行名称的替代建议,这对我来说也是可行的。
$connectionToken="Personal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url = "https://dev.azure.com/{organization}/{project}/_apis/test/runs?api-version=6.0"

$results= Invoke-RestMethod -Uri $trurl -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

$run = $results.value | Where-Object{$_.name -match $buildId}

$runId = $run.id