简体   繁体   English

TFS 2010 RC:如何使代码覆盖率低的构建失败?

[英]TFS 2010 RC: How to fail a build for low code coverage?

当代码覆盖率低于某个阈值时,如何使构建失败?

The main issue is that the code coverage results file that MSTest produces is in a binary format. 主要问题是MSTest生成的代码覆盖率结果文件是二进制格式。 However, assuming that things haven't changed too much in VS2010, you should be able to use this utility to convert it into an XML file: 但是,假设在VS2010中事情没有太大变化,您应该能够使用此实用程序将其转换为XML文件:

http://codeexperiment.com/file.axd?file=2008%2f9%2fCodeCoverageConverter.zip http://codeexperiment.com/file.axd?file=2008%2f9%2fCodeCoverageConverter.zip

NOTE: You'll probably need to recompile it against the VS2010 version of 'Microsoft.VisualStudio.Coverage.Analysis.dll. 注意:您可能需要针对VS2010版本的Microsoft.VisualStudio.Coverage.Analysis.dll重新编译它。

You can then use your preferred method of parsing that XML file, doing the maths for each of the instrumented assemblies to calculate an overall coverage ratio. 然后,您可以使用首选的解析XML文件的方法,对每个已检测的程序集进行数学计算以计算总体覆盖率。 The XPaths you're interested in (at least for VS2008) are: 您感兴趣的XPath(至少对于VS2008)是:

/CoverageDSPriv/Module/LinesCovered
/CoverageDSPriv/Module/LinesNotCovered

If you want to do that last step in pure MSBuild, then the 'XmlRead' and 'Math' tasks contained within the MSBuild Community Tasks library should be sufficient: 如果您想在纯MSBuild中执行最后一步,那么MSBuild社区任务库中包含的'XmlRead'和'Math'任务应该足够:

http://msbuildtasks.tigris.org/ http://msbuildtasks.tigris.org/

Once you have the overall ratio in an MSBuild property, you then simply use a conditional task to break the build if that number is lower than your desired threshold. 在MSBuild属性中获得总体比率后,如果该数字低于所需阈值,则只需使用条件任务来中断构建。

<Error Condition=" $(CodeCoverageRatio) &lt; $(MinCodeCoverage) "
       Text="Code Coverage is below required threshold." />

There is very likely a way to do this with a build task (particularly if you are willing to roll your own). 使用构建任务很可能有一种方法(特别是如果你愿意自己动手​​)。 Hopefully someone will post some sample code for you. 希望有人会为您发布一些示例代码。

If not, I have been impressed with NDepend for this type of task. 如果没有,我对NDepend的这类任务印象深刻。 You can write in a very self-explanatory, SQL-like syntax to determine all sorts of metrics about your code and warn or fail a build based on thresholds. 您可以使用非常不言自明的类似SQL的语法编写,以确定有关代码的各种度量标准,并根据阈值警告或失败构建。

Examples: 例子:

WARN IF Count > 0 IN SELECT METHODS WHERE CodeWasChanged AND PercentageCoverage <  95
WARN IF Count > 0 IN SELECT METHODS WHERE IsPublic AND IsInOlderBuild AND WasRemoved 

Ancient question, but not marked as answered. 古代的问题,但没有标记为回答。 Take a look at this 看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM