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 Class
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()
Create the Client test hookup
Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
<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()
ThisWorkbook = CType(Activator.GetObject( _
GetType(ExcelWorkbook1.ThisWorkbook), _
"tcp://localhost:8085/ExcelWorkbook1"), ExcelWorkbook1.ThisWorkbook)
<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()
<TestMethod()> Public Sub TestMethod1()
' TODO: Add test logic here
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)
<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)
<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)
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.