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

vstest.console.exe generate cover for only Moq.dll

我正在尝试使用 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>

生成的代码覆盖率报告图片:

我遇到了同样的行为,幸好我找到了解决办法:

打开Visual Studio测试任务并:

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

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

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

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

    这是我的 .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>
    

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