Search code examples
c#nunitassertionsnunit-3.0fluent-assertions

Multiple assertions using Fluent Assertions library


It seems that Fluent Assertions doesn't work within NUnit's Assert.Multiple block:

Assert.Multiple(() =>
    {
        1.Should().Be(2);
        3.Should().Be(4);
    });

When this code is run, the test fails immediately after the first assertion, so the second assertion is not even executed.

But, if I use NUnit's native assertions, I get the results I want:

Assert.Multiple(() =>
    {
        Assert.That(1, Is.EqualTo(2));
        Assert.That(3, Is.EqualTo(4));
    });

And the output contains details on both failures:

Test Failed - ExampleTest()

Message: Expected: 2 But was: 1

Test Failed - ExampleTest()

Message: Expected: 4 But was: 3

How can I get similar results using Fluent Assertions with NUnit?


Solution

  • You can do this with assertion scopes like this:

    using (new AssertionScope())
    {
        5.Should().Be(10);
        "Actual".Should().Be("Expected");
    }