locked
Exclude code from code coverage result

    Question

  • Hello, 

    We have a problem in our solution to get the "right" figures for code coverage. We want to exclude some code that we haven't written, for example generated proxy, other generated code, external code etc.

    I have scanned the forums and the Net and it seems like the only option that is possible with the MSTest tools in Visual Studio is to put the <DebuggerNonUserCode>-attribute on top of every class. I have describe it here http://www.marcusoft.net/2008/04/removing-mysettings-from-code-coverage.html.

    That is at best very boring but you'll also need to redo it every time a class is regenerated... which also makes it irritating.

    What I would like is some kind of attribute or flag to send to MSTest to exclude certain stuff from the code coverage measuring. Like for example NCover's "exclude"-option (see http://www.ncover.com/documentation/console/profilingoptions)

    Is there a way to get this behavior? If not - are there any plans on including it in future releases?
    Monday, January 12, 2009 8:47 AM

Answers

  • Hello Marcus,

     

    In addition to DebuggerNonUserCode attribute, you can exclude functions and methods, as well as namespaces, from the coverage collection. To do so, you would need to use command line tools to instrument your assemblies and gather coverage data. Take a look at -EXCLUDE option for VSInstr at http://msdn.microsoft.com/en-us/library/ms182402(VS.80).aspx.

    --Daryush


    Daryush Laqab - MSFT ---------------------- To help our tracking, please mark replies as answers if they resolve your issue.
    • Marked as answer by Roahn Luo Thursday, January 15, 2009 1:37 AM
    Wednesday, January 14, 2009 4:47 PM

All replies

  • Hi Marcus

    The code coverage feature is implemented by code profiler. I'm going to move this thread to the profiler and I believe you will have better luck there. 

    Please mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, January 14, 2009 2:44 AM
  • Hello,

     

    I can’t agree with you more, careful guy! But so far, using the DebuggerNonUserCode attribute to exclude code is what I used to do often. Your suggestion might be of great value if you could submit a suggestion via :

    https://connect.microsoft.com/site/sitehome.aspx?SiteID=210&wa=wsignin1.0

    Our development team will carefully consider that and might release the feature in a near future version of Visual Studio.

     

    Thanks in advance,

    Best regards,


    Roahn
    Wednesday, January 14, 2009 10:20 AM
  • Hello Marcus,

     

    In addition to DebuggerNonUserCode attribute, you can exclude functions and methods, as well as namespaces, from the coverage collection. To do so, you would need to use command line tools to instrument your assemblies and gather coverage data. Take a look at -EXCLUDE option for VSInstr at http://msdn.microsoft.com/en-us/library/ms182402(VS.80).aspx.

    --Daryush


    Daryush Laqab - MSFT ---------------------- To help our tracking, please mark replies as answers if they resolve your issue.
    • Marked as answer by Roahn Luo Thursday, January 15, 2009 1:37 AM
    Wednesday, January 14, 2009 4:47 PM
  • Great! 

    That might just be the answer. However - being a newbie on performance tools - I have two questions:
    1. How do I run mstest under VsInstr, could you supply me with a sample? Do I have to install something on my local developer machine (Visual Studio 2008 Team System) or do I run it on the TFS Server?
    2. Is there a way to do this in a MSBuild script?
    Thank you for your quick answers - I am sorry for being slow.

    /Marcus
    Friday, January 16, 2009 9:18 AM
  • Hello Marcus,

    You should have all the artifacts you need on your local developer box when you install VSTS Team Suite or VSTS Developer. Once you run the VSInstr with /EXCLUDE flag, your assembly is adequately instrumented. All you need to do then would be to run MSTest with /TestContainer flag and you should be able to collect the coverage data you need. Take a look at http://msdn.microsoft.com/en-us/library/ms182489(VS.80).aspx.

    With regards to MSBuild, I am not very familiar with it, but do not see a reason why you cannot script it under MSBuild; that is as long as you have all the necessary artifacts installed and configured on the box ;-)

    --Daryush

    Daryush Laqab - MSFT ---------------------- To help our tracking, please mark replies as answers if they resolve your issue.
    Monday, January 19, 2009 7:22 PM

  • We can exclude  non implimented methods from the code coverage by applying the [ExcludeFromCodeCoverage] attribute to the  method.
    Thursday, April 22, 2010 9:39 AM