maui-unit-testing

Configure .NET MAUI unit tests with xUnit, Moq, and coverlet.

Updated Nov 27, 2025
One-click install
npx skills add https://github.com/seydakaratekeli/KamPay3 --skill maui-unit-testing-seydakaratekeli
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: maui-unit-testing
Source: https://github.com/seydakaratekeli/KamPay3/tree/main/KamPay/.github/skills/maui-unit-testing%20-%20Kopyala
Command: npx skills add https://github.com/seydakaratekeli/KamPay3 --skill maui-unit-testing-seydakaratekeli

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides practical guidance and best practices to make .NET MAUI ViewModels and platform-dependent services reliably unit-testable, avoiding fragile UI-bound tests and platform-specific build pitfalls.

Core Features & Use Cases

  • ViewModel-first testing: patterns to test ViewModel state and commands rather than UI bindings for fast, deterministic tests.
  • Mocking MAUI services: recommended mock strategies for ISecureStorage, IPreferences, IConnectivity, IGeolocation, IFilePicker, IMediaPicker, IDispatcher and abstracting Shell/Application behind interfaces.
  • Test project setup & build gotchas: guidance on creating class library test projects targeting plain net9.0/net10.0, conditional OutputType for app projects, and CI-friendly dotnet test workflows.
  • Code coverage & on-device runners: instructions for using coverlet and xunit.runner.devices when real platform APIs are required.

Quick Start

Create a class library test project targeting net9.0/net10.0, add xUnit, Microsoft.NET.Test.Sdk, Moq and coverlet, reference your MAUI app with a conditional OutputType for the test TFM, and run dotnet test to validate ViewModel tests.

Frequently Asked Questions about maui-unit-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I unit test .NET MAUI ViewModels without triggering UI-bound tests?

Unit test .NET MAUI ViewModels by targeting ViewModel state and commands directly rather than UI bindings. This approach ensures fast, deterministic tests by focusing on logic execution and mocked dependencies instead of fragile platform-specific UI components.

How do I mock MAUI services like ISecureStorage and IPreferences for unit testing?

Mock MAUI services like ISecureStorage and IPreferences using Moq. Abstract platform-dependent services behind interfaces, including IConnectivity, IGeolocation, IFilePicker, and IDispatcher, to isolate ViewModel logic and ensure reliable unit tests.

Why does my .NET MAUI test project fail to build when referencing my app project?

Your .NET MAUI test project build fails because app projects need a conditional OutputType set to build as libraries for the test target framework. Create a class library test project targeting plain net9.0 or net10.0 to resolve this platform-specific build pitfall.

What's the best way to measure code coverage for .NET MAUI unit tests?

The best way to measure code coverage for .NET MAUI unit tests is using coverlet alongside xUnit. Add coverlet to your test project setup to generate coverage reports when running dotnet test workflows for your ViewModel and platform service tests.

Do I need an on-device test runner for .NET MAUI platform services?

You need an on-device test runner like xunit.runner.devices for .NET MAUI platform services when real platform APIs are required. For standard ViewModel logic, mocked MAUI services running on plain net9.0/net10.0 are sufficient without on-device execution.

Can I use xUnit with Moq for .NET MAUI unit testing?

Yes, you can use xUnit with Moq for .NET MAUI unit testing. Add xUnit, Microsoft.NET.Test.Sdk, and Moq to a class library test project targeting net9.0/net10.0 to effectively mock MAUI services and validate ViewModel commands and state.