0

我们需要在自定义的 ContinousIntegration 环境中运行测试并发布结果。MSTest 用于测试,所以我们使用 tfs build 的命令行执行。使用 tfsbuild.exe 执行测试/构建后,我得到了 BuildNumber 和 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

我使用 UpdatedBuildNumber 来查询 tfs 并获取 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);

buildDetail 有 BuildUri,它被传递来检索 TestRunId,使用它可以导出 TestResults (trx) 文件(tcm 命令)

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;
            }
        }

此代码并不总是有效。获取构建 uri 工作但 testRunId 失败说 Enumertion Yeilded No results。有人可以建议如何使用 BuildNumber 或 UpdatedBuildNumber 获取 TestRunId 吗?

4

2 回答 2

1

ITestRun我遇到了同样的问题BuildUri。我找到了两种对我有用的方法来解决这个问题:

// 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;
于 2015-01-01T10:26:12.233 回答
0

我刚刚运行了您的代码,它对我来说非常有效。

但是,我为大约一小时前完成的构建运行此代码。

您是否在构建完成后立即运行此代码?
如果是这样,可能是测试运行信息尚未在 TFS 上更新。
尝试在几分钟后运行它。

于 2014-04-07T12:04:54.897 回答