Skip to content

[dotnet test] Add MTP test adapter for Android device testing#11130

Open
jonathanpeppers wants to merge 6 commits intomainfrom
jonathanpeppers/dev-peppers-use-instrumentation-dotnet-r
Open

[dotnet test] Add MTP test adapter for Android device testing#11130
jonathanpeppers wants to merge 6 commits intomainfrom
jonathanpeppers/dev-peppers-use-instrumentation-dotnet-r

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

Summary

Adds Microsoft Testing Platform (MTP) test adapter support to Microsoft.Android.Run, enabling dotnet test to work with Android device test projects created from the androidtest template.

Fixes #10683

Changes

  • AndroidTestAdapter.cs — MTP test framework adapter that:

    • Runs am instrument -w on the connected device/emulator
    • Pulls the TRX results file from the device
    • Parses test results and reports them through the MTP protocol back to dotnet test
  • Program.cs — Added --server and --dotnet-test-pipe options for dotnet test integration. When dotnet test invokes Microsoft.Android.Run, these MTP protocol args are detected and the tool switches to test adapter mode.

  • Microsoft.Android.Run.csproj — Added Microsoft.Testing.Platform package reference (version managed in Directory.Build.props)

  • DotNetCLI.cs — Added StartTest() helper method for running dotnet test in integration tests

  • InstallAndRunTests.cs — Parameterized DotNetNewAndroidTest() with [TestCase("run")] and [TestCase("test")] to verify both execution modes work with the androidtest template

How dotnet test works with Android projects

  1. dotnet test builds the project and evaluates RunCommand/RunArguments (which point to Microsoft.Android.Run)
  2. It appends --server dotnettestcli --dotnet-test-pipe <pipe> to launch the test host
  3. Microsoft.Android.Run detects these args and creates an MTP TestApplication with AndroidTestAdapter
  4. The adapter runs instrumentation on device, pulls TRX, and reports results through MTP
  5. dotnet test displays the test results to the user

Add AndroidTestAdapter that enables 'dotnet test' to work with Android
test projects via the Microsoft Testing Platform (MTP) protocol:

- AndroidTestAdapter runs 'am instrument -w' on device, pulls TRX
  results, and reports individual test results through MTP
- Program.cs gains --server/--dotnet-test-pipe options for dotnet test
  integration, passing remaining args through to MTP
- Microsoft.Testing.Platform package reference added to
  Microsoft.Android.Run
- DotNetCLI.StartTest() helper for running 'dotnet test' in tests
- DotNetNewAndroidTest() parameterized to verify both 'dotnet run' and
  'dotnet test' modes work with the androidtest template

Fixes #10683

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 16, 2026 17:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Microsoft Testing Platform (MTP) adapter support to Microsoft.Android.Run so dotnet test can execute Android device tests produced by the androidtest template, collecting TRX results from device instrumentation and reporting them back through MTP.

Changes:

  • Introduces an AndroidTestAdapter that runs am instrument -w, pulls TRX from device, parses it, and publishes results via MTP.
  • Extends Microsoft.Android.Run CLI to detect dotnet test invocation (--server / --dotnet-test-pipe) and switch into MTP host mode.
  • Updates device integration tests/utilities to exercise both dotnet run and dotnet test execution paths for the androidtest template.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs Parameterizes the template test to run via dotnet run and dotnet test and adjusts assertions/logging accordingly.
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs Adds StartTest() helper for launching dotnet test as a long-running process in integration tests.
src/Microsoft.Android.Run/Program.cs Adds --server / --dotnet-test-pipe handling and a RunDotnetTestAsync path to host MTP.
src/Microsoft.Android.Run/Microsoft.Android.Run.csproj References Microsoft.Testing.Platform for the new adapter/host flow.
src/Microsoft.Android.Run/AndroidTestAdapter.cs New MTP test framework adapter that runs instrumentation, pulls/parses TRX, and publishes per-test results.
Directory.Build.props Introduces MicrosoftTestingPlatformVersion property for package version management.

Comment thread src/Microsoft.Android.Run/AndroidTestAdapter.cs
Comment thread tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs Outdated
Comment thread src/Microsoft.Android.Run/Program.cs Outdated
Comment thread src/Microsoft.Android.Run/AndroidTestAdapter.cs Outdated
Copy link
Copy Markdown
Member Author

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review Summary

Verdict: ⚠️ Needs Changes

Found 5 issues (2 ❌, 2 ⚠️, 1 💡):

  • Nullable: dotnetTestPipe! and package! use the banned null-forgiving operator (Program.cs:294,301)
  • ⚠️ Error handling: RunInstrumentationOnDeviceAsync ignores adb exit code — device failures produce silent zero-test results (AndroidTestAdapter.cs:96)
  • ⚠️ Error handling: ParseTrxFile catch block swallows exceptions and returns empty results (AndroidTestAdapter.cs:247)
  • ⚠️ Error handling: error parameter in ParseInstrumentationBundle is accepted but never used (AndroidTestAdapter.cs:144)
  • 💡 Nullable: Use .IsNullOrEmpty() extension method for NRT flow analysis (AndroidTestAdapter.cs:219,231,232)

👍 Clean MTP integration architecture with good separation between the adapter and CLI. Proper CancellationToken propagation throughout, well-structured TRX parsing, and nice parameterized test coverage for both run and test modes.

⏳ CI is still pending — cannot confirm all checks pass.


Review generated by android-reviewer from review guidelines.

Comment thread src/Microsoft.Android.Run/Program.cs Outdated
Comment thread src/Microsoft.Android.Run/AndroidTestAdapter.cs
Comment thread src/Microsoft.Android.Run/AndroidTestAdapter.cs Outdated
Comment thread src/Microsoft.Android.Run/AndroidTestAdapter.cs
Comment thread src/Microsoft.Android.Run/AndroidTestAdapter.cs Outdated
- Replace Debug.Assert + null-forgiving operators with explicit
  validation and user-facing error messages in RunDotnetTestAsync
- Check adb exit code in RunInstrumentationOnDeviceAsync and surface
  stderr when device failures occur
- Propagate instrumentation errors by throwing instead of silently
  returning (prevents dotnet test showing 0 tests on failure)
- Remove try/catch that swallowed TRX parsing exceptions
- Use adb stderr in ParseInstrumentationBundle when no results parsed
- Fix dotnet test assertions to check summary counts (Passed/Failed/
  Skipped) instead of method names that may not appear in output

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers marked this pull request as draft April 16, 2026 19:53
jonathanpeppers and others added 2 commits April 16, 2026 14:56
The .NET SDK muxer searches for global.json starting from the current
working directory. Without setting WorkingDirectory, dotnet test
inherits the CI runner's cwd and finds the repo root's global.json
(which has no 'test' section), falling back to VSTest mode. MTP v2 on
.NET 10 SDK rejects VSTest mode with an error.

Setting WorkingDirectory to the project directory ensures dotnet test
finds the template's global.json (with 'test.runner' set to
'Microsoft.Testing.Platform') first.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The MTP adapter in Microsoft.Android.Run requires
Microsoft.Testing.Platform.dll at runtime. Add it to the SDK pack's
tools/ directory so dotnet test can load it. No deps.json needed —
the existing tool already resolves dependencies from the flat tools/
directory layout (same as Mono.Options.dll and
Xamarin.Android.Tools.AndroidSdk.dll).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers force-pushed the jonathanpeppers/dev-peppers-use-instrumentation-dotnet-r branch from 1e32406 to faf7ad4 Compare April 17, 2026 17:21
dotnet test with MTP does not go through the MSBuild Run target, so
the Install target never runs and the APK is not deployed to the
device. Add an explicit 'dotnet build -t:Install' step before running
dotnet test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
MTP uses lowercase 'succeeded/failed/skipped' in its summary, not
the capitalized 'Passed/Failed/Skipped' format.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers marked this pull request as ready for review April 20, 2026 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add UseInstrumentation support to dotnet run for device testing

2 participants