How to decorate a class as untestable for Code Cov

2019-02-03 02:12发布

Background: Visual Studio 2008, C#, .Net 3.5.

I have a number of utility classes that are simply not unit-testable. This is mainly because they interact with resources (e.g. databases, files etc).

Is there a way I can decorate these classes so that the Visual Studio Code Coverage engine will ignore them, so not decreasing the code coverage percentage?

3条回答
闹够了就滚
2楼-- · 2019-02-03 02:49

When you upgrade your project to .NET 4, you'll get the ExcludeFromCodeCoverageAttribute Class.

查看更多
Luminary・发光体
3楼-- · 2019-02-03 02:49

One of the reason you want to write unit-tests is to make your code loosely coupled. You can read this article if you're interested in learning how to write loosely coupled code (in case you don't know how).

Saying that you can try to use tools like TypeMock that can help you mock your objects even if you don't write them using Dependency Injection principle.

TypeMock was the first Mock Framework I used. I switch to Rhino Mocks because with TypeMock I didn't have to be discipline enough to write loosely coupled code.

查看更多
倾城 Initia
4楼-- · 2019-02-03 03:08

There is an answer in this article about how to use [System.Diagnostics.DebuggerHidden] or [System.Diagnostics.DebuggerNonUserCode] Attributes to exclude methods from code coverage.

Update as per David's comment:

As of .NET 4.0 there is a dedicated attribute for this: [ExcludeFromCodeCoverage]

查看更多
登录 后发表回答