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

前端 未结 9 2104
伪装坚强ぢ
伪装坚强ぢ 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:33

    Nope. You shouldn't really have more than one assert per test anyway, that reduces the seperation and makes it more difficult to find out which one failed.

    If you have a lot of code that needs executing before the Assert, seperate it out into a [SetUp] function, or make it a seperate procedure.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-29 20:35

    I rewrote Konstantin Spirin's code sample into VB.

    Imports NUnit.Framework
    Imports System.Reflection
    
    Public Class Group_LIB_NUnit_Assert_Multiple
    
        Public Shared Sub Multiple(ParamArray Assertions As Action())
            Dim ExceptionObj As Exception
            Dim Exceptions As New List(Of Exception)
            Dim Message As String
    
            For Each Assertion In Assertions
                Try
                    Assertion()
                Catch ex As Exception
                    Exceptions.Add(ex)
                End Try
            Next
    
            If Exceptions.Count > 0 Then
                Message = String.Format("{0}{1} assertions failed: {2}{3}", Environment.NewLine, Exceptions.Count, Environment.NewLine, String.Join(Environment.NewLine, Exceptions.Select(Function(e) e.Message).ToList()))
    
                ExceptionObj = New AssertionException(Message)
    
                StackTraceReplace(ExceptionObj, Exceptions.First.StackTrace)
    
                Throw ExceptionObj
            End If
        End Sub
    
        Public Shared Sub StackTraceReplace(ExceptionObj As Exception, StackTrace As String)
            Dim RemoteStackTraceString As FieldInfo
    
            RemoteStackTraceString = GetType(Exception).GetField("_remoteStackTraceString", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    
            RemoteStackTraceString.SetValue(ExceptionObj, StackTrace)
    
        End Sub
    End Class
    
    Imports Group.Library4
    Imports NUnit.Framework
    
    <TestFixture()> _
    Public Class TEST_Group_LIB_NUnit_Assert_Multiple
    
        <Test()> _
        <TestCaseSource("Factory")> _
        Public Sub Multiple(TestObj As TEST_DATA_Group_LIB_NUnit_Assert_Multiple)
    
            Group_LIB_NUnit_Assert_Multiple.Multiple(Sub() Assert.That(TestObj.Gender, [Is].EqualTo("F"c), "Gender"), Sub() Assert.That(TestObj.Name, [Is].EqualTo("George Washington"), "Name"))
        End Sub
    
        Public Function Factory() As List(Of TEST_DATA_Group_LIB_NUnit_Assert_Multiple)
            Dim L As New List(Of TEST_DATA_Group_LIB_NUnit_Assert_Multiple)
            Dim TestObj As TEST_DATA_Group_LIB_NUnit_Assert_Multiple
    
            TestObj = New TEST_DATA_Group_LIB_NUnit_Assert_Multiple()
            TestObj.DOB = New DateTime(2015, 8, 12)
            TestObj.Gender = "M"c
            TestObj.Name = "Abraham Lincoln"
            L.Add(TestObj)
    
            TestObj = New TEST_DATA_Group_LIB_NUnit_Assert_Multiple()
            TestObj.DOB = New DateTime(2015, 8, 12)
            TestObj.Gender = "F"c
            TestObj.Name = "George Washington"
            L.Add(TestObj)
    
            TestObj = New TEST_DATA_Group_LIB_NUnit_Assert_Multiple()
            TestObj.DOB = New DateTime(2015, 8, 12)
            TestObj.Gender = "A"c
            TestObj.Name = "John Hancock"
            L.Add(TestObj)
    
            Return L
        End Function
    End Class
    
    Public Class TEST_DATA_Group_LIB_NUnit_Assert_Multiple
    
        Public Property DOB As Date
    
        Public Property Gender As Char
    
        Public Property Name As String
    
    End Class
    
    0 讨论(0)
提交回复
热议问题