Search code examples
c#unit-testingvisual-studio-2010code-analysisfxcop

Get percent code coverage with FxCop


I am currently trying to write a unit test library for a code base in C#. I want to view the percent code coverage my tests are achieving. I am using VS2010 which does not have any build in code analysis tools. I have downloaded FxCop and included it at an external tool in VS2010, but I don't know how to get the code coverage percentage. I believe it would be done by setting rules for that, but I am not sure. I have looked into the rule sets and how to create custom rules, but I am not sure how to implement it or what rule set libraries I would use.

I am completely new to FxCop and external analysis tools in general, so a step by step or any direction you can give would be great!


Solution

  • As far as I know FxCop, any version even the newest CodeAnalysis 2017, doesn't import code coverage data.

    You can try the tool NDepend. It can import code coverage data from VisualStudio coverage, NCover and dotCover. Then you can harness this code coverage data in a number of way:

    • Write a rule that checks that all classes with a certain attribute (like an attribute named FullCoveredAttribute) are 100% covered
    • Write a rule that checks that all classes that were 100% covered at the baseline, are still 100% covered (NDepend has the notion of baseline to check evolution)
    • Write a rule to compare code coverage with various other code metrics, like Cyclomatic Complexity, it is recommended that complex classes and methods gets 100% covered
    • Visualize Code Coverage data with the metric view based on colored treemaping. In the screenshot below, small rectangle are methods. The area is proportional to #lines of code and the color depends on the % code coverage (green = 100% yellow = 80% red = 0%)

    NDepend metric treemap colored code coverage