[dotnet test] Add MTP test adapter for Android device testing#11130
[dotnet test] Add MTP test adapter for Android device testing#11130jonathanpeppers wants to merge 6 commits intomainfrom
Conversation
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>
There was a problem hiding this comment.
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
AndroidTestAdapterthat runsam instrument -w, pulls TRX from device, parses it, and publishes results via MTP. - Extends
Microsoft.Android.RunCLI to detectdotnet testinvocation (--server/--dotnet-test-pipe) and switch into MTP host mode. - Updates device integration tests/utilities to exercise both
dotnet runanddotnet testexecution paths for theandroidtesttemplate.
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. |
jonathanpeppers
left a comment
There was a problem hiding this comment.
🤖 AI Review Summary
Verdict:
Found 5 issues (2 ❌, 2
- ❌ Nullable:
dotnetTestPipe!andpackage!use the banned null-forgiving operator (Program.cs:294,301) ⚠️ Error handling:RunInstrumentationOnDeviceAsyncignores adb exit code — device failures produce silent zero-test results (AndroidTestAdapter.cs:96)⚠️ Error handling:ParseTrxFilecatch block swallows exceptions and returns empty results (AndroidTestAdapter.cs:247)⚠️ Error handling:errorparameter inParseInstrumentationBundleis 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.
- 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>
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>
1e32406 to
faf7ad4
Compare
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>
Summary
Adds Microsoft Testing Platform (MTP) test adapter support to
Microsoft.Android.Run, enablingdotnet testto work with Android device test projects created from theandroidtesttemplate.Fixes #10683
Changes
AndroidTestAdapter.cs— MTP test framework adapter that:am instrument -won the connected device/emulatordotnet testProgram.cs— Added--serverand--dotnet-test-pipeoptions fordotnet testintegration. Whendotnet testinvokesMicrosoft.Android.Run, these MTP protocol args are detected and the tool switches to test adapter mode.Microsoft.Android.Run.csproj— AddedMicrosoft.Testing.Platformpackage reference (version managed inDirectory.Build.props)DotNetCLI.cs— AddedStartTest()helper method for runningdotnet testin integration testsInstallAndRunTests.cs— ParameterizedDotNetNewAndroidTest()with[TestCase("run")]and[TestCase("test")]to verify both execution modes work with theandroidtesttemplateHow
dotnet testworks with Android projectsdotnet testbuilds the project and evaluatesRunCommand/RunArguments(which point toMicrosoft.Android.Run)--server dotnettestcli --dotnet-test-pipe <pipe>to launch the test hostMicrosoft.Android.Rundetects these args and creates an MTPTestApplicationwithAndroidTestAdapterdotnet testdisplays the test results to the user