OpenCover 无法检测 .exe,因为无法加载 PDB/MDB

OpenCover Cannot instrument .exe as no PDB/MDB could be loaded

我有一个在 VS2015 下工作的 ASP.NET 核心项目(目标为 .NETFramework,版本=v4.6.1)。 当我转换到 VS2017 时,我无法使 OpenCover 工作。 当 build.PS1 运行时

C:\OpenCover.6.519\tools\OpenCover.Console.exe -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:" test "C:\GitRepos\AdminPortal\Source\test\AdminPortal.UnitTests\AdminPortal.UnitTests.csproj" " -output:"C:\GitRepos\AdminPortal\Build..\OUTPUT\Test-Output\projectCoverageReport.xml" -log:Verbose -oldStyle

输出(为简洁起见被截断)如下:

C:\OpenCover.6.519\tools\OpenCover.Console.exe -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:" test "C:\GitRepos\AdminPortal\Source\test\AdminPortal.UnitTests\AdminPortal.UnitTests.csproj" " -output:"C:\GitRepos\AdminPortal\Build..\OUTPUT\Test-Output\projectCoverageReport.xml" -log:Verbose -oldStyle Executing: C:\Program Files\dotnet\dotnet.exe
Cannot instrument C:\Program Files\dotnet\sdk.0.3\dotnet.dll as no PDB/MDB could be loaded ... Cannot instrument C:\Program Files\dotnet\sdk.0.3\Microsoft.TestPlatform.Build.dll as no PDB/MDB could be loaded
Build started, please wait...
...
Cannot instrument C:\Program Files\dotnet\sdk.0.3\NuGet.Packaging.dll as no PDB/MDB could be loaded
Cannot instrument RefEmit_InMemoryManifestModule as no PDB/MDB could be loaded Build completed.

Test run for C:\GitRepos\AdminPortal\Source\test\AdminPortal.UnitTests\bin\Debug\net461\AdminPortal.UnitTests.dll(.NETFramework,Version=v4.6.1) Cannot instrument C:\Program Files\dotnet\sdk.0.3\vstest.console.dll as no PDB/MDB could be loaded
Cannot instrument C:\Program Files\dotnet\sdk.0.3\Microsoft.TestPlatform.CoreUtilities.dll as no PDB/MDB could be loaded
Cannot instrument C:\Program Files\dotnet\sdk.0.3\Microsoft.VisualStudio.TestPlatform.ObjectModel.dll as no PDB/MDB could be loaded Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0 Copyright (c) Microsoft Corporation. All rights reserved.

Cannot instrument RefEmit_InMemoryManifestModule as no PDB/MDB could be loaded Build completed. Test run for C:\GitRepos\AdminPortal\Source\test\AdminPortal.UnitTests\bin\Debug\net461\AdminPortal.UnitTests.dll(.NETFramework,Version=v4.6.1) Cannot instrument C:\Program Files\dotnet\sdk.0.3\vstest.console.dll as no PDB/MDB could be loaded Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0 Copyright (c) Microsoft Corporation. All rights reserved. Starting test execution, please wait...
... Cannot instrument C:\Program Files\dotnet\sdk.0.3\TestHost\msdia140typelib_clr0200.dll as no PDB/MDB could be loaded
Cannot instrument C:\GitRepos\AdminPortal\Source\test\AdminPortal.UnitTests\bin\Debug\net461\AdminPortal.exe as no PDB/MDB could be loaded ...

Total tests: 39. Passed: 37. Failed: 0. Skipped: 2. Test Run Successful. Test execution time: 6.4838 Seconds

Committing...
No results, this could be for a number of reasons. The most common reasons are:
1) missing PDBs for the assemblies that match the filter please review the output file and refer to the Usage guide (Usage.rtf) about filters.
2) the profiler may not be registered correctly, please refer to the Usage guide and the -register switch.

我在 bin 文件夹中有 .PDB 文件(例如 C:\GitRepos\AdminPortal\Source\test\AdminPortal.UnitTests\bin\Debug\net461\AdminPortal.pdb), 所以我不知道 为什么 opencover 无法加载它。可能是什么原因?

我尝试指定 -targetdir:(按照建议 here )和 -oldStyle ,但没​​有区别。
我还尝试指定 -searchdirs:(来自 https://github.com/sawilde/opencover/wiki/Usage) 并重新注册探查器 regsvr32 /n /i:user C:OpenCover.4.5.3723\x86\OpenCover.Profiler.dll(来自 OpenCover/NUnit can't find PDB files)- 也没有区别。

是否与https://github.com/OpenCover/opencover/issues/610中提到的新便携式PDB格式有关?
但我的目标是 NETFramework v4.6.1,而不是 Core 1.1。我可以显式指定 dotnet testmsbuild 使用哪种 PDB 格式吗?

是否有任何工具可以显示,.PDB 文件是旧格式还是新格式? 我试过 https://github.com/Microsoft/microsoft-pdb/blob/master/cvdump/cvdump.exe as suggested at Reading a .pdb file, 但它没有 return 任何东西。

在不相关的讨论中 https://developercommunity.visualstudio.com/content/problem/15197/vs2017rc-locks-files-in-obj-folder-blocking-builds.html 建议 将项目属性中的 PDB 格式从便携式 PDB 更改为完整 PDB。
如果您喜欢使用 UI,请在 Visual Studio 中打开 Project Properties->Build ->Advanced->Output->Debugging Information 从 Portable 改为 Full。 它将添加到具有特定构建条件的 csproj 文件设置

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard1.6|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>True</DebugSymbols>
  </PropertyGroup>

通常你想将它应用到所有选项,所以删除条件以具有以下内容(如果你之前没有使用 Visual Studio,你可以将下面的 PropertyGroup 粘贴到你的 .csproj)

<PropertyGroup >
    <DebugType>full</DebugType>
    <DebugSymbols>True</DebugSymbols>
  </PropertyGroup>

并且 OpenCover 将能够检测您的项目。

2019 年 4 月 19 日更新:由于 OpenCover 仍然 doesn’t have .net core support #595, consider to use CoverLet,具有与 OpenCover 兼容的输出格式。