To use xUnit for testing your MAUI projects, do the following steps:
(Note: I am using Visual Studio on a Mac)
- Add test project
Right-click on your solution and choose Add > New Project … > xUnit Test Project (you can search for that) > Create - Reference main project in test project
In the test project, add a reference to your main project: Right click on Dependencies in the test project > Add Project Reference … > Choose you project > Select - Edit csproj files
Main project: Add net7.0 (or whichever version you are using) to your target frameworks:
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst;net7.0</TargetFrameworks>
Note: In my experience, you have to add net7.0 at the end. Otherwise the list of target devices for debugging will only contain a default entry, and all other devices are gone.
Change the output type, to create an exe file only if it is not net7.0:
Test project: Add this in the property group where the target frameworks are specified:<OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
<UseMaui>true</UseMaui>
- Optional: Add NuGet packages: NSubstitute and FluentAssertions