Assertion Scopes
In TUnit you can create an assertion scope by calling Assert.Multiple(). This returns an IDisposable and so you should use that by encapsulating the returned value in a using block. This will make sure that any assertion exceptions are aggregated together and thrown only after the scope is exited.
This is useful for asserting multiple properties and showing all errors at once, instead of having to fix > rerun > fix > rerun.
Behavior Differencesâ
Outside Assert.Multiple(): Assertions throw immediately when they fail, stopping test execution.
Inside Assert.Multiple(): Assertions accumulate failures and only throw when the scope exits, allowing all assertions within the scope to execute.
Implicit Scope:
[Test]
public async Task MyTest()
{
var result = Add(1, 2);
using var _ = Assert.Multiple();
await Assert.That(result).IsPositive();
await Assert.That(result).IsEqualTo(3);
}
Explicit Scope:
[Test]
public async Task MyTest()
{
var result = Add(1, 2);
using (Assert.Multiple())
{
await Assert.That(result).IsPositive();
await Assert.That(result).IsEqualTo(3);
}
}