Note: The implementation below is based on NCover, NUnit and CC.NET, but the concept itself does not care about specific tools.

We all do unit test coverage to monitor code areas that should be tested.

Obviously, not all classes are worth testing. For example, these seem to be reasonable exceptions:

  • Plain old С# classes used just for passing data around
  • Method shortcuts that simply rearrange the parameters and call another method
  • Helper extension methods
  • Simple factories, adapters and decorators

To avoid writing unit tests for these classes (or methods) and to keep the build from failing (when the code coverage ratio drops below the acceptance threshold) developers generally use some sort of NoCodeCoverageAttribute. This marker attribute is then included into the configuration of the coverage tool to tell it that the specified code does not count in the coverage calculation.

*Note: *It is a common practice to exclude classes with CompilerGeneratedAttribute as well.

However, with this attribute comes the problem. While rushing in some changes, developers often put this marker on classes without proper tests (while promising themselves to fix that some day later)

The simple unit test below allows to ensure that such thing never happens. It will fail if there is any complex method marked with NoCodeCoverageAttribute that has more than 35 instructions (empirical estimate).

[Test]
public void NCCA_Should_Not_Be_Applied_To_Complex_Methods()
{
  var methods = GlobalSetup.Methods
    .Where(m => m.HasBody && m.Body.Instructions.Count > 35)
    .Where(m => m.Has<NoCodeCoverageAttribute>() ||
           m.DeclaringType.Has<NoCodeCoverageAttribute>());

  if (methods.Count() == 0)
    return;

  Assert.Fail("Methods are too complex to be marked with NCCA: {0}",
              methods.Select(m => m.ToString()).Join(Environment.NewLine));
}