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