Attribute to include in code coverage. RRS feed

  • Question

  • When writing unit tests I realized that I would like to get coverage for only some code and ignore most of the code.
    For example I have around 100 methods out of which I want code coverage for only 10 methods.

    I userstand there is an attribute [ExcludeFromCodeCoverage] but adding this attribute to 90 methods is tedious.
    I would like to have some thing like [IncludeInCodeCoverage] to include only method with this attribute in code coverage and ignore the rest.

    I created a custom attribute and tried including in runsettings file as below

         <!--Don't forget "Attribute" at the end of the name -->

    This did not work. Kindly help me in solving the issue.

    Wednesday, November 19, 2014 7:03 PM


  • Hi Moin.Faisal,

    By default, the Visual Studio Code Coverage tool analyzes all solution assemblies (.exe/.dll) that are loaded during unit tests.

    If you want to collect code coverage for only 10 methods, you have to exclude them manually in runsettings file or through setting ExcludeFromCodeCoverageAttribute for each method.

    If some methods are in the same assembly, you can exclude the assembly directly from <Exclude> <ModulePath> instead of setting multiple ExcludeFromCodeCoverageAttribute for those methods, which will save some time.

    There is no such [IncludeInCodeCoverage] attribute. For your requierement,you could submit this feature request:


    The Visual Studio product team is listening to user voice there. You can send your ideas/suggestions there and people can vote.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Amanda Zhu Friday, November 28, 2014 9:43 AM
    Thursday, November 20, 2014 7:16 AM