0

我喜欢获取去年运行的每个构建中的单元测试数量。这样我就可以看看开发人员是否编写了一些新的测试。

为此,我获得了 TFS 仓库数据库的视图,并找到了视图 TestResultView,它正好为我提供了我需要的数据。

查询示例:

SELECT BuildId, YEAR(ResultDate) as year, MONTH(ResultDate) as month , DAY(ResultDate) as day, COUNT(*) AS Passed, 0 AS Failed FROM TestResultView
WHERE BuildDefinitionName = 'Test'
AND TestResultAttributesOutcome = 'Passed'
GROUP BY BuildId, YEAR(ResultDate), MONTH(ResultDate), DAY(ResultDate)
UNION ALL
SELECT BuildId, YEAR(ResultDate) as year, MONTH(ResultDate) as month, DAY(ResultDate) as day, 0 as Passed, COUNT(*) AS Failed FROM TestResultView
WHERE BuildDefinitionName = 'Test'
AND TestResultAttributesOutcome = 'Failed'
GROUP BY BuildId, YEAR(ResultDate), MONTH(ResultDate), DAY(ResultDate)

唯一的问题是我只看到最后四天。

我使用了错误的视图、数据库还是有任何其他方式来获取这些信息?

非常感谢您的帮助。

4

1 回答 1

0

您可以尝试使用 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

在此处输入图像描述

于 2018-01-19T07:34:31.020 回答