Search code examples
.netvb.netunit-testingassert

Unit Testing, automate to display Assert result for every object in collection


I'm new to unit testing practices, the problem I have is in the code below, the method TestEnums it seems to stop/break the iteration when the first Assert fail, I mean, it only shows one failed error message in the Test Explorer.

I would like to adapt this code to show all the failed asserts in the Test Explorer, that is, every tested Enum that failed the assert of EnumHasRepeatedValues method.

Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.Reflection

<TestClass()>
Public Class Application

    <TestMethod()>
    Public Sub TestEnums()
        Dim ass As Assembly = Assembly.Load("Elektro.Application")

        Dim types As IEnumerable(Of Type) =
            From t As Type In ass.GetTypes() Where t.IsEnum

        For Each t As Type In types
            EnumHasRepeatedValues(t)
        Next
    End Sub

    Public Sub EnumHasRepeatedValues(ByVal t As Type)
        Assert.AreEqual([Enum].GetValues(t).Length,
                        [Enum].GetNames(t).Length,
                        String.Format("Enum {0}.{1} has defined repeated values.", 
                                      t.Namespace, t.Name))
    End Sub

End Class

Solution

  • Testing methods should be independent of each other. Do not call test methods from other test methods.

    A test by definition fails at the time your first assertion fails, that's why you see only one test failure.

    If you want to test all the enum values, write a separate test for each.