10

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?

LopDev
  • 823
  • 10
  • 26
BDB
  • 429
  • 3
  • 16

2 Answers2

4

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.

BDB
  • 429
  • 3
  • 16
  • 1
    I would love to se your configuration of the task in DevOps for this? – Choco Jun 09 '22 at 13:04
  • Helpful but the poster asked how to get this working in DevOps. It would be helpful to see your .yml file to see how you set this up to get this working in DevOps. – John81 Sep 15 '22 at 15:51
0

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:

enter image description here

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.

Yang Shen - MSFT
  • 1,136
  • 1
  • 7
  • 9
  • Thank you @Yang Shen for your response. I found a simpler way, otherwise I would have approached your way. Sorry, I couldn't up-vote your solution because of my reputation limit, though it deserves. – BDB Mar 18 '20 at 14:54
  • Glad to know you found a simpler way, it's correct to mark the best solution always. And please don't feel sorry for the up-vote thing. Thanks very much! – Yang Shen - MSFT Mar 19 '20 at 07:50
  • Out of curiosity, do you happen to know why in most examples on the net the `Obsolete` attribute in excluded attributes list is the only one not having the `XyzAttribute` form? As if it's a requirement for any other attributes than that one? – Crono Dec 10 '20 at 14:01