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.
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.
'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)
RemotingServices.Marshal(Globals.ThisWorkbook, " ExcelWorkbook1")
Public Shared Sub Unregister()
Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
button1.Text = "Click to Test"
'Start the remoting server so Unit Testing can occur
Create the Client test hookup
<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.Visible = True
'Get the VSTOTestWrapper object
Dim channel As New TcpChannel()
ThisWorkbook = CType(Activator.GetObject( _
<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
<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
'passed if there are no exceptions
<TestMethod(), Description("Test writing to a cell")> _
Public Sub WriteToCellTest()
Dim TestString As String = "Test String"
'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
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.