In my last post, Unit Testing for VSTO (Part 1 Manual Tests), I showed you how to use VSTS to create manual unit tests. In this post you will learn how to create automated tests.

Automated Tests

You now have a basic understanding of how the manual unit testing works. But you don’t want to sit there and run hundred of tests manually. Automated tests allow you to pass data to your application to test various scenarios. The test can automatically determine if the test passes or fails. Let’s take a look at how to enable automated tests for VSTO 2005.

1. Open the ExcelWorkbook1 project you created in the previous section.

2. Add Remoting Server to the host app. The first thing you will do is add the server code to your Excel workbook. When the workbook opens the server will start. This will allow the test client to communicate with the workbook to run the tests. Add a new class to your ExcelWorkbook1 project called RemotingServer.vb. Add the following code to this class.

Imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Imports System.Runtime.Remoting.Channels.Tcp

 

'This class is to enable unit testing

Public Class RemotingServer

 

    Shared channel As TcpChannel

 

    Public Shared Sub Start()

        'pick any open channel number

        channel = New TcpChannel(8085)

        ChannelServices.RegisterChannel(channel, False)

        RemotingServices.Marshal(Globals.ThisWorkbook, " ExcelWorkbook1")

    End Sub

 

    Public Shared Sub Unregister()

        ChannelServices.UnregisterChannel(channel)

    End Sub

 End Class

  1. Add a Reference to System.Runtime.Remoting. Right Click on the ExcelWorkbook1 project and choose add references. Browse the .Net references for System.Runtime.Remoting.
  2. Start remoting server on startup. Add the following code to your existing workbook startup event handler

    Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        button1.Text = "Click to Test"

        ActionsPane.Controls.Add(button1)

 

        'Start the remoting server so Unit Testing can occur

        RemotingServer.Start()

    End Sub

  1. Verify Host. Press F5 to Build and Run the ExcelWorkbook1 project. The workbook should open with no error.

Create the Client test hookup

  1. Add 2 Refereces to UnitTest1 project. Right Click on the UnitTest1 project and click Add References. Under the Com tab of the add references dialog select Microsoft Excel 11 Object Library. Click OK. Now add a reference to the ExcelWorkbook1 project following the same steps as above. Note you may need to click on browse tab to browse to the output location where the ExcelWorkbook1.dll is. The last reference you need to add is under the .net tab of the add references dialog, System.Runtime.Remoting.
  2. Open the automated test. Double click on the UnitTest1.vb file in the TestProject1 project. The automated test will open.
    These next steps are shown in the code block in step 6
  3. Add the Import statements.
  4. Add shared variables to hold references to the workbook you are testing
  5. Add a ClassInitialize to hook up the unit test to the workbook. This is the client part that talks to the server you created in the previous section.
  6. Add a ClassCleanup to close the workbook when the tests are finished.
    Replace the code in the UnitTest1.vb file with the following code.

Imports System

Imports System.Text

Imports System.Collections.Generic

Imports Microsoft.VisualStudio.TestTools.UnitTesting

 

Imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Imports System.Runtime.Remoting.Channels.Tcp

 

<TestClass()> Public Class UnitTest1

 

    Shared ThisWorkbook As ExcelWorkbook1.ThisWorkbook

    Shared workbookToTest As String = "C:\ExcelWorkbook1\ExcelWorkbook1\bin\Debug\ExcelWorkbook1.xls"

 

    <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)

        'start the VSTO Word or Excel document

        Dim ExcelApp As Microsoft.Office.Interop.Excel.Application

        ExcelApp = New Microsoft.Office.Interop.Excel.Application

 

        ExcelApp.Workbooks.Open(workbookToTest)

        ExcelApp.Visible = True

 

        'Get the VSTOTestWrapper object

        Dim channel As New TcpChannel()

        ChannelServices.RegisterChannel(channel, False)

        ThisWorkbook = CType(Activator.GetObject( _

        GetType(ExcelWorkbook1.ThisWorkbook), _

        "tcp://localhost:8085/ExcelWorkbook1"), ExcelWorkbook1.ThisWorkbook)

    End Sub

 

    <ClassCleanup()> Public Shared Sub MyClassCleanup()

        'Get a reference to the workbook to close it

        Dim workbook As Microsoft.Office.Interop.Excel.Workbook

        workbook = GetObject(workbookToTest)

        workbook.Saved = True

        workbook.Close()

        workbook.Application.Quit()

    End Sub

 

    <TestMethod()> Public Sub TestMethod1()

        ' TODO: Add test logic here

    End Sub

 

End Class

Add the unit tests

You are now ready to start creating your unit tests. These are the automated tests that will exercise the features in your application. Let’s get started by creating a few tests.

1. Add the following 3 unit tests to the UnitTest1 class.

    <TestMethod(), Description("Test reading from a cell")> _

    Public Sub ReadFromCellTest()

        'Sub to test

        ThisWorkbook.ReadFromCell()

 

        'passed if there are no exceptions

        Assert.IsTrue(True)

 

    End Sub

 

    <TestMethod(), Description("Test writing to a cell")> _

    Public Sub WriteToCellTest()

        Dim TestString As String = "Test String"

 

        ThisWorkbook.WriteToCell(TestString)

 

        'now read the string to verify that it wrote it

        Dim celltext As String = ThisWorkbook.ReadFromCell()

 

        'verify the 2 strings are the same

        Assert.IsTrue(TestString = celltext)

 

    End Sub

 

    <TestMethod(), Description("Test dividing 2 numbers")> _

    Public Sub DivisionTest()

        Dim answer As Double

        answer = ThisWorkbook.Divide(6, 2)

 

        'This should equal 3

        Assert.AreEqual(answer, 3.0)

 

    End Sub

Running the manual test

1. Open the Test View Window. You will run your tests from the Test View. Click Test-Windows-Test View to open the list of all of your tests. In this case we now have 4 tests.

2. Run the Automatic Tests. Select all of the automated tests in the Test View and click on the Run selected test button.

3. You can monitor the progress of the tests in the Test Results window

4. You can see the final results in the Test Results window.

In the next post I will show you how to get code coverage and performance numbers from these automated tests.