4

Is it at all possible to exclude Class Libraries or code files from code metrics? I cannot find good resources on this as they all seem to focus on Code Coverage, which can be set in a .runsettings file.

I would like to have a build without warnings on lines-of-code on test projects.

Aage
  • 5,932
  • 2
  • 32
  • 57
  • When you say code metrics do you mean code analysis? If so you can suppress code analysis warnings from code analysis window by right clicking the warning and doing suppress in file or code. As for mormal warnings I'm unsure about. Youbcouldbset the TreatWarningsAsErrors to force you to fix them but that's overkill – Kyle Mar 20 '15 at 07:33
  • I'm guessing you dont mean code analysis but looks from my google search – Kyle Mar 20 '15 at 07:35
  • 1
    No, I mean code metrics. You can right-click a project > Analyze > Calculate Code Metrics. This will give you insight in the depth-of-inheritance, f.i. But I do not need this for test projects. – Aage Mar 20 '15 at 07:36
  • What ahout using the https://msdn.microsoft.com/en-us/library/system.codedom.compiler.generatedcodeattribute(v=vs.110).aspx this page http://geekswithblogs.net/terje/archive/2008/11/10/hiding-generated-code-from-code-analysis-metrics-and-test-coverage.aspx seems to indicate it could work although page is quite old and is this really the right way. – Kyle Mar 20 '15 at 07:43
  • Yeah, I found that too. I'm not willing to abuse that attribute for this purpose. See my answer for a solution. – Aage Mar 20 '15 at 07:55

2 Answers2

5

Apparently, there is a Files to ignore field on the build template, which I haven't noticed for months:

Code Metrics > Files to ignore.

This takes a regular expression, so I can just put:

*.Tests.dll

This excludes my test libraries.

Aage
  • 5,932
  • 2
  • 32
  • 57
  • 2
    I am using Visual studio community 2019 and I cannot find this options "Files to ignore". Is there such an option now, or have they removed? If yes can you guide me where to find it? – SoftDev30_15 Sep 20 '19 at 19:16
0

You have to separate your solution into two projects. One for your program, one for tests. Then you you just have to run code analysis on the program project. And remember that your tests have also to be clean

ZwoRmi
  • 1,093
  • 11
  • 30
  • It's a valid point that tests also need to be clean. However, I'm not prepared to seperate the solution into two, that doesn't make sense to me. See my answer for the solution I found. – Aage Mar 20 '15 at 07:53