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

不问归期 提交于 2021-02-11 17:19:25

问题


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?


回答1:


Yes, you can can code coverage data from a ASP.NET project. And it's simple as @riQQ suggested in this thread.

The following content is for supplements and reproduce @riQQ's answer in above thread.

Prepared a webform app and .net framework unit test, added the coverlet.runsettings file in repo, the content refering Coverlet options supported by VSTest integration:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat code coverage">
        <Configuration>
          <Format>json,cobertura</Format>          
          <Exclude>[coverlet.*.tests?]*,[*]Coverlet.Core*</Exclude> <!-- [Assembly-Filter]Type-Filter -->
          <Include>[coverlet.*]*,[*]Coverlet.Core*</Include> <!-- [Assembly-Filter]Type-Filter -->
          <ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute</ExcludeByAttribute>
          <ExcludeByFile>../dir1/class1.cs,../dir2/*.cs,../dir3/**/*.cs,</ExcludeByFile> <!-- Absolute or relative file paths -->
          <IncludeDirectory>../dir1/,../dir2/,</IncludeDirectory>
          <SingleHit>false</SingleHit>
          <UseSourceLink>true</UseSourceLink>
          <IncludeTestAssembly>true</IncludeTestAssembly>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Configure the VSTest task:

It can generate the coverage file successfully:

Passed   TestMethod1
Results File: D:\a\_temp\TestResults\VssAdministrator_fv-az38_2020-03-17_07_53_28.trx
Attachments:
  D:\a\_temp\TestResults\*******-****-****-****-**********\VssAdministrator_fv-az38 2020-03-17 07_53_18.coverage
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.

Note: If you want to run this build on self hosted agent, you might need to make sure the VS Enterprise is installed, refer to "Cannot find CodeCoverage.exe" on self-hosted agent.




回答2:


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.



来源:https://stackoverflow.com/questions/60707310/is-it-possible-to-get-code-coverage-of-net-framework-project-using-coverlet-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!