我们需要在自定义的ContinousIntegration环境中运行测试并发布结果。MSTest 用于测试,因此我们使用命令行执行 tfs 生成。使用 tfsbuild.exe 执行测试/构建后,我得到了 BuildNumber 和 UpdateBuildNumber。
C:Program Files (x86)Microsoft Visual Studio 10.0VC>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
我使用 UpdateBuildNumber 来查询 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,传递该 Uri 以检索 TestRunId,使用该 BuildUri 可以导出 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 失败,指出枚举 Yeilded 没有结果。有人可以建议如何使用BuildNumber或UpdateBuildNumber获取TestRunId吗?
我在BuildUri
ITestRun
时遇到了同样的问题。我找到了 2 种对我有用的解决方法:
// 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;
我刚刚运行了您的代码,它非常适合我。
但是,我为大约一小时前完成的构建运行此代码。
您是否在构建完成后立即运行此代码?
如果是这样,则可能是测试运行信息尚未在 TFS 上更新。
尝试在几分钟后运行它。