visual-studio 在 Visual Studio 中使用属性从覆盖率分析中省略代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1289429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:52:45  来源:igfitidea点击:

Use attribute to omit code from coverage analysis in Visual Studio

visual-studioattributescode-coveragencover

提问by tvanfosson

I have some classes that, for one reason or another, cannot be or need not be unit tested. I'd like to exclude these classes from my coverage metrics so that I can get a better feel for the coverage on the classes I actually care about. Right now I have to exclude the results after the fact. What I would like to do is use an attribute to mark those classes as excluded so that they aren't included to begin with. Is there any way to decorate a class with an attribute that will automatically exclude it from coverage analysis?Either VS coverage analysis or nCover will work.

我有一些类,由于某种原因,不能或不需要进行单元测试。我想从我的覆盖率指标中排除这些类,以便我可以更好地了解我真正关心的类的覆盖率。现在我必须在事后排除结果。我想要做的是使用一个属性将这些类标记为排除在外,这样它们就不会被包含在内。 有没有办法用一个属性装饰一个类,该属性会自动将其从覆盖率分析中排除?VS 覆盖率分析或 nCover 都可以使用。

FWIW, these are classes that I can assure myself by inspection that the code is correct. Mostly they are wrapper classes around existing framework classes that I've introduced in order to be able to mock the framework class out. Since the wrapper's get mocked out they don't get tested. That's ok because all they do is wrap the framework class' methods that I care about.

FWIW,这些是我可以通过检查代码是否正确来向自己保证的类。大多数情况下,它们是围绕我引入的现有框架类的包装类,以便能够模拟框架类。由于包装器被嘲笑,因此它们没有得到测试。没关系,因为他们所做的只是包装我关心的框架类的方法。

回答by Shrike

Starting with VS2010 we have ExcludeFromCodeCoverageAttribute. Commenters have noted this to work in NCover as well as DotCover + NUnit. Example usage:

从 VS2010 开始,我们有ExcludeFromCodeCoverageAttribute. 评论者已经注意到这可以在 NCover 以及 DotCover + NUnit 中使用。用法示例:

[ExcludeFromCodeCoverage]
public class myUntestableClass
{ }

Also see this link. They suggest using VSInstr as command line tool, it have /EXCLUDE options (it's not as handy).

另请参阅此链接。他们建议使用 VSInstr 作为命令行工具,它有 /EXCLUDE 选项(它不是那么方便)。

回答by tvanfosson

I've found some informationon a couple of Diagnostics attributes DebuggerNonUserCodeAttributeand DebuggerHiddenAttributethat indicates that using these attributes will cause the coverage analyzer in VS to leave these out of its results. I've tried it with the DebuggerNonUserCodeAttribute and it seems to work. I can probably live with this for most of the classes that I'm thinking of, though I don't like the side effect of not being able to step into these classes. That shouldn't be a problem for the wrapper classes, but it may end up being so with classes that are inherently hard to test and I need debugger access to.

我发现了一些关于诊断属性DebuggerNonUserCodeAttributeDebuggerHiddenAttribute 的一些信息这些信息表明使用这些属性将导致 VS 中的覆盖分析器将这些排除在其结果之外。我已经用 DebuggerNonUserCodeAttribute 尝试过它,它似乎有效。对于我正在考虑的大多数课程,我可能可以接受这一点,尽管我不喜欢无法进入这些课程的副作用。对于包装器类来说,这应该不是问题,但是对于本质上难以测试并且我需要调试器访问权限的类,它最终可能会成为问题。

I'm still looking for alternatives, though.

不过,我仍在寻找替代品。

回答by dcramos

With NCover you can create an attribute and then tell NCover to ignore that attribute.

使用 NCover,您可以创建一个属性,然后告诉 NCover 忽略该属性。

In our projects, we have defined this attribute (no namespace, so it is easy to use):

在我们的项目中,我们定义了这个属性(没有命名空间,所以很容易使用):

public class CoverageExcludeAttribute : Attribute { }

We use NAnt, so we have a target that looks like this:

我们使用 NAnt,所以我们有一个如下所示的目标:

<target name="unittests" description="run nunit tests" >        
    <ncover
        ....
        excludeAttributes="CoverageExcludeAttribute"
    />
</target>

Question 9 in the NCover FAQdescribes this method. We based our solution on this.

NCover FAQ中的问题 9 描述了这种方法。我们的解决方案基于此。

Alternatively, you can use the exclude feature of the NCoverExplorer to exclude namespaces and assemblies from the final report. This merely removes the data from the report, but the end result is the same.

或者,您可以使用 NCoverExplorer 的排除功能从最终报告中排除命名空间和程序集。这只是从报告中删除数据,但最终结果是相同的。

We use both techniques.

我们使用这两种技术。