我在同一个解决方案中有一个.NET MAUI项目和一个(单元)测试项目。测试项目引用了.NET MAUI项目,测试运行良好。我还想为.NET MAUI项目添加代码覆盖率。但是,我无法让它与coverlet.collector一起工作,因为生成的coverage.cobertura.xml不包括.NET MAUI项目(作为其中一个包)。
为什么它不选择.NET MAUI项目?
以下是我的Tests项目的 proj 文件:
<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>
下面是我的**.NET MAUI**项目的 proj 文件:
<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>
我在Tests项目根目录下运行以下命令:第一个月
...并最终获得以下输出:
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
所有测试均通过,包括涵盖MauiApp.Client的测试。
以下是 coverage.cobertura.xml 文件的内容:
<?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>
正如您所看到的,coverage.cobertura.xml 文件缺少MauiApp.Client包,但是它选择了Service项目,这只是一个普通的.NET 6项目(非MAUI)。
我在我的Windows 10机器本地以及ADO管道中看到了相同的行为。
1条答案
按热度按时间wqsoz72f1#
我在我们的项目中也看到了同样的问题。代码,这是明确涵盖的测试,没有显示在覆盖率报告。
我相信我已经将范围缩小到将一个xaml文件添加到项目中,一旦删除,覆盖范围就会按预期显示。
查看您的项目文件,我可以看到您添加了LoginView.xaml,所以我相信您遇到了同样的问题。我怀疑如果您删除该文件和任何其他xaml文件,那么您将得到一个覆盖率报告。
除了只在代码中创建页面内容之外,我还没有找到解决这个问题的方法,这并不理想。