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.

Thursday, March 4, 2010

Windows 7 Networking

So the last time I posted about Windows 7 my desktop machine failed to boot. For a long while I ignored it as it was not all that important to anything I was doing.

Anyway recently I was passing the machine and I pressed the power button and what do you know it booted. It had to run some repair routines but it booted. So I ran a virus scan and did get some report of a virus. Also an external drive had some hidden files that I could not access. So I removed the virus and reformatted the drive.

Then I went back to installing the shared printer. The routine was the same as I described in the previous post but again no documents would print. So I found a tip (cannot remember where right now) but simply install the printer as local (after installing the networked printer) and add a local port that maps to:


\\server\pinterShareName

I then has to install the same printer on my nephews mac notebook. So from this thread I followed the steps listed, which I will repeat here.
On the Windows 7 PC:

  1. Open Control panel
  2. Select 'Programs and Features' pane
  3. Click 'Turn Windows Features on or off'
  4. Check on the LDP protocol
  5. Make sure that the printer is shared and that the shared name is short and all one word with no spaces


On the Mac:

  1. Start/Applications/Unitities/Printer Setup Utility
  2. Hold down the option key and click the 'More Printers' button
  3. From the top menu select 'Advanced'
  4. From the Device field select 'LDP/LPR Host or Printer'
  5. In the 'Device url:' field type 'lpd://PCName/PrinterShareName'
  6. Click the Add Button

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 ...