Skip to content

DeviceTests

Shane Neuville edited this page Jun 27, 2024 · 7 revisions

These are tests that will run on an actual device

├── Controls 
│   ├── test
│   │   ├── Controls.DeviceTests
├── Core 
│   ├── test
│   │   ├── Core.DeviceTests
├── Essentials 
│   ├── test
│   │   ├── Essentials.DeviceTests
├── BlazorWebView 
│   ├── test
│   │   ├── MauiBlazorWebView.DeviceTests

Unfortunately, there is no IDE integration with the Device Tests. These tests run on the actual device via a test runner that's packaged with the app. The only way to run these tests is from the device itself. Each of the Device Test projects are full .NET MAUI apps that you can deploy to the platform you are testing.

The tests on these projects are written using XUnit. They are deployed to a device and can be run visually or with a headless test runner.

Writing a Device Test

[Theory("Text transform updates correctly)]
[ClassData(typeof(TextTransformCases))]
public async Task TextTransformUpdated(string text, TextTransform transform, string expected)
{
	//Create my cross platform Button
	var control = new Button() { Text = text };

	//Create it's handler so i can grab the native view and Assert
	var handler = await CreateHandlerAsync<ButtonHandler>(control);

	//Change a value on my cross platform layer
	await InvokeOnMainThreadAsync(() => control.TextTransform = transform);

	//Helper method to grab the text from the native button (UIButton, MaterialButton)
	var platformText = await GetPlatformText(handler);

	//Assert that the value is what we expect
	Assert.Equal(expected, platformText);
}

Prerequisites

To run the tests on iOS or Catalyst, you'll need a Mac. To run the tests on Windows, you'll need a Windows machine. Android tests can be run from either platform.

Windows

Make sure a dotnet tool restore on the repo home Make sure DeveloperMode is turned on Make sure you have maui workloads installed

Mac

Make sure a dotnet tool restore on the repo home Make sure you have maui workloads installed

Android prerequisites

Ensure that you have the Android API 30 SDK installed, with the emulator image for Play store on x86/x64. By default, the tests use that. See screenshot below. If you want to use another image you can specify the --device argument, with something like --device="android-emulator-64_33" where the 64 means image x86_x64 and 33 indicates the API level.

image

Running device tests from the IDE by launching the {Project}.DeviceTests app

  • Follow the instructions on the Development.md doc to get the .NET MAUI sln up and running.
  • Deploy The project Device Test project to the platform you are testing.
  • At this point you will be able to navigate to the test you want to run or run your full set of tests.

Running device tests from the command line (you can replace ./build.ps1 by dotnet cake)

./build.ps1 --target=dotnet-buildtasks --configuration="Release" --workloads=global

iOS

dotnet cake -script eng/devices/ios.cake --project="/Users/ruimarinho/dotnet/maui/src/Graphics/tests/DeviceTests/Graphics.DeviceTests.csproj" --device=ios-simulator-64  --workloads=global

dotnet cake -script eng/devices/ios.cake --project="/Users/ruimarinho/dotnet/maui/src/Core/tests/DeviceTests/Core.DeviceTests.csproj" --device=ios-simulator-64 --workloads=global

dotnet cake -script eng/devices/ios.cake --project="/Users/ruimarinho/dotnet/maui/src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj" --device=ios-simulator-64 --workloads=global

dotnet cake -script eng/devices/ios.cake --project="/Users/ruimarinho/dotnet/maui/src/BlazorWebView/tests/MauiDeviceTests/MauiBlazorWebView.DeviceTests.csproj" --device=ios-simulator-64 --workloads=global

Windows

dotnet cake -script eng/devices/windows.cake --project="C:\repos\dotnet\maui\src\Core\tests\DeviceTests\Core.DeviceTests.csproj" --device="unpackaged" --packageid=com.microsoft.maui.core.devicetests --workloads=global

dotnet cake -script eng/devices/windows.cake --project="C:\repos\dotnet\maui\src\Controls\tests\DeviceTests\Controls.DeviceTests.csproj" --device="unpackaged" --packageid=com.microsoft.maui.controls.devicetests --workloads=global

dotnet cake -script eng/devices/windows.cake --project="C:/repos/dotnet/maui/src/BlazorWebView/tests/MauiDeviceTests/MauiBlazorWebView.DeviceTests.csproj" --device="unpackaged" --packageid=Microsoft.Maui.MauiBlazorWebView.DeviceTests --workloads=global

Clone this wiki locally