0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sanketh
  • 353
  • 1
  • 4
  • 7
  • What kind of tests are you running? Unit tests? Automated Test Cases? – Elena Apr 06 '14 at 13:18
  • I'm asking because you are talking about _MSTest_ and _tcm_ but these are two different tools you can't combine this way. That means if are running _Unit tests_ via _MSTest_ you can't get their results using _tcm_. So please provide more information. – Elena Apr 06 '14 at 13:25
  • I am running Automated Tests using tfsbuild command line tool. I am able to export results file if I specify the TestRunId (copied from Build summary page) to TCM.exe. I want to automate this procedure, so I am trying to access TestRunId using TFS API. This code works only sometimes. Not sure what is wrong. – Sanketh Apr 06 '14 at 16:15

2 Answers2

1

I had the same problem with getting ITestRun with BuildUri. I found 2 ways to solve this that worked for me:

// after getting the build details, get all test runs and compare matching properties:
IBuildDetail buildDetail = tfsBuildServer.GetBuild(buildSpec, "XYZ_#", null, QueryOptions.All);

// get test run by title and build number, surprisingly they match:
ITestRun testRun = _testproject.TestRuns.Query("SELECT * FROM TestRun").Where(x => x.Title == buildDetail.BuildNumber).Single();

// get test run by their finish time:
ITestRun testRun = _testproject.TestRuns.Query("SELECT * FROM TestRun").Where(x => x.LastUpdated == buildDetail.LastChangedOn).Single();

int testRunId = testRun.Id;
Moshisho
  • 2,781
  • 1
  • 23
  • 39
0

I've just run your code and it's working perfectly for me.

However, I run this code for a build that was completed about one hour ago.

Are you running this code immediately after your build get completed?
If so, it could be that the test run information was not updated on TFS, yet.
Try to run it a few minutes later.

Elena
  • 1,928
  • 1
  • 15
  • 23
  • Thank you. I found that the BuildDefinition contains two other build definitions (BuildDefinition container). So the build number which i use for the query is incorrect. Is there a way to get the build number of child's builddefinition? – Sanketh Apr 19 '14 at 14:45