在 Visual Studio 中使用属性从覆盖分析中省略代码 [英] Use attribute to omit code from coverage analysis in Visual Studio

查看:17
本文介绍了在 Visual Studio 中使用属性从覆盖分析中省略代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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

[ExcludeFromCodeCoverage]
public class myUntestableClass
{ }

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

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

这篇关于在 Visual Studio 中使用属性从覆盖分析中省略代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆