Unit Testing for VSTO (part 1 Manual Tests)

Introduction

One question that I hear from developers creating advanced VSTO solution is how to create unit tests using Visual Studio 2005 Team Edition for Software Testers (VSTS).  VSTS doesn’t contain a test template for VSTO projects. The Add New Test dialog box includes specialized tests for web projects, manual tests and load tests but not VSTO. This means that we need to roll our own code to make this test infrastructure work. Unit tests allow us to run some code and determine if it was successful. There are 2 types of unit tests in VSTS, manual and automatic. I will show you how to setup both of these tests. VSTS also supports code coverage. Code Coverage is the ability to track which lines of code ran during a test. 

Manual Test

The easiest test to get working is the manual test. This test type requires no code changes. Let’s take a look at how to do this.

Creating the test host

1. Create a new Excel or Word VSTO solution in C# or VB. In these samples I will be using VB. Choose a location and name of your choice. I will be using the default name of ExcelWorkbook1

2. Write some code for you to test. Let’s write some code so that you will have something to test. You will add a button to the actions pane and a couple of methods to call. Right – Click on ThisWorkbook.vb and choose View Code. Replace the existing code with the following code.

Public Class ThisWorkbook

    Dim WithEvents button1 As New Button

    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)

    End Sub

    Private Sub ThisWorkbook_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click

        WriteToCell("FooBar")

    End Sub

    Public Sub WriteToCell(ByVal CellText As String)

        'Write the string to cell A1

        If CellText Is Nothing Then

            CellText = "Nothing"

        End If

        Globals.Sheet1.Range("A1").Value = CellText

    End Sub

    Public Function ReadFromCell()

        'return the value of cell A1

        Return Globals.Sheet1.Range("A1").Value

    End Function

    Public Function Divide(ByVal X As Integer, ByVal Y As Integer) As Double

        Return X / Y

    End Function

End Class

Creating the Manual Test

3. Add a Unit Test to your solution. Click File-Add-New Project from the main menu. Choose the Test project template from the Visual Basic-Test folder.

After the test project is added you will see a test project and a couple of solution items which are configuration files for the tests. You can ignore these for now.

4. Open the Manual Test. Manual tests are nothing more than a .mht file that has been added to your project. This is just an html file that has the directions for the tester. One is created by default called ManualTest.mht. Double-Click this file to open it in Word. You can see that the default template has some starter text to help you write a good manual test. Close the manualtest1.mht file when are finished examining it.

Running the manual test

5. 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 have 2 tests (created by the default project template). One test is a sample manual test and the other a sample automated test. I will talk about automated tests in the next section.

6. Run the Manual Test. Select ManualTest1 in the Test View and click on the Run selected test button.

7. Execute the Test. Once the test begins you will see a couple of warning dialogs that you can skip through.

This dialog is just warning you that if you are running multiple tests at the same time that the test run is not complete until all tests have finished, including manual tests.

This dialog is just letting you know that the test framework is ready. And you can begin your test. So don’t start the test before you see this dialog.

8. Open the Workbook. The steps for your test are to open the Excel workbook you created in the previous steps. Once the workbook opens click the button in the actions pane. If you see FooBar in Cell A1 the test passed.

9. Pass or Fail. With manual tests the only way the system knows if the test passed or not is if you tell it. Click Pass in the Select Results section. Enter any comments about the test in the comments textbox. Click Apply to record the results.

OK there is a lot going on in this screen shot. This is what you see when your test is running.

Analyze the results

10. Test Results. Double Click on the Passed test in the Test Results window at the bottom.

In the next post, Unit Testing for VSTO (part 2 Automated Tests), I will show you how to create automated tests for VSTO.