Skip to content

Unit Testing

ckuhn203 edited this page Nov 24, 2014 · 25 revisions

Test Explorer

The Test Explorer allows browsing/finding, running, and adding unit tests to the active VBProject:

Test Explorer window

The Refresh command synchronizes the test methods with the code in the IDE, but if test methods are added from within the Text Explorer then the new tests will appear automatically.

The Run menu makes running the tests as convenient as in the .NET versions of Visual Studio:

Test Explorer 'Run' menu

"Selected Tests" refer to the selection in the grid, not in the IDE.

The Add menu makes it easy to add new tests:

Test Explorer 'Add' menu

Adding a Test Module ensures the active VBProject has a reference to the add-in's type library, then adds a new standard code module with this content:

'@TestModule
Option Explicit
Private Assert As New RetailCoderVBE.AssertClass

Adding a Test Method adds this template snippet at the end of the active test module:

'@TestMethod
Public Sub TestMethod1() 'TODO: Rename test
    On Error GoTo TestFail

    'Arrange

    'Act

    'Assert
    Assert.Inconclusive
    
TestExit:
    Exit Sub
TestFail:
    If Err.Number <> 0 Then
        Assert.Fail "Test raised an error: " & Err.Description
    End If
    Resume TestExit
End Sub

Adding a Test Method (expected error) adds this template snippet at the end of the active test module:

'@TestMethod
Public Sub TestMethod2() 'TODO: Rename test
    Const ExpectedError As Long = 0 'TODO: Change to expected error number
    On Error GoTo TestFail
    
    'Arrange

    'Act

    'Assert
    Assert.Fail "Expected error was not raised."
    
TestExit:
    Exit Sub
TestFail:
    Assert.AreEqual ExpectedError, Err.Number
    Resume TestExit
End Sub

The number at the end of the generated method name depends on the number of test methods in the test module.

Clone this wiki locally