您可以尝试使用 REST API 来获取现有单元测试的数量(只能获取运行单元测试,如果单元测试从未运行过,那么我们无法获取它们)。
首先获取测试运行列表,然后获取特定测试运行的测试结果。
获取测试运行列表(REST API):
GET https://server:8080/tfs/DefaultCollection/project/_apis/test/runs?api-version=1.0
获取测试结果列表(REST API):
GET https://server:8080/tfs/Defaultcollection/project/_apis/test/runs/{runid}/results?api-version=3.0-preview
您可以简单地使用下面的 PowerShell 脚本来获取项目中每个测试运行的单元测试计数,然后可以将列表导出到.csv文件(在 Excel 中打开)以计算特定项目中的单元测试数量。您可以按测试过滤completeDate
,或定期运行脚本,然后比较计数和测试名称以识别新添加的测试。
Param(
[string]$collectionurl = "http://server:8080/tfs/Collection",
[string]$project = "ProjectName",
[string]$user = "username",
[string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Get test runs
$testrunsUrl = "$collectionurl/$project/_apis/test/runs?api-version=1.0"
$testruns = (Invoke-RestMethod -Uri $testrunsUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
#Get unit tests from each test run
$UnitTestResults = @()
foreach ($testrun in $testruns.value)
{
$testrunID = $testrun.id
#Get test results for specific test run
$baseUrl = "$collectionurl/$project/_apis/test/runs/$testrunID/results?api-version=3.0-preview"
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$unittests = $response.value
$customObject = new-object PSObject -property @{
"TestRunID" = $testrunID
"CountOfUnitTest" = $response.count
"UnitTestname" = @($unittests.testCaseTitle -join ',')|Select-Object
"outcome" = @($unittests.outcome -join ',')|Select-Object
"completedDate" = @($unittests.completedDate -join ',')|Select-Object
}
$UnitTestResults += $customObject
}
$UnitTestResults | Select `
TestRunID,
CountOfUnitTest,
UnitTestname,
outcome,
completedDate | Where {$_.completedDate -like "*2017*"} #|export-csv -Path C:\LC\UnitTest.csv -NoTypeInformation