【问题标题】:How to retrieve TestRunId by build number using TFS api?如何使用 TFS api 按内部版本号检索 TestRunId?
【发布时间】:2016-12-25 03:43:42
【问题描述】:

我们需要在自定义的 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 吗?

【问题讨论】:

  • 您正在运行什么样的测试?单元测试?自动化测试用例?
  • 我问是因为您在谈论 MSTesttcm 但这是两个不同的工具,您不能以这种方式组合。这意味着如果通过 MSTest 运行 单元测试,您将无法使用 tcm 获得结果。所以请提供更多信息。
  • 我正在使用 tfsbuild 命令行工具运行自动化测试。如果我将 TestRunId(从构建摘要页面复制)指定到 TCM.exe,我可以导出结果文件。我想自动化这个过程,所以我尝试使用 TFS API 访问 TestRunId。此代码仅在某些时候有效。不知道出了什么问题。

标签: tfs tfsbuild microsoft-test-manager tcm


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我刚刚运行了你的代码,它非常适合我。

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

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

    【讨论】:

    • 谢谢。我发现 BuildDefinition 包含另外两个构建定义(BuildDefinition 容器)。所以我用于查询的内部版本号不正确。有没有办法获取孩子的 builddefinition 的内部版本号?
    猜你喜欢
    • 2010-12-04
    • 2016-03-25
    • 2012-06-08
    • 2017-02-15
    • 1970-01-01
    • 2011-08-04
    • 2011-09-20
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多