Skip to content

DeviceTests

Rui Marinho edited this page Jan 4, 2024 · 7 revisions

The tests on these projects are written using XUnit. They are deployed to a device and can be run either visually our with a headless testrunner. The Device tests projects are built for all platforms, so when you write a test it will appear for all different frameworks net7.0-android net7.0-ios net7.0-windows net7.0-maccatalyst . You can include or exclude a test from a particular platform by wrapping your test method on a compile directive e.g. #if !WINDOWS. On device tests what we want to test normally is if we changed something on the cross platform layer the native representation has the value we expect.

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

  • Build the project and open VS
dotnet cake --target=vs --workloads=global
  • Deploy project to iOS or Android device or simulator.

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/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