Skip to content

MTP mode throws PlatformNotSupportedException on Android: NUnit3TestExecutor subscribes to Console.CancelKeyPress #1549

Open

Description

@zhollis21

Description

When NUnit tests run on Microsoft.Testing.Platform inside an Android instrumentation (the dotnet new androidtest template, with builder.AddMSTest(...) swapped for builder.AddNUnit(...)), every run fails before any test executes:

System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.InteropServices.PosixSignalRegistration.Register(PosixSignal signal, Action`1 handler)
   at System.Runtime.InteropServices.PosixSignalRegistration.Create(PosixSignal signal, Action`1 handler)
   at System.Console.add_CancelKeyPress(ConsoleCancelEventHandler value)
   at NUnit.VisualStudio.TestAdapter.NUnit3TestExecutor..ctor(Boolean isMTP) in .../NUnit3TestExecutor.cs:line 128
   at NUnit.VisualStudio.TestAdapter.TestingPlatformAdapter.NUnitBridgedTestFramework.SynchronizedRunTestsAsync(...) in .../NUnitBridgedTestFramework.cs:line 91
   at Microsoft.Testing.Extensions.VSTestBridge.SynchronizedSingleSessionVSTestBridgedTestFramework...

The isMTP branch of the constructor (added for #1361) subscribes to Console.CancelKeyPress unconditionally. Console.CancelKeyPress is unsupported on Android (and, per the runtime's [UnsupportedOSPlatform] annotations, iOS, tvOS and browser), so the subscription throws.

NUnit itself works on the device: driving the same test assembly with NUnitTestAssemblyRunner from the instrumentation runs and reports every test. Only the MTP adapter path fails. The stock MSTest version of the template runs under dotnet test --device as expected.

Repro

  • .NET SDK 11.0.100, net11.0-android, Android emulator (x86_64)
  • NUnit 5.0.0, NUnit3TestAdapter 6.3.1, Microsoft.Testing.Extensions.TrxReport 2.4.1
  • dotnet new androidtest, then in the project: replace the MSTest package with NUnit + NUnit3TestAdapter, EnableMSTestRunner with EnableNUnitRunner, and in TestInstrumentation.cs builder.AddMSTest(() => [GetType().Assembly]) with builder.AddNUnit(() => [GetType().Assembly])
  • dotnet test --device <emulator>

Suggested fix

Skip the subscription where it is unsupported, for example:

if (isMTP)
{
    AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
    if (!OperatingSystem.IsAndroid() && !OperatingSystem.IsIOS() && !OperatingSystem.IsTvOS() && !OperatingSystem.IsBrowser())
        Console.CancelKeyPress += OnCancelKeyPress;
}

(or wrap it in try { ... } catch (PlatformNotSupportedException) { }, which also covers targets the adapter is not multi-targeted for).

Activity

  1. added
    MTP
    on Nov 13, 2026
  2. added this to the 6.4 milestone on Nov 13, 2026
  3. OsirisTerje commented on Nov 13, 2026

    @OsirisTerje
    Member

    @zhollis21 Do you want to raise a PR for this ? You have the environment to check it out too, that's an advantage 😄

  4. zhollis21 commented on Nov 13, 2026

    @zhollis21
    Author

    Thanks @OsirisTerje. The PR is up: #1550.

    I checked it on an Android emulator. With 6.3.1, dotnet test --device fails right away with this PlatformNotSupportedException. With the fix, the run gets past the constructor. No test reaches the device yet, though: the NUnit engine fails next (the extension directory scan, then loading the engine and the test assembly). Those are a bigger question than this bug, so I've written them up separately in nunit/nunit-console#1966.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    MTPis:bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions