Skip to content

Analyzer to detect not awaited async calls in a test case #9816

Description

@stan-sz

Summary

Signal an async call is not awaited

[TestMethod]
public void AsyncThrow()
{
    Assert.ThrowsAsync<ArgumentException>(async () =>
    {
        throw new ArgumentException("exception", "value");
    });
}

Expected analyzer behavior: suggest

[TestMethod]
public async Task AsyncThrow()
{
    await Assert.ThrowsAsync<ArgumentException>(async () =>
    {
        throw new ArgumentException("exception", "value");
    });
}

Currently https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0040 covers only async void case.

Activity

  1. added theissue type on Jul 10, 2026
  2. Youssef1313 commented on Jul 10, 2026

    @Youssef1313
    Member

    I think a warning should only be produced if a method result is ignored when the method returns an awaitable type. It can be fine to not await the result as long as the user is doing something with it and not ignoring it completely.

    I don't think MSTest should do something here specific to testing as it's a general concern and not really specific to testing or MSTest.

    In addition, this should already produce a compiler warning CS4014. Did you disable CS4014 in your codebase?

  3. Evangelink commented on Jul 13, 2026

    @Evangelink
    Member

    Just to add a data point: there's already a first-party Microsoft analyzer that covers this general case — VSTHRD110 "Observe result of async calls" from Microsoft.VisualStudio.Threading.Analyzers. It fires when an awaitable-returning call's result is ignored completely, which matches the behavior described here (ignoring the result → warn; doing something with it → fine).

    On the CS4014 point: CS4014 only fires inside async methods. The repro here is public void AsyncThrow() (non-async), so CS4014 stays silent — that's the actual gap, and it's exactly what VSTHRD110 catches.

    That said, I don't think pointing at VSTHRD110 fully closes this: it's not installed in a lot of repos, and MSTest users don't get it out-of-the-box. So there may still be value in an MSTest rule (or extending MSTEST0040) so this is covered by default for test projects, even if the underlying concern is general. Worth deciding whether we want to own that or rely on the external analyzer.

  4. added
    area/analyzersMSTest.Analyzers Roslyn analyzers and code fixes.
    and removed
    needs/triageNeeds triage by a maintainer.
    on Aug 27, 2026
  5. drieseng commented on Sep 1, 2026

    @drieseng
  6. stan-sz commented on Oct 30, 2026

    @stan-sz
    ContributorAuthor

    How about have a conditional (e.g. MSTestEnableThreadingAnalyzers property) package reference of Microsoft.VisualStudio.Threading.Analyzers though MSTest meta package to rely on existing code?

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

    area/analyzersMSTest.Analyzers Roslyn analyzers and code fixes.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions