Monday, March 29, 2010

Unit Testing in Visual Studio 2010 and converting from NUnit

Previously I had a project in VB.NET 2005 that was developed using a Test Driven Development approach. My test framework of choice was NUnit .
The screen shot Left should an example of the NUnit user interface in action. In this case with failing and interrupted tests.

In this case the tests are failing but in a sucess case the progress bar would be green and the unit test would have green buttons instead of red ones as in the image below.

One thing to notice is that NUnit gui automatically groups the tests according to the dll it is in and the class that it belong to.


 Now, the code is pretty simple, you can add a class, import the reference to the NUnit Framework and add the attribute to the class and a attribute to method that implements the test as shown in the code sample below.


<TestTixture()>
Public Class TestLookup

Private mLk As New Lookup

<Test()>
Public Sub TestNewLookup()

Dim lk As New Lookup
Assert.IsNotNull(lk)
End Sub

End Class


When converting to Visual Studio 2010 release candidate I found that it was most convenient to recreate the project files in Visual Studio and copy the code files in to the appropiate folder and then add them to the projects. In my case it also helped clean up the project structure that had become a little disorganised over the project cycle. So I cannot comment on the ability of Visual Studio to convert from the earlier format. I also choose this route as I wanted to use the built in unit testing rather than NUnit. Visual Studio holds unit test is a specific project type and I have not found any obvious way of converting a code library to a test project. I suppose I could go around editing the project files but at this stage that was not a route I wanted to take.




The Visual Studio Test Editor window is seen above and the test list on the left is manually organised by creating test list that correspond to the classes. The tests themselves are included automatically when you convert the NUnit attributes to their Visual Studio equivalents.

The following code shows a class originally created in NUnit now in VS test format

Imports Techrete.MasterListData
Imports Techrete.SharedLibrary
Imports System.Data.SqlClient
Imports Microsoft.VisualStudio.TestTools.UnitTesting

<testclass()> _

Public Class TestManageIndices


    Private mObject As String

    Private mPrefix As String

    Private mDoReset As Boolean = False

    Private mResetIndexValue As Integer


<testcleanup()> _
Public Sub Cleanup()

       Dim cn As SqlConnection = TestComponentsData.GetDataConnection

        Dim cmd As New SqlCommand("Update csm_ManageUserIndexes Set LastIndex = " & mResetIndexValue.ToString _
 " Where [Object] = '" & mObject & "' and Prefix = '" & mPrefix & "'", cn)

        If mDoReset Then

            Try
                cn.Open()

                cmd.ExecuteNonQuery()

            Finally

                cn.Close()

            End Try

        End If

    End Sub

    <testmethod()> _
    Public Sub TestAssemblyIndex()

        Dim lastIndex As Integer

        Dim currentIndex As Integer

        Dim prefix As String = "F"

        'Now obtain the last index and increment by 1

        'then save it - that should succeed and save it again - that should fail.

        Dim sql As String = "Select LastIndex from csm_ManageUserIndexes Where Object = 'Assembly'"

        Dim cn As SqlConnection = TestComponentsData.GetDataConnection

        Dim cmd As New SqlCommand(sql, cn)
        Try

            cn.Open()
            lastIndex = CInt(cmd.ExecuteScalar)
        Finally

            cn.Close()

        End Try

        currentIndex = IndexData.GetLastIndex("Assembly", prefix)

        Assert.AreEqual(lastIndex, currentIndex)

    End Sub
 End Class

One big difference is that in the current release of Visual Studio the Assert.AreEquals does not appear to work as in NUnit. In that you can have two object of the same type and with the same property values and in NUnit they will pass but in Visual Studio they will be considered not equal. It appears that at the moment Visual Studio only accept that two object that have the same address in memory are equal and there is no functional difference between Assert.AreEquals and Assert.AreSame.

No comments:

Post a Comment

Fantasygrounds Unity: Importing an NPC from a text stat block.

  Fantasygrounds has been my primary VTT of choice for many years now. For most of that time I have been running games using official WoTC ...