这将需要几个步骤:
- 创建自定义构建活动
- 将活动添加到构建控制器
- 在新的构建过程中调用该自定义构建活动
- 使用新的构建过程
1. 创建自定义构建活动。
在 VS2012 中创建一个新项目(我调用了我的 CodeCoverageLibrary。参考以下程序集:
- Microsoft.TeamFoundation.Build.Client
- Microsoft.TeamFoundation.Client
- Microsoft.TeamFoundation.TestManagement.Client
- Microsoft.TeamFoundation.WorkItemTracking.Client
- 系统
- 系统活动
- 系统核心
- System.Xaml
您可以使用以下代码:
using System;
using System.Activities;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
namespace CodeCoverageLibrary
{
[BuildActivity(HostEnvironmentOption.All)]
public sealed class GetCodeCoverage : CodeActivity<double>
{
public InArgument<IBuildDetail> BuildDetail { get; set; }
protected override double Execute(CodeActivityContext context)
{
var buildDetail = BuildDetail.Get(context);
var buildCoverages = GetBuildCoverages(buildDetail.BuildServer.TeamProjectCollection.Uri,
buildDetail.TeamProject, buildDetail.Uri);
return GetTotalCoverage(buildCoverages);
}
private static IEnumerable<IBuildCoverage> GetBuildCoverages(Uri uri, string projectName, Uri buildUri)
{
return TfsTeamProjectCollectionFactory.GetTeamProjectCollection(uri)
.GetService<ITestManagementService>()
.GetTeamProject(projectName)
.CoverageAnalysisManager.QueryBuildCoverage(buildUri.ToString(),
CoverageQueryFlags.Modules);
}
private static double GetTotalCoverage(IEnumerable<IBuildCoverage> buildCoverages)
{
int totalCoveredBlocks = 0, totalUncoveredBlocks = 0;
foreach (var buildCoverage in buildCoverages)
{
foreach (var module in buildCoverage.Modules)
{
totalCoveredBlocks += module.Statistics.BlocksCovered;
totalUncoveredBlocks += module.Statistics.BlocksNotCovered;
}
}
return (totalCoveredBlocks == 0 && totalUncoveredBlocks == 0)
? 0.0
: ((double) totalCoveredBlocks) /
((double) (totalCoveredBlocks + totalUncoveredBlocks));
}
}
}
编译项目并添加到 TFS 上的版本控制路径。
2. 将活动添加到构建控制器
在 Team Explorer 中,转到Builds > Actions > Manage Build Controllers ...。然后单击构建控制器的属性...。在自定义程序集的版本控制路径下,放置您在上面使用的路径。
3. 在构建过程中调用自定义构建活动
将BuildProcessTemplates\DefaultTemplate.11.1.xaml复制到新文件。
更新新 XAML 文件的开头以包含以下内容:
<Activity ...
xmlns:ccl="clr-namespace:CodeCoverageLibrary;assembly=CodeCoverageLibrary"
...
>
<x:Members>
...
<x:Property Name="CodeCoverageTolerance" Type="InArgument(x:Double)" />
</x:Members>
...
<this:Process.Metadata>
<mtbw:ProcessParameterMetadataCollection>
...
<mtbw:ProcessParameterMetadata BrowsableWhen="EditingDefinition" Category="#900 Misc" DisplayName="CodeCoverageTolerance" Description="If the overall code coverage drops below this threshold, then the build will fail. This is a number between 0 and 1." ParameterName="CodeCoverageTolerance" />
</mtbw:ProcessParameterMetadataCollection>
</this:Process.Metadata>
更新 XAML 文件的末尾以包含以下内容:
<Sequence DisplayName="Code Coverage Check" mtbwt:BuildTrackingParticipant.Importance="None">
<Sequence.Variables>
<Variable x:TypeArguments="x:Double" Name="CodeCovered" />
</Sequence.Variables>
<ccl:GetCodeCoverage DisplayName="Getting Code Coverage" BuildDetail="[BuildDetail]" Result="[CodeCovered]" />
<If Condition="[CodeCovered < CodeCoverageTolerance]">
<If.Then>
<Sequence DisplayName="Comparing Code Coverage Against Tolerance">
<mtbwa:SetBuildProperties DisplayName="Set TestStatus to Failed" mtbwt:BuildTrackingParticipant.Importance="Low" PropertiesToSet="TestStatus" TestStatus="[Microsoft.TeamFoundation.Build.Client.BuildPhaseStatus.Failed]" />
<mtbwa:WriteBuildError Message="["Code Coverage of " + CodeCovered.ToString("P") + " is less than required " + CodeCoverageTolerance.ToString("P")]" />
</Sequence>
</If.Then>
</If>
</Sequence>
</mtbwa:AgentScope>
<mtbwa:InvokeForReason DisplayName="Check In Gated Changes for CheckInShelveset Builds" Reason="CheckInShelveset">
<mtbwa:CheckInGatedChanges DisplayName="Check In Gated Changes" />
</mtbwa:InvokeForReason>
</Sequence>
</Activity>
将此检查到 TFS。
4.使用新的构建过程
在 Team Explorer 中,转到Builds并右键单击您的构建定义。转到Edit Build Definition... > Process。展开构建过程模板并单击新建...。单击选择现有 XAML 文件并输入新 XAML 文件的路径。单击确定。您现在应该在4. Misc下看到CodeCoverageTolerance。您可以输入一个介于 0 和 1 之间的数字来表示您想要的百分比。