Unit Testing .NET MAUI Apps with xUnit

Gerald Versluis · 18K views · 2022 · 9:08

Open on YouTube

Testing, debugging & developer toolsSoftware (other)tutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as the go-to walkthrough when an xUnit or MSTest project can't reference a multi-target .NET MAUI project, usually alongside the fix of adding a core net target and making OutputType conditional.

Recommended by 6 people across 6 different months, 2022 to 2025.

What it is

A short tutorial by Gerald Versluis on adding an xUnit test project to a .NET MAUI solution and getting it to build. It walks through editing the project files, adding a plain net6.0 target to the MAUI project, and using MAUI APIs in tests, with a sample repo.

What people said

Recommends Josh Karabin · 2022-12 · Stack Overflow · on “How to use xUnit for MAUI projects for platform specific code?”

A stock "net6.0" xUnit test in Visual Studio cannot reference net6.0-android and net6.0-iOS libraries, etc. I have seen that your MAUI app can be modified to multi-target net6.0. See [this video] for a tutorial. For running tests with platform-specific code, I've had reasonable success with [LINK] , to run on actual mobile devices. The xUnit team has made runners for Xamarin.Forms in the past - I don't know if at some point they will start to maintain them for MAUI or not.

Recommends Felix Shen · 2023-09 · Stack Overflow · on “MAUI Project is not compatible with net7.0 when I try to build it on windows but it builds on mac”

I recommend you refer to the MauixUnitTestSample sample code [LINK] and watch the awesome Unit Testing tutorial [this video] by Gerald Versluis. Though the tutorial is about .NET6, it also applies to .NET7. To sum up,just make some changes to maui project file: 1.Add net7.0 to TargetFrameworks <PropertyGroup> <TargetFrameworks>net7.0;net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks> 2.Change this line: <OutputType>Exe</OutputType> to <OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType> If you still have any questions, free to ask me. Hope it helps!

Recommends Julian · 2023-09 · Stack Overflow · on “MAUI Project is not compatible with net7.0 when I try to build it on windows but it builds on mac”

Please read how to ask a good question [LINK] and include the relevant parts of the *.csproj* file of your Test project. This answer may solve your problem [LINK] . If you follow the steps that Gerald shows in his video on the topic [this video] , it should work.

Recommends Bruno Guardia · 2024-02 · Stack Overflow · on “How to add an xUnit project to .NET 7 MAUI solution?”

The solution was found and presented by Gerald Versluis in his video Unit Testing .NET MAUI Apps with xUnit [this video] Summary: On your MAUI project, add a core .Net target. My example is with .Net 8 but his is .Net 6, so this should work for any .Net core version. I am adding "net8.0" at the beginning: Before <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks> After <TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks> Then we need, in the same project file, the output to be a Library in order to be consumed by the tests, but only when the target is the core framework. Before <OutputType>Exe</OutputType> After <OutputType Conditi

Recommends David Maw · 2024-06 · Stack Overflow · on “.NET MAUI PackageVersion was expected to have a single value across all target frameworks”

I have a .NET MAUI app that I want to run unit tests against. I already have some MsTest tests but never could get the MsTest project to build. Enter Gerald versluis' fine video on how to do that here [this video] and a working sample using xUnit here [LINK] . Basically he just adds a Net8 target to the original project and goes on from there. Problem is when the project has a version other than 1.0 Nuget restore fails with "...The property PackageVersion was expected to have a single value across all target frameworks, but instead had the following values: 1.0.0, 2.0" (I took Gerald's original code set the version to 2.0 in my test). I'm guessing PackageVersion is normally inherited from Version

Recommends Felix Shen · 2024-10 · Stack Overflow · on “How to create unit tests for MAUI hybrid app?”

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 [LINK] and Unit Testing tutorial [this video] .

Recommends Bhavanesh N · 2025-04 · Stack Overflow · on “testhost.exe doesn't see Microsoft.Android Framework?”

Your unit test project shouldn't target net8.0-android . Instead, change it to .NET 8.0 ( net8.0 ). However, this will cause another issue: since your test project is referencing your MAUI project, and the MAUI project targets net8.0-android , the target frameworks between the two won't match. To fix this issue, follow the steps below: ✅ Steps to Fix Target Framework Mismatch In your MAUI project , add net8.0 to the target frameworks: <TargetFrameworks>net8.0;net8.0-android</TargetFrameworks> To allow your MAUI project to compile as a DLL (which is required by the test project), conditionally set the OutputType : <OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType> For m

Cites Julian · 2022-10 · Stack Overflow · on “MAUI: How to use partial classes for platform specific implementations together with net7.0 as TargetFramework in the SingleProject?”

I am using partial classes to implement platform specific behavior in a .NET MAUI app: Stem: public partial class MyServices { public partial void DoSomething(); } Android/iOS/MacCatalyst/Windows/Tizen specific implementations all look similar to this: public partial class MyServices { public partial void DoSomething() { // Android/iOS/MacCatalyst/Windows/Tizen specific implementation } } So far, so normal for MAUI (although the platform specific implementation could be done differently, but the partial class approach is common for MAUI and seemed convenient). Now, in order to be able to execute unit tests (xUnit), it is necessary to add the net7.0 target to the <TargetFrameworks> in the .cs

Cites Anass · 2023-04 · Stack Overflow · on “.NET MAUI app building but not showing up on Windows after adding a Unit test project”

I have a .NET MAUI project that stopped showing up on Windows platform (on Android it hangs on Hot Reload initializing) after adding a unit test project (xUnit 2.4.2) using .net7.0 . I have referenced my .NET MAUI app as a dependency in my Tests project, following Gerald Versluis on Youtube [this video] and this article [LINK] and I followed all the steps. I only tested in Debug mode. I added the UseMaui property to the Tests.csproj file <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <UseMaui>true</UseMaui> <IsPackable>false</IsPackable> </PropertyGroup> I have added .net7.0 as a target framework and updated the condit