I’ve been working on Net Core projects recently. So far, anything is so awesome except for one feature: Code Coverage. It just breaks, with no visible exception thrown. Why is this happening with my Net Core projects only?
Please note that this feature requires Visual Studio Enterprise version. I’m using Visual Studio 2017 Enterprise v15.5.2.
After a few tries, a few restarts only to no avail, I decided to search online for the fixes. The solution turns out to be pretty simple. According to the issue on GitHub and documented at Visual Studio Test’s documentation, the current workaround is to simply specify the DebugType as Full in your .csproj project file as following.
<PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <!-- Add the line below --> <DebugType>Full</DebugType> </PropertyGroup>
For example, you have one project called
MyCompany.MyProduct.Service which stores the code to be tested and one test project called
MyCompany.MyProduct.UnitTests defining the unit tests. In Visual Studio, navigate to your Solution Explorer panel, right click on your
Service project and choose “Edit MyCompany.MyProduct.Service.csproj”. Add the new line as above. Boom, code coverage fixed!
But what does the line do? In short, it tells Visual Studio tools (debugger, test runner, profiler, and others) to use the classic PDB symbol file. We have been using this format for years. However, with the new Net Core’s mindset, we need a cross-platform solution, which is now developed and called Portable PDB. For the details, you can read more about Portable PDB here.
To read the PDB files, our Code Coverage profiler depends on Debug Interface Access SDK (of course, provided by Microsoft). And the DIA APIs are yet to support the new Portable PDB format. Hence, that’s why we have to set the fallback point. But don’t worry, the team promises to have everything sorted out in VS 15.8. Until then, I guess we still have to use the classic PDF format and the workaround as stated above.
I hope this fixed your problems and provides you with a bit of information behind the scene. Happy coding!