Hi,
This is the solution for a problem I encountered a while ago.
When writing tests with Visual Studio, we frequently want to use the Code Coverage tool to evaluate our created tests. Though this is a good tool, we don't always want all of our code to be included in the coverage result. Let me give you an example.
I have created 2 projects:
- A class library [MyProject] with 2 classes: ClassToBeTested and MyException
- A unit test project [MyTestProject] for which I kept the default class. I renamed it to MyProjectUnitTest

The classes from the class library look like this:
public class ClassToBeTested
{
private const int MaxParameterLength = 5;
public int MethodToBeTested(string someParameter)
{
// Validate the parameter
if (someParameter.Length > MaxParameterLength)
{
throw new MyException("The length of the parameter was too large.");
}
return new Random().Next();
}
}
[System.Serializable]
public class MyException : Exception
{
public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception inner) : base(message, inner) { }
protected MyException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
The Unit Test Class looks like this (I did snip some generated code):
[TestClass]
public class MyProjectUnitTest
{
[TestMethod]
public void ClassToBeTestedTest()
{
int expected = 0;
ClassToBeTested classToBeTested = new ClassToBeTested();
int actual = classToBeTested.MethodToBeTested("abc");
Assert.IsTrue(actual > expected);
}
[TestMethod]
[ExpectedException(typeof(MyException))]
public void ClassToBeTestedExceptionTest()
{
ClassToBeTested classToBeTested = new ClassToBeTested();
int actual = classToBeTested.MethodToBeTested("abcdef");
}
}
When we run the unit tests with code coverage used for MyProject, we get a code coverage result of 62,50% which is not as much as we would expect in such a small project.
Below, we can see that the reason for this low score, is the Exception class we use.
Now, if we create a project with many Exception classes, we do not want to write tests for all of these classes. I want to exclude this class from code coverage. If we search the net, we will find that there is no specific solution for this problem. There is a workaround though.
This workaround comes in the form of attributes. We have 2 possibilities:
1. [System.Diagnostics.DebuggerNonUserCode()]
2. [System.Diagnostics.DebuggerHidden()]
The first attribute we can use for both classes and class members. The second one, we can only use for class members: constructors, methods, properties and indexers.
As we want to exclude the whole class from code coverage, I will opt for attribute 1. We just place it on the MyException class (or any class you want to exclude).
We run the tests again, and take a look at the result:

Not only is our code coverage raised to 100%, the class on which I put the attribute has also disappeared from the results.
Important note
As you may have guessed, there is a drawback to using these attributes.
If you use the DebuggerNonUserCode or DebuggerHidden attribute, the code will be ignored completely; you won't be able to step into the code anymore. Also, any breakpoints you will add, will be ignored completely.
Make sure you don't just start adding the attributes for the sake of raising your code coverage score. Use this approach carefully as you might be excluding code from your results that needs to be tested!
Good luck.