posted on Sunday, January 08, 2006 4:38 PM by anoras

How To: Exclude Generated Code From Code Coverage Analysis

To ensure that your unit tests exercise your code properly, you’ll need sufficient code coverage. Your coverage should be as close as possible to 100 percent. However, if your code base includes generated artifacts, such as resource accessors or strongly typed datasets, it is unlikely that you’ll come close to this figure. This is because the generated code has methods and properties for a wide variety of usages, many more than you’re actually using, thus leaving lots of untested code in your project. Since generated code skews your code coverage results, factoring generated code to separate assemblies is a common practice to avoid misleading code coverage metrics. A basic example is to have datasets in one project and business components in another project with a reference to the dataset project. This solves the code coverage issue, but it also introduces some other issues. Since one logical assembly has to be split into two or more physical assemblies, you’ll lose cohesion between the different logical assemblies. This might not be an issue during development, but you’ll at least have twice as many assemblies to deploy, version and maintain when the solution goes to production. Michael Barnett of Microsoft Research has developed a tool called IL Merge which can be used to merge multiple .NET assemblies into a single assembly. If you’re familiar with C-programming, IL Merge is akin to a linker.

For the remainder of this post I’ll show how you can factor a resource accessor out of a business component project to its own project, make the internals of that project available to the business component project (resource accessors aren’t publicly visible) and use IL Merge to merge the two assemblies into one.

I have created a small Visual Studio 2005 solution with two projects, one for business components and another for resources.

As mentioned earlier the generated resource accessor class is an internal class, and hence it is not available outside the assembly itself. There are numerous ways you can access the internals of one assembly from another. The most elegant is to use the .NET 2.0 InternalsVisibleTo attribute. This attribute allows you to declaratively give one or more assemblies access to the assembly’s internal members. The following declaration has been added to the ClassLibrary.Resources project’s AssemblyInfo.cs file. [assembly:InternalsVisibleTo("ClassLibrary")]

For a thorough discussion on InternalsVisibleTo and the other options available for accessing one assembly’s internal members from another, see my post “InternalsVisibleTo, StrongNameIdentityPermission and reflection”.

The internals of ClassLibrary.Resources are now available from within the ClassLibrary project with IntelliSense and other Visual Studio comforts just like if both the resources and the code had been part of the same project. An important difference is that the code base is spilt into two assemblies, so that you’re able to instrument just the ClassLibrary assembly and get accurate code coverage results.

When the unit test have been run (with code coverage instrumentation) you can then merge the two into a single assembly by using the IL Merge tool.

As you would expect, the merged assembly contains both the business component from the ClassLibrary project and the resources from the ClassLibrary.Resources project. Also note that it is a regular .NET assembly, so you can add it as a reference to any other project, sign it with your strong name key or similar.

Typically you would merge the two assemblies either when you package a release version of your code or from within your continuous integration build script prior to deploying it to a test rig.

There is a NAnt task for IL Merge available for download here. I’m not aware of a MSBuild task for IL Merge, but you can easily run IL Merge from your MSBuild script using the Exec task as shown below

    <Project DefaultTargets="CompileUserControls"
     xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     ...
     <Target Name="MergeAssemblies">
     <Exec Command="$(ILMergeEXE) /out:$MergedClassLibrary.dll
     ClassLibrary.dll ClassLibrary.Resources.dll" />
     </Target>
     ...
    </Project>

I believe this is an elegant solution to a problem which almost everyone doing Test Driven Development on the .NET platform run into. However, one feature I’d really like to see in Visual Studio Orcas is the ability to declaratively exclude types, or even members from code coverage instrumentation. This could very well follow the same convention used to suppress static analysis rules with System.Diagnostics.CodeAnalysis.SuppressMessageAttribute since this would allow any vendor to support it without forcing developers to reference a particular assembly in their code (SupressMessageAttribute is defined in mscorlib.dll). The solution described in this post is vendor neutral and works just as good with Team Test as with Clover.NET or any other code coverage analyzer.

Comments

# Very good Site! @ Sunday, May 20, 2007 3:45 AM

http://www.torrentspy.com/site1/ [url=http://www.torrentspy.com/site2/]site2[/url] <a href="http://www.torrentspy.com/site3/">site3</a> <a href=http://www.torrentz.com/site4/>site4</a>
[url="http://www.torrentz.com/site5/"]site5[/url] [link=http://www.torrentz.com/site6/]site6[/link] v

Jykqwcot

# Very good Sitehiezdv40 @ Friday, May 25, 2007 11:54 AM

http://www.usmle.net/step-3/messages2007a/182339.html http://www.usmle.net/step-3/messages2007a/182329.html http://www.usmle.net/step-3/messages2007a/182324.html http://www.usmle.net/step-3/messages2007a/182338.html http://www.usmle.net/step-3/messages2007a/182340.html http://www.usmle.net/step-3/messages2007a/182323.html ei

Vmquzafn

# Very good Sitesiwjno80 @ Saturday, May 26, 2007 3:33 AM

http://www.usmle.net/step-3/messages2007a/182326.html http://www.usmle.net/step-3/messages2007a/182332.html http://www.usmle.net/step-3/messages2007a/182323.html http://www.usmle.net/step-3/messages2007a/182330.html http://www.usmle.net/step-3/messages2007a/182340.html http://www.usmle.net/step-3/messages2007a/182338.html de

Eiftdwyn

# Best viewssgukci74 @ Saturday, May 26, 2007 6:11 PM

http://www.usmle.net/step-3/messages2007a/182324.html http://www.usmle.net/step-3/messages2007a/182332.html http://www.usmle.net/step-3/messages2007a/182330.html http://www.usmle.net/step-3/messages2007a/182322.html http://www.usmle.net/step-3/messages2007a/182323.html http://www.usmle.net/step-3/messages2007a/182331.html gh

Ofsczwgm

# Best content, fine designsfgltg97 @ Sunday, May 27, 2007 9:44 AM

http://www.usmle.net/step-3/messages2007a/182333.html http://www.usmle.net/step-3/messages2007a/182335.html http://www.usmle.net/step-3/messages2007a/182340.html http://www.usmle.net/step-3/messages2007a/182328.html http://www.usmle.net/step-3/messages2007a/182331.html http://www.usmle.net/step-3/messages2007a/182323.html nr

Zdimlhek

# Very good Site! @ Wednesday, May 30, 2007 6:46 PM

http://www.torrentspy.com/site1/ [url=http://www.torrentspy.com/site2/]site2[/url] <a href="http://www.torrentspy.com/site3/">site3</a> <a href=http://www.torrentz.com/site4/>site4</a>
[url="http://www.torrentz.com/site5/"]site5[/url] [link=http://www.torrentz.com/site6/]site6[/link] v

Vszritha

# Weston @ Tuesday, June 05, 2007 11:41 AM

Pioneering screenwriter Nigel Kneale, best known for the Quatermass TV serials and films, dies aged 84.

Anonymous

# Traylor @ Tuesday, June 05, 2007 12:07 PM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Carl @ Tuesday, June 05, 2007 12:37 PM

Veteran actor William Franklyn, known for voicing the 1960s Schweppes TV adverts, dies aged 81.

Anonymous

# Salerno @ Tuesday, June 05, 2007 1:07 PM

The Rolling Stones postpone a show in the US to allow singer Sir Mick Jagger time to rest his voice.

Anonymous

# Torres @ Tuesday, June 05, 2007 2:01 PM

Social networking site MySpace is to block users from uploading copyrighted music to its pages.

Anonymous

# Feeney @ Tuesday, June 05, 2007 2:21 PM

TV host Oprah Winfrey gives audience members $1,000 (526) each to donate to a charitable cause.

Anonymous

# Christman @ Tuesday, June 05, 2007 2:36 PM

Jonathan Ross is dubbed &amp;quot;risque&amp;quot; by Ofcom but not in breach of rules over an interview with David Cameron.

Anonymous

# Calvert @ Tuesday, June 05, 2007 2:51 PM

Doctor Who takes three prizes at the National Television Awards in a repeat of its success last year.

Anonymous

# Gabriel @ Tuesday, June 05, 2007 3:06 PM

Pop trio Atomic Kitten will reform to play a concert in support of jailed Liverpool football fan Michael Shields.

Anonymous

# Chambers @ Tuesday, June 05, 2007 3:21 PM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Tracy @ Tuesday, June 05, 2007 3:36 PM

London-born rapper Sway is to be honoured at the BET Hip-Hop awards in the US.

Anonymous

# Stamps @ Tuesday, June 05, 2007 3:51 PM

The Red Hot Chili Peppers are leading the way at this years MTV Europe music awards with four nominations.

Anonymous

# Crews @ Tuesday, June 05, 2007 4:07 PM

Social networking site MySpace is to block users from uploading copyrighted music to its pages.

Anonymous

# Bryant @ Tuesday, June 05, 2007 4:23 PM

Alec Baldwin asks for his voice to be removed from an &amp;quot;unfair&amp;quot; documentary about Arnold Schwarzenegger.

Anonymous

# Zimmer @ Tuesday, June 05, 2007 4:35 PM

Social networking site MySpace is to block users from uploading copyrighted music to its pages.

Anonymous

# Coggins @ Tuesday, June 05, 2007 4:51 PM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Battaglia @ Tuesday, June 05, 2007 5:06 PM

TV host Oprah Winfrey gives audience members $1,000 (526) each to donate to a charitable cause.

Anonymous

# Kincaid @ Tuesday, June 05, 2007 5:20 PM

London-born rapper Sway is to be honoured at the BET Hip-Hop awards in the US.

Anonymous

# Mccartney @ Tuesday, June 05, 2007 5:36 PM

Veteran actor William Franklyn, known for voicing the 1960s Schweppes TV adverts, dies aged 81.

Anonymous

# Wynne @ Tuesday, June 05, 2007 5:50 PM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Crayton @ Tuesday, June 05, 2007 6:06 PM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Crayton @ Tuesday, June 05, 2007 6:07 PM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Verdin @ Tuesday, June 05, 2007 6:20 PM

The judge who put coded messages in his Da Vinci Code plagiarism trial ruling has written another.

Anonymous

# Cooks @ Tuesday, June 05, 2007 6:36 PM

The judge who put coded messages in his Da Vinci Code plagiarism trial ruling has written another.

Anonymous

# Halstead @ Tuesday, June 05, 2007 6:50 PM

The judge who put coded messages in his Da Vinci Code plagiarism trial ruling has written another.

Anonymous

# Sisco @ Tuesday, June 05, 2007 7:06 PM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Robison @ Tuesday, June 05, 2007 7:20 PM

Madonna says she may adopt another child from abroad following her proposed adoption of a Malawian boy.

Anonymous

# Witt @ Tuesday, June 05, 2007 7:34 PM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Sanders @ Tuesday, June 05, 2007 7:51 PM

Pop trio Atomic Kitten will reform to play a concert in support of jailed Liverpool football fan Michael Shields.

Anonymous

# Masterson @ Tuesday, June 05, 2007 8:03 PM

The Rolling Stones cancel a gig in Hawaii and postpone other tour dates as Mick Jagger suffers throat troubles.

Anonymous

# Reiter @ Tuesday, June 05, 2007 8:18 PM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Gaffney @ Tuesday, June 05, 2007 8:33 PM

Social networking site MySpace is to block users from uploading copyrighted music to its pages.

Anonymous

# Folk @ Tuesday, June 05, 2007 8:48 PM

London-born rapper Sway is to be honoured at the BET Hip-Hop awards in the US.

Anonymous

# Chang @ Tuesday, June 05, 2007 9:01 PM

Veteran actor William Franklyn, known for voicing the 1960s Schweppes TV adverts, dies aged 81.

Anonymous

# Brewster @ Tuesday, June 05, 2007 9:17 PM

Jonathan Ross is dubbed &amp;quot;risque&amp;quot; by Ofcom but not in breach of rules over an interview with David Cameron.

Anonymous

# Norfleet @ Tuesday, June 05, 2007 9:33 PM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Matlock @ Tuesday, June 05, 2007 9:47 PM

Veteran actor William Franklyn, known for voicing the 1960s Schweppes TV adverts, dies aged 81.

Anonymous

# Acuna @ Tuesday, June 05, 2007 10:04 PM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Creech @ Tuesday, June 05, 2007 10:18 PM

TV host Oprah Winfrey gives audience members $1,000 (526) each to donate to a charitable cause.

Anonymous

# Mccrary @ Tuesday, June 05, 2007 10:34 PM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Tice @ Tuesday, June 05, 2007 10:50 PM

The judge who put coded messages in his Da Vinci Code plagiarism trial ruling has written another.

Anonymous

# Mcneil @ Tuesday, June 05, 2007 11:04 PM

The Red Hot Chili Peppers are leading the way at this years MTV Europe music awards with four nominations.

Anonymous

# Christenson @ Tuesday, June 05, 2007 11:17 PM

Record company EMI sign a deal with the estate of crooner Dean Martin to use the singer's likeness.

Anonymous

# Farrell @ Tuesday, June 05, 2007 11:33 PM

Pioneering screenwriter Nigel Kneale, best known for the Quatermass TV serials and films, dies aged 84.

Anonymous

# Sauer @ Tuesday, June 05, 2007 11:47 PM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Folse @ Wednesday, June 06, 2007 12:02 AM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Graham @ Wednesday, June 06, 2007 12:18 AM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Arroyo @ Wednesday, June 06, 2007 12:49 AM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Wentworth @ Wednesday, June 06, 2007 1:06 AM

Record company EMI sign a deal with the estate of crooner Dean Martin to use the singer's likeness.

Anonymous

# Marcotte @ Wednesday, June 06, 2007 1:20 AM

Record company EMI sign a deal with the estate of crooner Dean Martin to use the singer's likeness.

Anonymous

# Batiste @ Wednesday, June 06, 2007 1:36 AM

A musical about the witches from The Wizard of Oz breaks West End box office records, its producers say.

Anonymous

# Guy @ Wednesday, June 06, 2007 1:51 AM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Creech @ Wednesday, June 06, 2007 2:06 AM

The Rolling Stones cancel a gig in Hawaii and postpone other tour dates as Mick Jagger suffers throat troubles.

Anonymous

# Ireland @ Wednesday, June 06, 2007 2:34 AM

Social networking site MySpace is to block users from uploading copyrighted music to its pages.

Anonymous

# Duke @ Wednesday, June 06, 2007 2:49 AM

Microsoft and Peter Jackson postpone the making of a film based on the Halo video game after backers pull out.

Anonymous

# Moya @ Wednesday, June 06, 2007 3:03 AM

Doctor Who takes three prizes at the National Television Awards in a repeat of its success last year.

Anonymous

# Pinckney @ Wednesday, June 06, 2007 3:20 AM

Pop trio Atomic Kitten will reform to play a concert in support of jailed Liverpool football fan Michael Shields.

Anonymous

# Arthur @ Wednesday, June 06, 2007 3:32 AM

The Rolling Stones cancel a gig in Hawaii and postpone other tour dates as Mick Jagger suffers throat troubles.

Anonymous

# Monk @ Wednesday, June 06, 2007 3:49 AM

Record company EMI sign a deal with the estate of crooner Dean Martin to use the singer's likeness.

Anonymous

# Wahl @ Wednesday, June 06, 2007 4:04 AM

Pop trio Atomic Kitten will reform to play a concert in support of jailed Liverpool football fan Michael Shields.

Anonymous

# Batiste @ Wednesday, June 06, 2007 4:18 AM

Record company EMI sign a deal with the estate of crooner Dean Martin to use the singer's likeness.

Anonymous

# Middleton @ Wednesday, June 06, 2007 4:35 AM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Eng @ Wednesday, June 06, 2007 4:52 AM

A musical about the witches from The Wizard of Oz breaks West End box office records, its producers say.

Anonymous

# Hurst @ Wednesday, June 06, 2007 5:06 AM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Stoker @ Wednesday, June 06, 2007 5:23 AM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Stoker @ Wednesday, June 06, 2007 5:23 AM

Colombia's vice president is &amp;quot;baffled&amp;quot; by Kate Moss's success following cocaine allegations.

Anonymous

# Fagan @ Wednesday, June 06, 2007 5:39 AM

Singer George Michael lends the piano on which John Lennon wrote Imagine to an anti-war exhibition.

Anonymous

# Watts @ Wednesday, June 06, 2007 5:54 AM

William Styron, whose Holocaust novel Sophie's Choice became a film and an opera, has died, aged 81.

Anonymous

# Ingle @ Wednesday, June 06, 2007 6:12 AM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Rush @ Wednesday, June 06, 2007 6:24 AM

Jonathan Ross is dubbed &amp;quot;risque&amp;quot; by Ofcom but not in breach of rules over an interview with David Cameron.

Anonymous

# Mayo @ Wednesday, June 06, 2007 6:41 AM

The Red Hot Chili Peppers are leading the way at this years MTV Europe music awards with four nominations.

Anonymous

# Wade @ Wednesday, June 06, 2007 6:54 AM

Singer George Michael lends the piano on which John Lennon wrote Imagine to an anti-war exhibition.

Anonymous

# Knight @ Wednesday, June 06, 2007 7:22 AM

Veteran actor William Franklyn, known for voicing the 1960s Schweppes TV adverts, dies aged 81.

Anonymous

# Blount @ Wednesday, June 06, 2007 7:37 AM

Doctor Who takes three prizes at the National Television Awards in a repeat of its success last year.

Anonymous

# Benedict @ Wednesday, June 06, 2007 7:52 AM

The judge who put coded messages in his Da Vinci Code plagiarism trial ruling has written another.

Anonymous

# Coons @ Wednesday, June 06, 2007 8:07 AM

William Styron, whose Holocaust novel Sophie's Choice became a film and an opera, has died, aged 81.

Anonymous

# Forster @ Wednesday, June 06, 2007 8:23 AM

Borat creator Sacha Baron Cohen reportedly signs a $42.5m (22m) film deal starring his character Bruno.

Anonymous

# Andrus @ Wednesday, June 06, 2007 8:38 AM

Microsoft and Peter Jackson postpone the making of a film based on the Halo video game after backers pull out.

Anonymous

# Handy @ Wednesday, June 06, 2007 8:53 AM

The judge who put coded messages in his Da Vinci Code plagiarism trial ruling has written another.

Anonymous

# Ramey @ Wednesday, June 06, 2007 9:10 AM

Microsoft and Peter Jackson postpone the making of a film based on the Halo video game after backers pull out.

Anonymous

# Norman @ Wednesday, June 06, 2007 9:25 AM

The first stage of a 150m investment in regional museums is praised for boosting visitor numbers.

Anonymous

# Schulze @ Wednesday, June 06, 2007 9:39 AM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Mintz @ Wednesday, June 06, 2007 9:55 AM

Jonathan Ross is dubbed &amp;quot;risque&amp;quot; by Ofcom but not in breach of rules over an interview with David Cameron.

Anonymous

# Gant @ Wednesday, June 06, 2007 10:10 AM

Doctor Who takes three prizes at the National Television Awards in a repeat of its success last year.

Anonymous

# Baxter @ Wednesday, June 06, 2007 10:25 AM

Veteran actor William Franklyn, known for voicing the 1960s Schweppes TV adverts, dies aged 81.

Anonymous

# Blackwood @ Wednesday, June 06, 2007 10:40 AM

Veteran game show host Bob Barker is stepping down from hosting The Price is Right after 35 years.

Anonymous

# Gallo @ Wednesday, June 06, 2007 10: