Skip to content

Test execution never ends when Ctrl+C key hit in MTP mode #5106

@radim-bernatik-veeam

Description

@radim-bernatik-veeam

When using NUnit tests with MTP mode the execution of tests is not terminated when CTRL+C key is pressed in the console. It is visible that CtrlCIssue.UnitTests.Test event is raised, but test engine is running forever from that point. If no CTRL+C is pressed, test session ends in 5 seconds as there is only one test with 5s sleep code.

I tried the same case with MSTest tests, to identify if the issue is in MTP code or NUnit (adapter). When MSTest used instead of NUnit, behavior is correct. After CTRL+C the test session is finished and properly reported. When NUnit is used, I have to kill the process externally (via Task Manager or PowerShell).

How to simulate:

  1. Compile the example below
  2. Execute CtrlCIssue.exe --no-ansi --output Detailed command (using no-ANSI console reporter for better logging in the example, MTP logger has no impact)
  3. Right after execution when NUnit3TestExecutor discovered 1 of 1 NUnit test cases using Current Discovery mode, Non-Explicit run message is displayed, hit the CTRL+C key.
  4. After few second warning exception is displayed The operation was canceled., but execution never stops. The message Canceling the test session... comes from MTP when Console.CancelKeyPress event is raised.

Screenshot from the execution

Image

CtrlCIssueMSTest.csproj

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>net8</TargetFramework>
		<IsPackable>false</IsPackable>
		<IsTestProject>true</IsTestProject>
		<EnableNUnitRunner>true</EnableNUnitRunner>
		<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
		<OutputType>Exe</OutputType>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="coverlet.collector" Version="6.0.0" />
		<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
		<PackageReference Include="Microsoft.Testing.Extensions.Telemetry" Version="2.0.2" />
		<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="2.0.2" />
		<PackageReference Include="Microsoft.Testing.Extensions.VSTestBridge" Version="2.0.2" />
		<PackageReference Include="Microsoft.Testing.Platform" Version="2.0.2" />
		<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="2.0.2" />
		<PackageReference Include="NUnit" Version="3.12.0" />
		<PackageReference Include="NUnit.Analyzers" Version="3.8.0" />
		<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
	</ItemGroup>
</Project>

UnitTests.cs

using NUnit.Framework;
using System.Threading;

namespace CtrlCIssue
{
    [TestFixture]
    public class UnitTests
    {
        [Test]
        public void Test()
        {
            Thread.Sleep(5000);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions