Search code examples
c#azure-devopscode-coveragemstestcoverlet

Is it possible to get Code Coverage of .NET Framework Project using coverlet in Azure DevOps?


I easily configured to get the coverage result for .NET Core Projects in Azure DevOps, but no luck with .NET Framework Projects.
So, I would be so grateful to get suggestion on this because coverlet documentation is clearly saying that we can also use it for .NET Framework Projects. This question is kinda similar to mine but I didn't see any answer there, Can you use Coverlet to get code coverage data in a .NET Framework project?


Solution

  • Finally, I found a simpler solution. Here it is,

    • Add <IsTestProject>true</IsTestProject> in test project file.

    • Run the commend dotnet test/dotnet test /p:CollectCoverage=true being at test project location(where TestProject.csproj exist)

    • You might get following error after running the command,

      The imported project "C:\Program Files\dotnet\sdk\3.1.100\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found.

    • In the project file(which you want to unit test and get code coverage), change following Import statement

      This, <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />

      To, <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />

    This worked for me both locally and with Azure DevOps.

    Note: Don't forget to install Coverlet.msbuild in your test project.

    Update:

    Above approach works only if you don't get ".Microsoft.WebApplication.targets was not found" error. Commenting suggested import statement will make publish fail at the end, which is obvious. So, I ended up using Coverlet.Console and it's working smoothly without any error. But, to use coverlet.console I needed TestProject.dll file instead of project file(.csproj); so I had to add extra build task for test project. Here is the documentation how to install and use Coverlet.console

    Hope this will be helpful for the ones who end up landing here.