如何使用TFSAPI通过内部版本号检索TestRunId?

如何使用TFSAPI通过内部版本号检索TestRunId?,tfs,tfsbuild,microsoft-test-manager,tcm,Tfs,Tfsbuild,Microsoft Test Manager,Tcm,我们需要运行测试并在自定义ContinuUsintegration环境中发布结果。MSTest用于测试,所以我们使用命令行执行tfs构建。 在使用tfsbuild.exe执行测试/构建之后,我获得BuildNumber和UpdatedBuildNumber C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>tfsbuild start "tfsurl" "TeamProjectName" "BuildDefinitionName"

我们需要运行测试并在自定义ContinuUsintegration环境中发布结果。MSTest用于测试,所以我们使用命令行执行tfs构建。 在使用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);

这段代码并不总是有效的。获取构建uri是可行的,但testRunId失败,表示Enumertion Yeild没有结果。有人能建议如何使用BuildNumber或UpdatedBuildNumber获取TestRunId吗?

我刚刚运行了您的代码,它对我来说非常适合

然而,我为大约一小时前完成的构建运行了这段代码

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

几分钟后试着运行它。

我在使用
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;

您正在运行什么类型的测试?单元测试?自动测试用例?我这样问是因为你们在谈论MSTest和tcm,但这是两种不同的工具,你们不能以这种方式组合。这意味着,如果您通过MSTest运行单元测试,则无法使用tcm获得它们的结果。所以请提供更多信息。我正在使用tfsbuild命令行工具运行自动测试。如果我将TestRunId(从构建摘要页面复制)指定为TCM.exe,我就能够导出结果文件。我想自动化这个过程,所以我尝试使用TFSAPI访问TestRunId。此代码仅在某些情况下有效。不知道怎么了,谢谢。我发现BuildDefinition包含另外两个构建定义(builddefinitioncontainer)。因此,我用于查询的内部版本号不正确。有没有办法获取孩子的builddefinition的构建编号?
// 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;