Skip to content

Complain about calling 'async void' methods. #962

@manfred-brands

Description

@manfred-brands

When calling an async void method or delegate, the method runs in the background and the current method continues without having a chance to observe its result.

[Test]
public static void NonAsyncTestCallingAsyncVoidMethod()
{
    AsyncVoidMethod();
}

private static async void AsyncVoidMethod()
{
    await Task.Delay(10);
    throw new InvalidOperationException("Exception from  inside async void method.");
}

NUnit will think that the NonAsyncTestCallingAsyncVoidMethod has finished before the exception is thrown in the async void method.
Once the exception is thrown in an unexpected place, it could crash NUnit (pre NUnit 4.5) or catch it and mark the test as Error.
But if the time between the exception and the test 'finishing' is too small, nunit could have marked the test as Success prematurely.

See: nunit/nunit#3930

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions