.NET MAUI: Testing with xUnit

      Keine Kommentare zu .NET MAUI: Testing with xUnit

To use xUnit for testing your MAUI projects, do the following steps:
(Note: I am using Visual Studio on a Mac)

  1. Add test project
    Right-click on your solution and choose Add > New Project … > xUnit Test Project (you can search for that) > Create

  2. 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

  3. 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:
    <OutputType Condition="'$(TargetFramework)' != 'net7.0'">Exe</OutputType>
    Test project: Add this in the property group where the target frameworks are specified:
    <UseMaui>true</UseMaui>
  4. Optional: Add NuGet packages: NSubstitute and FluentAssertions