Assert.ThrowsAsync is called without await, so the test ends before the assertion is checked

tx0/csharp:unawaited-throws-assertiondefaulterrormaintainability/testsdepth fastbug

What it finds

Assert.ThrowsAsync and its relatives return a task that fails when the expected exception is not thrown. Without await, the test returns at once and passes whatever the code does.

Reported are the assertions of xUnit and MSTest (Assert.ThrowsAsync, Assert.ThrowsAnyAsync, Assert.ThrowsExceptionAsync, Record.ExceptionAsync), of Shouldly (Should.ThrowAsync, ShouldThrowAsync, ShouldNotThrowAsync) and of FluentAssertions (.Should().ThrowAsync() and its relatives), and Assert.ThatAsync. In NUnit, Assert.ThrowsAsync and Assert.CatchAsync wait for the task themselves and return the exception, so they are left alone in files that use NUnit and not xUnit or MSTest.

From the rule's source, unawaited_throws_assertion.rs.

Example from the rule's tests
using System;using System.Threading.Tasks;using Xunit; public class Client{    [Fact]    public async Task Rejects()    {        Assert.ThrowsAsync<ArgumentException>(() => Send(null));tx0 reports: ThrowsAsync is not awaited, so it never fails the test        await Assert.ThrowsAsync<ArgumentException>(() => Send(null));    }     Task Send(string s) => Task.CompletedTask;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:unawaited-throws-assertion" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:unawaited-throws-assertion] reason="..."

tx0 explain tx0/csharp:unawaited-throws-assertion shows the same in the terminal. Found a false positive? Report it.