You can try to use the REST API to get the number of existing unit tests (Can only get the run unit tests, if the unit tests never never been run before, then we cannot get them).
Get the list of test runs first, then get the test results for the specific test run.
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
You can simply use below PowerShell script to get the count of unit tests for each test run in a project, then you can export the list to a .csv file (open in Excel) to calucate the number of unit tests in a specific project. You can filter by the test completeDate
, or run the script periodically, then compare the count and test names to idetify the new added tests.
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
