我在哪里可以获得现有单元测试的数量

时间:2018-01-18 12:20:21

标签: testing tfs tfs2017 warehouse test-results

我想获得去年运行的每个版本中的单元测试数量。这样我就可以看到开发人员是否编写了一些新测试。

为此,我获得了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)

唯一的问题是我只看到过去的四天。

我使用错误的视图,数据库还是有其他方式来获取此信息?

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用REST API来获取现有单元测试的数量(只能获得运行单元测试,如果以前从未运行过单元测试,那么我们就无法获得它们。)

首先获取测试运行列表,然后获取特定测试运行的测试结果。

Get a list of test runs (REST API)

GET https://server:8080/tfs/DefaultCollection/project/_apis/test/runs?api-version=1.0

Get a list of test results (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

enter image description here