How to exclude Projects with names ending in “.Test” from my code coverage analysis in VS2012 Unit Tests

有些话、适合烂在心里 提交于 2019-12-18 11:46:47

问题


My solution is set up with projects called "ProjectName" with "ProjectName".Tests containing my unit tests. I'd like to exclude the test projects from the code coverage analysis under VS 2012 (MS Test) and have successfully managed to do this by adding the ExcludeFromCodeCoverage attribute to each test class as described here.

As the number of tests classes is growing it would be nice to exclude the entire Test assemblies. I want to use the .runsettings file also described in that MSDN link but don't seem to be having any luck.

Here is my .runsettings file:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <Configuration>
          <CodeCoverage>
            <ModulePaths>
              <Exclude>
                <ModulePath>.*tests.*</ModulePath>
                <ModulePath>.*Tests.*</ModulePath>>
              </Exclude>
            </ModulePaths>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

This results in Empty results generated for Code Coverage, if I comment out the whole <Exclude> block I get code coverage across all of the solution's projects including the Tests (as expected, I just wanted to ensure that the addition of the runSettings file wasn't causing issues itself).

I have tried adding in:

<Include>
  <ModulePath>.*\.dll$</ModulePath>
  <ModulePath>.*\.exe$</ModulePath>
</Include>

But again, I get Empty Results. I was under the impression that an empty (or non-existent) Include block will include everything by default unless matched by the Exclude block, so I don't think this is strictly required.

Can anyone point me in the right direction? I see from this other question that I'm not alone in trying to exclude tests, but I would like to do it at assembly level and MSDN seems to suggest I can.


回答1:


There is a connection with the period issue as it was mentioned here. If you change the exclude section to this

<ModulePath>.*tests.dll</ModulePath>
<ModulePath>.*Tests.dll</ModulePath>

or this

<ModulePath>.*\.tests\..*</ModulePath>
<ModulePath>.*\.Tests\..*</ModulePath>

it'll work




回答2:


You can exclude it by excluding the project dll or by using project name itself also. E.g. -

<ModulePaths>
 <Exclude>
  <ModulePath>Fabrikam.Math.UnitTest.dll</ModulePath>  
    <!-- You can add more ModulePath nodes here. -->
 </Exclude> 
</ModulePaths>

This MSDN link is useful for it.



来源:https://stackoverflow.com/questions/18508534/how-to-exclude-projects-with-names-ending-in-test-from-my-code-coverage-analy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!