如何从VS2012单元测试中的代码覆盖率分析中排除名称以“.Test”结尾的项目

时间:2022-03-02 07:31:36

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.

我的解决方案是使用名为“ProjectName”的项目和“ProjectName”设置的。包含我的单元测试的测试。我想从VS 2012(MS Test)下的代码覆盖率分析中排除测试项目,并成功设法通过向每个测试类添加ExcludeFromCodeCoverage属性来完成此操作,如此处所述。

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.

随着测试类数量的增加,排除整个测试程序集会很不错。我想使用MSDN链接中描述的.runsettings文件,但似乎没有运气。

Here is my .runsettings file:

这是我的.runsettings文件:

<?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).

这会导致为代码覆盖率生成空结果,如果我注释掉整个 块,我会在所有解决方案的项目中获得代码覆盖率,包括测试(正如预期的那样,我只是想确保添加runSettings文件不是不会造成问题。

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.

但同样,我得到空结果。我的印象是,一个空的(或不存在的)Include块将默认包含所有内容,除非与Exclude块匹配,所以我不认为这是严格要求的。

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.

谁能指出我正确的方向?我从另一个问题中看到,我并不是唯一一个试图排除测试的人,但我想在汇编级别进行,MSDN似乎建议我可以。

2 个解决方案

#1


40  

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


0  

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

您可以通过排除项目dll或使用项目名称本身来排除它。例如。 -

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

This MSDN link is useful for it.

此MSDN链接对它很有用。

#1


40  

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


0  

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

您可以通过排除项目dll或使用项目名称本身来排除它。例如。 -

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

This MSDN link is useful for it.

此MSDN链接对它很有用。