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

25,657

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

Share:
25,657
Dutts
Author by

Dutts

Software Engineer with over 15 years commercial experience in C, C++ and C# .Net. Current projects involve C#, .Net and F#. Blogging interesting tech stuff I come across on my travels at http://www.dutton.me.uk.

Updated on May 18, 2020

Comments

  • Dutts
    Dutts about 4 years

    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.