Testing a .NET MAUI application across Android, iOS, macOS, Mac Catalyst, and Windows can be challenging. Each platform has its own runtime, native APIs, device capabilities, and UI behavior. A feature that works correctly on an Android emulator may behave differently on a physical iPhone or a Windows desktop.
Traditional unit testing is useful for validating business logic, ViewModels, validation rules, and service classes. However, it cannot verify every platform-specific behavior. For example, a regular unit test cannot fully validate how an application interacts with Android permissions, iOS native APIs, or platform-specific control handlers.
The newer device test project templates introduced for .NET MAUI provide another approach. These templates allow developers to create test projects that run inside a platform application process on a device, simulator, or desktop target.
They use Microsoft.Testing.Platform (MTP) and MSTest, allowing platform-specific tests to run through the .NET testing workflow.
In this article, we will explore the new .NET MAUI device test templates, understand how they work, create platform-specific test projects, write practical tests, and discuss how to use them in a real application development workflow.
Understanding Device Testing in .NET MAUI
Before exploring the new templates, it is important to understand the difference between unit testing and device testing.
A traditional unit test runs on the host machine. It tests a specific method or class without necessarily creating an Android, iOS, or Windows application process.
For example, consider a service that calculates the total price of an order.
public class OrderService
{
public decimal CalculateTotal(
decimal price,
int quantity)
{
return price * quantity;
}
}
A normal unit test can verify this method without launching a mobile application.
However, consider a different requirement: testing whether a platform-specific API returns the expected device information or whether a native implementation behaves correctly.
Such tests may require the actual platform runtime and application environment.
This is where device testing becomes useful.
A device test executes inside a platform application process. Instead of running only as a regular desktop test, the test project is built and deployed to the selected platform.
For example:
Android tests run inside an Android application process.
iOS tests run inside an iOS application process.
macOS tests run inside a macOS application process.
Mac Catalyst tests run inside a Mac Catalyst application process.
The new templates make it easier to create these platform-specific test projects without manually assembling the entire project structure.
What Are the New .NET MAUI Device Test Templates?
The .NET 11 SDK introduces Microsoft.Testing.Platform-based test project templates for Android and Apple platforms. These templates were added in .NET 11 RC 1.
The available template names include:
Template | Target platform | Execution environment |
|---|
androidtest
| Android | Connected device or emulator |
iostest
| iOS | Connected device or simulator |
tvostest
| tvOS | Connected device or simulator |
macostest
| macOS | Desktop application process |
maccatalysttest
| Mac Catalyst | Desktop application process |
These templates create MSTest projects configured to run as platform applications.
The important difference is that the test process runs within the target platform environment. This allows tests to access platform APIs that are not available to ordinary host-based unit tests.
The templates also integrate with dotnet test through Microsoft.Testing.Platform.
Before using them, verify that the installed .NET SDK includes the required templates:
dotnet --version
You can also inspect the templates available in your environment:
dotnet new list
If the templates are not available, check that the correct .NET SDK and platform workloads are installed.
Understanding Microsoft.Testing.Platform
Microsoft.Testing.Platform is a lightweight testing platform designed to execute and report .NET tests.
It is important to understand that a testing framework and a test platform are not the same thing.
A testing framework provides the attributes, assertions, and test lifecycle. A testing platform discovers and executes tests and integrates with development tools and command-line workflows.
For example:
Component | Responsibility |
|---|
MSTest | Provides test attributes and assertions |
Microsoft.Testing.Platform | Executes tests and reports results |
.NET SDK | Provides project templates and build tooling |
Android or Apple tooling | Builds, deploys, and runs the platform application |
Emulator or physical device | Provides the target execution environment |
The new device templates use MSTest for writing tests and Microsoft.Testing.Platform for execution.
This approach is different from creating an ordinary xUnit or MSTest class library that targets the host .NET runtime.
A regular test project does not automatically gain access to native platform APIs simply because it references a .NET MAUI application.
Creating an Android Device Test Project
Let's start by creating an Android device test project.
Make sure the Android SDK and required .NET Android workload are installed. You should also have an Android emulator running or a physical Android device connected.
Open a terminal and execute:
dotnet new androidtest -n MyAndroidTests
This creates a new Android test project named MyAndroidTests.
Navigate to the project directory:
cd MyAndroidTests
The project is configured as a platform application that hosts and executes tests.
You can now run the tests using:
dotnet test
Alternatively, execute the command from the solution directory:
dotnet test --project ./MyAndroidTests/MyAndroidTests.csproj
The command builds the test application, deploys it to the selected Android target, and executes the tests inside the Android application process.
You need to start an emulator or connect a compatible device before running the command.
If multiple devices are connected, make sure the intended device is selected by your Android tooling.
Creating an iOS Device Test Project
The iOS template follows a similar workflow.
Create the project using:
dotnet new iostest -n MyiOSTests
Navigate to the project:
cd MyiOSTests
Run the tests:
dotnet test
Unlike Android development, iOS builds require Apple's development tools, which run on macOS.
If you are developing on Windows, you need access to a Mac build host configured for .NET MAUI iOS development.
You also need an available iOS simulator or a properly provisioned physical device.
For a physical device, signing and provisioning requirements must be configured correctly.
The test application runs in the iOS application process, allowing it to access supported iOS platform functionality.
Creating Test Projects for Other Apple Platforms
The templates also support additional Apple platforms.
tvOS
Create a tvOS test project:
dotnet new tvostest -n MyTvOSTests
The project runs in a tvOS application process on a compatible device or simulator.
This is useful when validating functionality that depends on tvOS-specific APIs or behavior.
macOS
Create a macOS test project:
dotnet new macostest -n MyMacTests
Unlike mobile device tests, this project runs as a desktop application process.
It can be used to validate behavior that requires the macOS platform environment.
Mac Catalyst
Create a Mac Catalyst test project:
dotnet new maccatalysttest -n MyMacCatalystTests
Mac Catalyst allows applications to run on macOS using Apple's Mac Catalyst technology.
The test project executes within a Mac Catalyst application process, making it useful for validating platform-specific behavior.
Writing Your First Device Test
After creating a device test project, you can write tests using MSTest attributes.
The following example demonstrates a simple test class.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyAndroidTests;
[TestClass]
public class DeviceTests
{
[TestMethod]
public void ApplicationPlatform_ShouldBeAndroid()
{
var platform = DeviceInfo.Platform;
Assert.AreEqual(
DevicePlatform.Android,
platform);
}
}
This test verifies that the application is executing on Android.
The example uses DeviceInfo, which is available through .NET MAUI Essentials functionality.
Because this test runs in an Android application process, it can access the platform environment provided by the application.
A similar test can be created for other platform-specific test projects by changing the expected platform.
For example, an iOS test can verify:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyiOSTests;
[TestClass]
public class DeviceTests
{
[TestMethod]
public void ApplicationPlatform_ShouldBeIOS()
{
var platform = DeviceInfo.Platform;
Assert.AreEqual(
DevicePlatform.iOS,
platform);
}
}
These examples demonstrate the test structure. Real device tests should focus on application behavior and platform functionality rather than only checking the platform name.
Testing Platform-Specific Functionality
The real value of device test templates becomes clearer when testing functionality that depends on native APIs.
Consider an application that uses device information to determine the platform and operating system version.
You can create a test that checks whether the operating system version is available.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyAndroidTests;
[TestClass]
public class DeviceInformationTests
{
[TestMethod]
public void OperatingSystemVersion_ShouldNotBeEmpty()
{
var version = DeviceInfo.VersionString;
Assert.IsFalse(
string.IsNullOrWhiteSpace(version),
"Operating system version was not available.");
}
}
This test executes within the Android application process and checks the device information exposed by .NET MAUI.
Other possible testing scenarios include:
Verifying platform-specific device information.
Testing native service implementations.
Checking permission-related behavior under controlled conditions.
Validating platform-specific storage integrations.
Testing native control handlers.
Verifying behavior that depends on the operating system.
However, a device test does not automatically grant access to every protected platform feature. Permissions, provisioning, application configuration, and operating system restrictions still apply.
For example, testing camera access requires appropriate application permissions and a suitable test environment.
Understanding Device Tests, Unit Tests, and UI Tests
Device tests are useful, but they do not replace every other testing approach.
A well-structured .NET MAUI application should use different test types for different responsibilities.
Test type | Where it runs | Main purpose |
|---|
Unit tests | Development machine | Validate isolated business logic |
Device tests | Platform application process | Test platform APIs and native behavior |
UI tests | Device or simulator | Interact with application screens and controls |
Integration tests | Depends on test environment | Validate communication between application components and services |
Unit Testing
Unit tests are appropriate for business logic, validation, calculations, and ViewModels.
For example, an order calculation service does not need an Android emulator to verify its calculations.
Keeping this logic independent makes tests faster and easier to maintain.
Device Testing
Device tests are useful when the code needs an actual platform application process.
For example, a service that reads device-specific information or interacts with a native implementation may need to run in the target environment.
UI Testing
UI testing verifies what users actually experience.
For example, a test may open a login screen, enter credentials, tap a button, and verify that the dashboard appears.
Device test templates do not automatically provide this kind of UI automation.
For cross-platform UI automation, tools such as Appium can be used to interact with application controls on real devices and simulators.
Organizing Tests in a .NET MAUI Solution
For a production application, avoid putting every test into one project.
A better approach is to organize test projects according to their responsibilities.
For example:
MyMauiApplication/
│
├── MyMauiApplication/
│ ├── Views/
│ ├── ViewModels/
│ ├── Services/
│ └── Platforms/
│
├── MyApplication.Core/
│ ├── Models/
│ ├── Services/
│ └── Validation/
│
├── MyApplication.UnitTests/
│
├── MyApplication.AndroidTests/
│
├── MyApplication.iOSTests/
│
└── MyApplication.UITests/
The structure separates business logic testing from platform testing and UI automation.
The Core project contains reusable application logic. The unit test project validates that logic without requiring a device.
The Android and iOS test projects validate platform-specific behavior, while the UI test project handles user interaction scenarios.
This separation makes the solution easier to maintain as the application grows.
Running Device Tests in a CI/CD Pipeline
Device testing is also useful in automated build and deployment workflows.
However, device tests require more infrastructure than ordinary unit tests.
For example, an Android test job needs an Android SDK and a compatible emulator or connected device. An iOS test job requires macOS, Xcode, and a simulator or physical device.
A simplified Android pipeline can execute:
dotnet restore
dotnet build MyMauiApplication.sln \
-c Release
dotnet test \
--project MyApplication.AndroidTests/MyApplication.AndroidTests.csproj
The exact pipeline configuration depends on the available build agent and the test project.
For reliable automation, consider the following practices:
Use a dedicated emulator image for repeatable Android tests.
Keep SDK and workload versions consistent across development and CI environments.
Collect test output and application logs when a test fails.
Separate platform test jobs so one platform failure does not hide results from another.
Avoid depending on external services unless the test environment controls their availability.
Use test accounts and isolated test data.
Run a smaller device test suite on pull requests and a broader suite before release.
A successful build does not guarantee that a device test can run. The target emulator, simulator, or physical device must also be available and configured correctly.
Common Mistakes When Using Device Test Templates
Treating Device Tests Like Ordinary Unit Tests
A device test project is a platform application. It needs platform tooling and an appropriate execution environment.
Do not expect it to run on a machine that has only the standard .NET SDK installed.
Forgetting to Start an Emulator
Android device tests need a connected Android device or running emulator.
If no compatible target is available, the test deployment or execution process can fail.
Running iOS Tests Without a Mac
iOS development depends on Apple's build tools.
A Windows-only environment cannot independently build and run iOS test applications. Configure a Mac build host when working from Windows.
Using Device Tests for Every Business Rule
Not every test needs a device.
Calculations, validation, and business rules should generally remain in ordinary unit test projects.
Using an emulator for every small test increases execution time and makes the testing process more complicated than necessary.
Assuming Device Tests Are UI Automation
A test running in an Android application process does not automatically interact with buttons, text fields, or navigation screens.
Use a dedicated UI automation framework when the requirement is to simulate user actions.
Ignoring Permissions and Platform Configuration
Native APIs may require runtime permissions, entitlements, provisioning, or other platform-specific settings.
A failing test may indicate an environment configuration problem rather than an application bug.
Best Practices for Production Applications
Keep Business Logic Outside Platform Projects
Use separate services and class libraries for business logic wherever possible.
This allows most application behavior to be tested quickly without launching an emulator or simulator.
Test Only Platform-Dependent Behavior on Devices
Reserve device tests for functionality that genuinely requires the platform runtime.
This keeps device test suites focused and reduces unnecessary execution time.
Use Realistic Device Conditions
An emulator is useful for repeatable tests, but physical devices can reveal hardware and operating system differences.
Test important functionality on physical devices before release, especially when it depends on hardware, permissions, connectivity, or performance.
Keep Test Data Independent
Device tests should not depend on a developer's personal account or production data.
Use dedicated test accounts, controlled test data, and predictable test environments.
Capture Diagnostic Information
When a device test fails, collect application logs, test output, platform information, and relevant exception details.
This makes failures easier to reproduce and investigate.
Avoid Testing Unstable External Dependencies
A device test should not fail simply because an unrelated external API is temporarily unavailable.
Mock external services where appropriate, or use a controlled test environment for integration scenarios.
Advantages and Disadvantages of the New Device Test Templates
Advantages | Disadvantages |
|---|
Simplifies creation of platform-specific test projects | Requires platform workloads and development tools |
Executes tests inside a platform application process | Slower than ordinary unit tests |
Provides access to platform-specific APIs | Requires emulators, simulators, or devices for supported targets |
Integrates with dotnet test through MTP | CI/CD setup requires additional infrastructure |
Uses MSTest for familiar test attributes and assertions | Does not replace UI automation frameworks |
Helps validate native behavior earlier | Platform configuration can make tests harder to maintain |
Summary
The new .NET MAUI device test templates provide a practical way to test functionality that requires a real platform application environment.
With templates such as androidtest, iostest, tvostest, macostest, and maccatalysttest, developers can create platform-specific MSTest projects that execute through Microsoft.Testing.Platform.
These templates help bridge the gap between traditional unit testing and platform-dependent application testing.
However, they should be used as part of a broader testing strategy. Keep business logic in ordinary unit test projects, use device tests for native platform behavior, and use UI automation for validating complete user workflows.
By separating these responsibilities, development teams can build more reliable .NET MAUI applications while keeping their test suites maintainable and practical for continuous integration.
Join the conversation! Your thoughts help the community grow.