dik1977
dik1977

Reputation: 21

coverlet.collector not adding a .NET MAUI project to the coverage.cobertura.xml

I have a .NET MAUI project and a (Unit) Tests project in the same solution. The Tests project references the .NET MAUI project and the tests run fine. I want to also add code coverage for the .NET MAUI project. However, I am unable to get this to work with coverlet.collector as the produced coverage.cobertura.xml does not include the .NET MAUI project (as one of the packages).

Why is it not picking up the .NET MAUI project?

Below is my proj file for the Tests project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <IsPackable>false</IsPackable>
        <EnableNETAnalyzers>true</EnableNETAnalyzers>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
        <PackageReference Include="xunit" Version="2.4.2" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="3.2.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="FluentAssertions" Version="6.8.0" />
        <PackageReference Include="NSubstitute" Version="4.4.0" />
    </ItemGroup>
    <ItemGroup>
      <ProjectReference Include="..\..\Source\MauiApp.Client\MauiApp.Client.csproj" />
      <ProjectReference Include="..\..\Source\Service\Service.csproj" />
    </ItemGroup>
    <ItemGroup>
      <Folder Include="Service\" />
    </ItemGroup>
</Project>

Below is my proj file for the .NET MAUI project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <!--<TargetFrameworks>net6.0-ios;net6.0</TargetFrameworks>-->
        <TargetFrameworks>net6.0</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net6.0-windows10.0.19041.0;$(TargetFrameworks)</TargetFrameworks>
        <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
        <!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
        <OutputType Condition="'$(TargetFramework)' != 'net6.0'">Exe</OutputType>
        <RootNamespace>$(MSBuildProjectName.Replace("-", "_"))</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <!-- Display name -->
        <ApplicationTitle>GridSwift</ApplicationTitle>
        <!-- App Identifier -->
        <ApplicationId>someapp.gist</ApplicationId>
        <ApplicationIdGuid>CCB22D0C-6528-4C55-9BB7-2CAB312363AB</ApplicationIdGuid>
        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>
        <!-- <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.4</SupportedOSPlatformVersion>-->
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</TargetPlatformMinVersion>
        <DefaultLanguage>en-us</DefaultLanguage>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-windows10.0.19041.0|AnyCPU'">
      <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-windows10.0.19041.0|AnyCPU'">
      <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
    </PropertyGroup>
    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />
        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Identity.Client" Version="4.48.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
        <PackageReference Include="ReduxSimple" Version="3.7.0" />
    </ItemGroup>
    <ItemGroup>
      <MauiXaml Update="Views\LoginView.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
    </ItemGroup>
</Project>

I run the following command under the Tests project root: dotnet test --collect:"XPlat Code Coverage"

...and eventually get the following output:

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed!  - Failed:     0, Passed:    22, Skipped:     0, Total:    22, Duration: 199 ms - Tests.dll (net6.0)
Attachments:
  C:\Users\T702180\code\MyApp\Testing\Tests\TestResults\4a6ce0bf-8748-4f58-9fe9-b3ae8a0958a0\coverage.cobertura.xml

All the tests pass, including the ones covering the MauiApp.Client.

Below are the contents of the coverage.cobertura.xml file:

<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="0.37929999999999997" branch-rate="0.3285" version="1.9" timestamp="1670364272" lines-covered="239" lines-valid="630" branches-covered="23" branches-valid="70">
  <sources>
    <source>C:\Users\T702180\code\MyApp\Source\Service\</source>
  </sources>
  <packages>
    <package name="Service" line-rate="0.37929999999999997" branch-rate="0.3285" complexity="211">
      ...
    </package>
  </packages>
</coverage>

As you can see, the coverage.cobertura.xml file is missing the MauiApp.Client package. However, it picks-up the Service project, which is just a plain .NET 6 project (non-MAUI).

I am seeing the same behavior locally on my Windows 10 machine as well as in the ADO pipeline.

Upvotes: 1

Views: 482

Answers (2)

Jean-Pierre Poulin
Jean-Pierre Poulin

Reputation: 1

I just had the problem and solved it by adding:

<PackageReference Include="coverlet.msbuild" Version="6.0.2" />

I think you need to use the same version as the package "coverlet.collector" . In my case, I use the one below:

<PackageReference Include="coverlet.collector" Version="6.0.2" />

Upvotes: 0

Andrew Sankey
Andrew Sankey

Reputation: 11

I'm seeing this same issue in our projects. Code, which is clearly covered by tests, are not showing in the coverage report.

I believe I have narrowed this down to having a xaml file added to the project, when once removed, the coverage shows as expected.

Looking at your project file I can see you have LoginView.xaml added, so I believe you're running into the same issue. I suspect if you remove that file and any other xaml files, then you will get a coverage report.

I've found no work around to this yet other than creating the page content within code only, which is not ideal.

Upvotes: 1

Related Questions