vstest.console.exe仅为Moq.dll生成封面

bal*_*glu 6 c# code-coverage runsettings .net-core vstest.console.exe

我正在尝试使用vstest.console.exe生成代码覆盖率报告.我也使用.runsettings文件并将其作为参数传递.

无论我想做什么,它只为moq.dll生成一个覆盖率报告.

我在下面分享我正在运行的命令参数的全文以及.runsettings文件的内容.任何想法,我在哪里做错了什么?

命令:

vstest.console.exe"C:\ Xyz.Tests\bin\Debug \netcoreapp2.0\Xyz.Tests.dll"/ InIsolation/EnableCodeCoverage /settings:CodeCoverage.runsettings

CodeCoverage.runsettings文件内容:

<RunSettings>
<DataCollectionRunSettings>
  <DataCollectors>
    <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" enabled="false">
      <Configuration>
        <CodeCoverage>
        </CodeCoverage>
      </Configuration>
    </DataCollector>
  </DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
Run Code Online (Sandbox Code Playgroud)

生成的代码覆盖率报告的图像: 在此输入图像描述

Ale*_*ndr 3

我遇到了同样的行为,但幸运的是我找到了解决方案:

打开 Visual Studio 测试任务并:

  • 取消选中Code coverage enabled标志
  • 放入--collect:"Code Coverage"其他控制台选项
  • 编辑项目的.csproj文件,包含测试的类和:

    添加<DebugType>full</DebugType><PropertyGroup>部分

    要避免代码覆盖率结果中出现 moq.dll:

    添加<ModulePath>.*moq.dll</ModulePath><ModulePaths> -> <Exclude>.runsettings 文件的部分

    这是我的 .runsettings

    <?xml version="1.0" encoding="utf-8"?>
    <RunSettings>
      <RunConfiguration>
        <MaxCpuCount>0</MaxCpuCount>
      </RunConfiguration>
      <DataCollectionRunSettings>
        <DataCollectors>
          <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
            <Configuration>
              <CodeCoverage>
                <!-- Match assembly file paths: -->
                <ModulePaths>
                  <Include>
                    <ModulePath>.*\.dll$</ModulePath>
                    <ModulePath>.*\.exe$</ModulePath>
                  </Include>
                  <Exclude>
                    <ModulePath>.*moq.dll</ModulePath>
                    <ModulePath>.*CPPUnitTestFramework.*</ModulePath>
                    <ModulePath>.*TestAdapter.*</ModulePath>
                  </Exclude>
                </ModulePaths>
              </CodeCoverage>
            </Configuration>
          </DataCollector>
        </DataCollectors>
      </DataCollectionRunSettings>
    </RunSettings>
    
    Run Code Online (Sandbox Code Playgroud)

    请查看https://developercommunity.visualstudio.com/content/problem/92905/net-core-unit-testing-code-coverage.html链接

    • 谢谢 Alxander,这个解决方案对我有用。似乎最重要的部分是将 &lt;DebugType&gt;full&lt;/DebugType&gt; 添加到项目中。 (2认同)