1

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Have you read through [this](https://msdn.microsoft.com/en-us/library/ms182527.aspx) about using a data source? – A Friend Dec 14 '16 at 14:18
  • @Pro Grammer thanks for comment, however, I'm unsure if that kind of solution could help me to acchieve this, it is necessary to generate a database like in the MSDN example?. – ElektroStudios Dec 14 '16 at 14:38
  • From what I've seen you could embed a [CSV, XML or a local data source](http://stackoverflow.com/a/14139050/6144259) into your test library. It doesnt have to be a full database. I also found [this](http://stackoverflow.com/a/13710788/6144259) which might be of more help – A Friend Dec 14 '16 at 15:05
  • Consider using [NUnit](https://www.nunit.org/index.php?p=download). It has tools for [test summary](https://launchpad.net/nunit-summary/) or even [html reports](https://launchpad.net/nunit-results/). Even if this is not 100% what you are looking for it is way more mighty and flexible than MSUnit. – Alex B. Dec 14 '16 at 18:34
  • @Alex B Thanks but it is a greenfield project, well... less than that, it is a free open source project and the unit tests should be available to compile for the end-user, without forcing they to install 3rd party solutions like Nunit, NCrunch, JustMock, or any other. If only were for me, then probably I would install something more flexible than what Microsoft provides. – ElektroStudios Dec 14 '16 at 19:32
  • `Microsoft.VisualStudio.TestTools.UnitTesting` is kind of third party framework too which you need reference in your project. You can add other usefull dependecies as testing frameworks(NUnit, XUnit ..) or mocking libraries (Mock, NSubstitute..) or assertions libraries (FluentAssertions). Add them through NuGet and anybody who load your project will be able to restore all those dependencies. FluentAssertions actually would be great help in your case. – Fabio Sep 27 '17 at 18:57
  • You can always compare collection of enum types against expected collection - in this case you will get one "failed" message with explanation in one test – Fabio Sep 27 '17 at 18:58

1 Answers1

2

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.

Stefan
  • 1,590
  • 3
  • 18
  • 33
  • Thanks for your answer, but I can't do that, literally are more than 200 enums counting all the assemblies to test. For that reason I'm in the need to automate the enum testing in the manner exposed. – ElektroStudios Dec 14 '16 at 14:22
  • Well, imagine that a test fails one time, it shows one single message only, then you fix that error, then run again the test, then again it fails, with one single error message always, its a loose of time, that is not productive, for me is always better to know ALL the errors/failed tests at once, than only one per turn. (sorry for my bad English) – ElektroStudios Dec 14 '16 at 14:28