Skip to content

ThrowsAsync reports OperationCanceledException as TaskCanceledException #2168

Description

@rouacke

The ThrowsAsync method reports exceptions of type OperationCanceledException as TaskCanceledException instead.

Repro:

Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
    await Task.Yield();
    throw new OperationCanceledException();
});

Result:

Failed : NUnitRepro.Tests.Repro
  Expected: <System.OperationCanceledException>
  But was:  <System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult)
   at NUnit.Framework.Assert.ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, String message, Object[] args)>

Other exception types are reported correctly. The following test passes.

Assert.ThrowsAsync<TimeoutException>(async () =>
{
    await Task.Yield();
    throw new TimeoutException();
});

I believe it's possible to workaround this issue using the "constraint model". The following test passes.

Assert.That(async () =>
{
    await Task.Yield();
    throw new OperationCanceledException();
}, Throws.InstanceOf<OperationCanceledException>());

I observed the issue in version 3.5.0. I updated to 3.6.1 and confirmed that the issue is still present in the latest version.

I also installed the console runner and tested from the command line as per the contributing guidelines. Test output below.

C:\...\NUnitRepro\NUnitRepro\bin\Debug>..\..\..\packages\NUnit.ConsoleRunner.3.6.1\tools\nunit3-console.exe NUnitRepro.exe
NUnit Console Runner 3.6.1
Copyright (C) 2017 Charlie Poole

Runtime Environment
   OS Version: Microsoft Windows NT 10.0.14393.0
  CLR Version: 4.0.30319.42000

Test Files
    NUnitRepro.exe


Errors, Failures and Warnings

1) Failed : NUnitRepro.Tests.Repro
  Expected: <System.OperationCanceledException>
  But was:  <System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult)
   at NUnit.Framework.Assert.ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, String message, Object[] args)>
at NUnitRepro.Tests.Repro() in c:\...\NUnitRepro\NUnitRepro\Tests.cs:line 16

Run Settings
    DisposeRunners: True
    WorkDirectory: C:\...\NUnitRepro\NUnitRepro\bin\Debug
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6
    ImageRequiresX86: True
    RunAsX86: True
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 4

Test Run Summary
  Overall result: Failed
  Test Count: 3, Passed: 2, Failed: 1, Warnings: 0, Inconclusive: 0, Skipped: 0
    Failed Tests - Failures: 1, Errors: 0, Invalid: 0
  Start time: 2017-05-15 15:43:50Z
    End time: 2017-05-15 15:43:50Z
    Duration: 0.564 seconds

Results (nunit3) saved as TestResult.xml

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions