How to exclude Projects with names ending in “.Tes

2019-02-04 02:59发布

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.

2条回答
Animai°情兽
2楼-- · 2019-02-04 03:43

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.

查看更多
聊天终结者
3楼-- · 2019-02-04 03:51

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

查看更多
登录 后发表回答