Latest Replies
Tuesday
Oct212008

How to ensure that complex methods are covered with tests

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));
}

Check out Lokad.Shared to see all the implementation details for yourself.

« How to improve .NET code by integrating hints for ReSharper | Main | Productivity migration »

Reader Comments (2)

Thanks for sharing all this cool stuff, I am learning heaps about funky language syntax's & it keeps expanding my expectations of what a development language & tools can do.

Keep up the good work ;D
Cheers!!!

October 22, 2008 | Unregistered CommenterJoel

Joel,

my pleasure)

October 22, 2008 | Unregistered CommenterRinat Abdullin

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>