We need to run the tests and publish the results in custom ContinousIntegration environment. MSTest is used for testing, so we use commandline execution of tfs build. After executing a tests/build using tfsbuild.exe, I get the BuildNumber and UpdatedBuildNumber.
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>tfsbuild start "tfsurl" "TeamProjectName" "BuildDefinitionName"
Microsoft (R) TfsBuild Version 10.0.0.0
for Microsoft Visual Studio v10.0
Copyright (c) Microsoft Corporation. All rights reserved.
Build number: 36399
Updated Build number: XYZ_20140405.1
Succeeded
I use the UpdatedBuildNumber to query tfs and get the BuildUri.
Uri tfsUri = new Uri("tfsurl");
TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(tfsUri);
IBuildServer tfsBuildServer = _tfs.GetService<IBuildServer>();
IBuildDefinitionSpec buildSpec =
tfsBuildServer.CreateBuildDefinitionSpec("TeamProjectName");
IBuildDetail buildDetail = tfsBuildServer.GetBuild(buildSpec,
"XYZ_20140405.1", null, QueryOptions.All);
the buildDetail has the BuildUri which is passed to retrieve the TestRunId, using which the TestResults (trx) file can be exported (tcm command)
ITestManagementService test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
ITestManagementTeamProject _testproject = test_service.GetTeamProject("TeamProjectName");
var testRuns = _testproject.TestRuns.ByBuild(buildDetail.Uri);
int testRunId= 0;
if (buildDetail.BuildFinished)
{
foreach (ITestRun item in testRuns)
{
testRunId= item.Id;
}
}
This code doesn't work always. getting the build uri works but testRunId fails saying Enumertion Yeilded No results. Can someone suggest how to get the TestRunId using BuildNumber or UpdatedBuildNumber?