Toto
Toto

Reputation: 970

How to create unit tests for MAUI hybrid app?

I am working on a MAUI Hybrid app, and I want to add unit tests for it. At first it was a regular MAUI app. I was able to create a xUnit test project for it. The csproj looked like this:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
    </PropertyGroup>
....

Now using this kind of project for a MAUI Hybrid app does not work:

Project is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Project supports:

  • net8.0-android34.0 (.NETCoreApp,Version=v8.0)
  • net8.0-ios17.5 (.NETCoreApp,Version=v8.0)
  • net8.0-maccatalyst17.5 (.NETCoreApp,Version=v8.0)
  • net8.0-windows10.0.19041 (.NETCoreApp,Version=v8.0)

Project targets 'net8.0-android;net8.0-ios;net8.0-maccatalyst;net8.0-windows10.0.19041.0'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v8.0'

Unfortunately there are no info out there regarding MAUI hybrid project, only regular MAUI project so I have not found guidance.

-> How to create a xUnit project (is that possible in a first place, if not which kind of unit test project work ?) for MAUI Hybrid ?

Thanks for any help with this

Upvotes: 0

Views: 160

Answers (1)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 8070

Please try the following steps,

1.Change your MAUI csproj, add net8.0 to TargetFrameworks

<PropertyGroup>
    <TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>

2.Still in MAUI csproj:

Replace

<OutputType>Exe</OutputType>

with

<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>

For more info, you may refer to the MauixUnitTestSample sample code and Unit Testing tutorial.

Upvotes: 1

Related Questions