Skip to content

Skip Console.CancelKeyPress where the platform does not support it - #1550

Open
zhollis21 wants to merge 4 commits into
nunit:mainfrom
zhollis21:issue-1549-cancelkeypress-android
Open

zhollis21 wants to merge 4 commits into
nunit:mainfrom
zhollis21:issue-1549-cancelkeypress-android

Conversation

@zhollis21

@zhollis21 zhollis21 commented Nov 13, 2026 •

Copy link
Copy Markdown

Fixes #1549

Under Microsoft.Testing.Platform, the NUnit3TestExecutor constructor subscribes to Console.CancelKeyPress unconditionally. The runtime marks that event unsupported on Android, iOS, tvOS and browser, so on Android the constructor throws PlatformNotSupportedException before any test runs.

The change:

  • The subscription is wrapped in a try/catch (PlatformNotSupportedException). I chose this over OperatingSystem.IsAndroid() and friends because the adapter also targets net462, where OperatingSystem doesn't exist. It also covers any other platform that throws.
  • Dispose only unsubscribes when the subscription succeeded, so it doesn't throw (and log) the same exception again.
  • New CancelKeyPressTests: a subscription that throws PlatformNotSupportedException is skipped; a normal one subscribes; other exceptions still propagate; an MTP executor can be created and disposed.

.\build -t test passes on net462 and net8.0.

Checked on an Android emulator

I used a dotnet new androidtest project (net11.0-android, x86_64 emulator, API 36), switched from MSTest to NUnit 5.0.0 + NUnit3TestAdapter with EnableNUnitRunner and builder.AddNUnit(...), and ran dotnet test --device <emulator>:

To be clear about what this does not show: no NUnit test runs on the device yet. After this fix, the run stops in the NUnit engine (the extension directory scan, then engine and test-assembly loading). I've written those up separately in nunit/nunit-console#1966, since they are a bigger question than this bug. This PR removes the first blocker and is safe on every platform on its own.

…unit#1549)

Under Microsoft.Testing.Platform the executor subscribed to
Console.CancelKeyPress unconditionally. On Android, iOS, tvOS and browser
that throws PlatformNotSupportedException from the constructor, so no test
ran. Subscribe in a try/catch for that exception, and unsubscribe in
Dispose only when the subscription succeeded.
@zhollis21

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the console-subscription change.

No open issues 🎉

What changed in this PR

Addresses #1549 by preventing unsupported Console.CancelKeyPress subscriptions from blocking Microsoft.Testing.Platform executor construction. This removes the first Android startup blocker; the engine limitations tracked in #1551 remain outside this PR.

Changes:

  • Catch PlatformNotSupportedException during subscription while allowing other exceptions to propagate.
  • Unsubscribe during disposal only when registration succeeded.
  • Add tests for subscription outcomes, exception propagation, and MTP executor creation/disposal.
File Description
src/​NUnitTestAdapterTests/​CancelKeyPressTests.cs Adds subscription behavior tests and an executor lifecycle smoke test.
src/​NUnitTestAdapter/​NUnit3TestExecutor.cs Guards unsupported console subscriptions and tracks successful registration for cleanup.

Review effort: Balanced

@OsirisTerje

Copy link
Copy Markdown
Member

@zhollis21 Can you check this again, as main now have engine 3.23.0, which includes the engine fix.

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.

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

3 participants