Skip to content

Add documentation for EnterMultipleScope #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions docs/articles/nunit/writing-tests/assertions/multiple-asserts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,51 @@ object initialization and UI appearance as well as certain kinds of integration

## Syntax / Example

Multiple asserts are implemented using the `Assert.Multiple` method.
Multiple asserts are implemented using the `Assert.EnterMultipleScope` method.

Let's assume we have production code that looks like the following:

[!code-csharp[MultipleAssertsProdCode](~/snippets/Snippets.NUnit/MultipleAsserts.cs#MultipleAssertsProdCode)]

In that case, we could write a test with multiple assertions, such as:

[!code-csharp[MultipleAssertsTests](~/snippets/Snippets.NUnit/MultipleAsserts.cs#MultipleAssertsTests)]
[!code-csharp[MultipleAssertsScopes](~/snippets/Snippets.NUnit/MultipleAsserts.cs#MultipleAssertsScopes)]

Functionally, this results in NUnit storing any failures encountered in the block and reporting all of them together
upon exit from the block. If both asserts failed, then both would be reported. The test itself would terminate at the
end of the block if any failures were encountered, but would continue otherwise.

### Older NUnit versions

Earlier than NUnit 4.2 you can must use `Assert.Multiple`:

[!code-csharp[MultipleAssertsTests](~/snippets/Snippets.NUnit/MultipleAsserts.cs#MultipleAssertsTests)]

## Notes

1. The multiple assert block may contain any arbitrary code, not just asserts.
1. For more information on `EnterMultipleScope` see this [Feature Request](https://github.com/nunit/nunit/issues/4587).

2. The multiple assert block may contain any arbitrary code, not just asserts.

2. Multiple assert blocks may be nested. Failure is not reported until the outermost block exits.
3. Multiple assert blocks may be nested. Failure is not reported until the outermost block exits.

3. If the code in the block calls a method, that method may also contain multiple assert blocks.
4. If the code in the block calls a method, that method may also contain multiple assert blocks.

4. The test will be terminated immediately if any exception is thrown that is not handled. An unexpected exception is
5. The test will be terminated immediately if any exception is thrown that is not handled. An unexpected exception is
often an indication that the test itself is in error, so it must be terminated. If the exception occurs after one or
more assertion failures have been recorded, those failures will be reported along with the terminating exception
itself.

5. Assert.Fail is handled just as any other assert failure. The message and stack trace are recorded but the test
6. Assert.Fail is handled just as any other assert failure. The message and stack trace are recorded but the test
continues to execute until the end of the block.

6. An error is reported if any of the following are used inside a multiple assert block:
7. An error is reported if any of the following are used inside a multiple assert block:
* Assert.Pass
* Assert.Ignore
* Assert.Inconclusive
* Assume.That

7. Use of Warnings (Assert.Warn, Warn.If, Warn.Unless) is permitted inside a multiple assert block. Warnings are
8. Use of Warnings (Assert.Warn, Warn.If, Warn.Unless) is permitted inside a multiple assert block. Warnings are
reported normally along with any failures that occur inside the block.

### Runner Support
Expand Down
22 changes: 22 additions & 0 deletions docs/snippets/Snippets.NUnit/MultipleAsserts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,26 @@ public void MultipleAssertsDemo()
});
}
#endregion

#region MultipleAssertsScopes
[Test]
public void MultipleAssertsScopeDemo()
{
var situationUnderTest = new SomeCalculator();
var result = situationUnderTest.DoCalculation();

using (Assert.EnterMultipleScope())
{
Assert.That(result.RealPart, Is.EqualTo(5.2));
Assert.That(result.ImaginaryPart, Is.EqualTo(3.9));
}

// Can also work with the classic assertion syntax
using (Assert.EnterMultipleScope())
{
ClassicAssert.AreEqual(5.2, result.RealPart, "Real Part");
ClassicAssert.AreEqual(3.9, result.ImaginaryPart, "Imaginary Part");
}
}
#endregion
}
Loading