The Assert.Scope() method enables soft assertions. Instead of throwing an exception immediately upon a failure, assertion failures are collected and reported together when the scope is disposed. This allows you to verify multiple properties or conditions in a single test block without the test stopping at the first failure.
Important Behavior:
When using Assert.Scope(), assertions do not enforce postconditions. This means that if an assertion like Assert.IsNotNull(obj) fails, the execution continues, and obj may still be null in the subsequent lines of code. While the compiler may treat obj as non-null due to annotations, you should not rely on this within a scope to avoid NullReferenceException at runtime.
using (Assert.Scope())
{
Assert.AreEqual(1, actual.X); // failure collected, execution continues
Assert.AreEqual(2, actual.Y); // failure collected, execution continues
Assert.IsTrue(actual.IsValid); // failure collected, execution continues
}
// Dispose() throws AggregateException-like AssertFailedException with all 3 failures