NUnit, is it possible to continue executing test after Assert fails?

前端 未结 9 2200
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 19:42

In a test that contains some asserts, for example:

Assert.AreEqual(1,1);
Assert.AreEqual(2,1);
Assert.AreEqual(2,2);

is it possible to let

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 20:35

    No you can't do it with NUnit alone. You have to do something like @Konstantin Spirin said. I created a small extension that you can use; it's called NUnit-GroupAssert. It can be found here: https://github.com/slvnperron/NUnit-GroupAssert

    How to use it:

    [Test]
    public void Verify_GroupsExceptions()
    {
        var group = new AssertGroup();
        group.Add(() => Assert.AreEqual(10, 20));
        group.Add(() => Assert.AreEqual(1, 1));
        group.Add(() => Assert.AreEqual(3, 4));
        group.Add(() => Assert.IsTrue(1 > 3));
        group.Verify();
    }
    
    // OR
    
    public void Verify_GroupsExceptions()
    {
        // Verifies on disposal
        using (var group = new AssertGroup())
        {
            group.Add(() => Assert.AreEqual(10, 20));
            group.Add(() => Assert.AreEqual(1, 1));
            group.Add(() => Assert.AreEqual(3, 4));
            group.Add(() => Assert.IsTrue(1 > 3));
        }
    }
    

    it will output:

    Test failed because one or more assertions failed:
    1) Expected: 10
    But was: 20
    From Verify_GroupsExceptions at line 18

    2) Expected: 3
    But was: 4
    From Verify_GroupsExceptions at line 20

    3) Expected: True
    But was: False
    From Verify_GroupsExceptions at line 21

提交回复
热议问题