Run the test instances in a test set locally, remotely, or as planned

Public Sub RunTestSet(tsFolderName As String, tSetName As String, _
        HostName As String, runWhere As Integer)

' Run the test instances in a test set locally, remotely, or as planned
' Get status information when running a test set

' This example show how to run a test set in three different ways:
' * Run all tests on the local machine (where this code runs).
' * Run the tests on a specified remote machine.
' * Run the tests on the hosts as planned in the test set.
    Dim TSetFact As TestSetFactory, tsList As List
    Dim theTestSet As TestSet
    Dim tsTreeMgr As TestSetTreeManager
    Dim tsFolder As TestSetFolder
    Dim Scheduler As TSScheduler
    Dim execStatus As ExecutionStatus
    On Error GoTo RunTestSetErr
    errmsg = "RunTestSet"
' Get the test set tree manager from the test set factory.
    'tdc is the global TDConnection object.
    Set TSetFact = tdc.TestSetFactory
    Set tsTreeMgr = tdc.TestSetTreeManager
' Get the test set folder passed as an argument to the example code.
    Dim nPath$
    nPath = "Root\" & Trim(tsFolderName)
    On Error Resume Next
    Set tsFolder = tsTreeMgr.NodeByPath(nPath)
    If tsFolder Is Nothing Then
        err.Raise vbObjectError + 1, "RunTestSet", "Could not find folder " & nPath
        GoTo RunTestSetErr
    End If
    On Error GoTo RunTestSetErr
' Search for the test set passed as an argument to the example code.
    Set tsList = tsFolder.FindTestSets(tSetName)
    If tsList.Count > 1 Then
        MsgBox "FindTestSets found more than one test set: refine search"
    ElseIf tsList.Count < 1 Then
        MsgBox "FindTestSets: test set not found"
    End If
    Set theTestSet = tsList.Item(1)
    Debug.Print theTestSet.ID

' Start the scheduler on the local machine.
    Set Scheduler = theTestSet.StartExecution("")

'Set up for the run depending on where the test instances
' are to execute.
    Select Case runWhere
        Case RUN_LOCAL
        ' Run all tests on the local machine.
            Scheduler.RunAllLocally = True
        Case RUN_REMOTE
        ' Run tests on a specified remote machine.
            Scheduler.TdHostName = HostName
            ' RunAllLocally must not be set for
            ' remote invocation of tests.
            ' Do not do this:
            ' Scheduler.RunAllLocally = False
        ' Run on the hosts as planned in the test set.
            Dim TSTestFact As TSTestFactory, testList As List
            Dim tsFilter As TDFilter
            Dim TSTst As TSTest
        ' Get the test instances from the test set.
            Set TSTestFact = theTestSet.TSTestFactory
            Set tsFilter = TSTestFact.Filter
            tsFilter.Filter("TC_CYCLE_ID") = theTestSet.ID
            Set testList = TSTestFact.NewList(tsFilter.Text)
            Debug.Print "Test instances and planned hosts:"
        'For each test instance, set the host to run depending
        '  on the planning in the test set.
            For Each TSTst In testList
                Debug.Print "Name: " & TSTst.Name & " ID: " & TSTst.ID & " Planned Host: " & TSTst.HostName
                Scheduler.RunOnHost(TSTst.ID) = TSTst.HostName
            Next TSTst
            Scheduler.RunAllLocally = False
    End Select
' Run the tests.
' Get the execution status object.
    Set execStatus = Scheduler.ExecutionStatus
' Track the events and statuses.
    Dim RunFinished As Boolean, iter As Integer, i As Integer
    Dim ExecEventInfoObj As ExecEventInfo, EventsList As List
    Dim TestExecStatusObj As TestExecStatus
    While ((RunFinished = False) And (iter < 100))
        iter = iter + 1
        execStatus.RefreshExecStatusInfo "all", True
        RunFinished = execStatus.Finished
        Set EventsList = execStatus.EventsList
        For Each ExecEventInfoObj In EventsList
            Debug.Print Tab; "Event: " & ExecEventInfoObj.EventDate & " " & _
                    ExecEventInfoObj.EventTime & " " & _
                    "Event Type: " & ExecEventInfoObj.EventType & " [Event types: " & _
                    "1-fail, 2-finished, 3-env fail, 4-timeout, 5-manual]"
        Debug.Print Tab; execStatus.Count & " exec status"
        For i = 1 To execStatus.Count
            Set TestExecStatusObj = execStatus.Item(i)
            Debug.Print Tab; "Iteration " & iter & " Status: " & _
                        " Test " & TestExecStatusObj.testID & _
                        " ,Test instance " & TestExecStatusObj.TestInstance & _
                        " ,order " & TestExecStatusObj.TSTestID & " " & _
                        TestExecStatusObj.Message & ", status=" & _
        Next i
        'Sleep() has to be declared before it can be used.
        'This is the module level declaration of Sleep():
        'Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
        Sleep (5000)
    Wend 'Loop While execStatus.Finished = False
    Debug.Print "Scheduler finished around " & CStr(Now)
Exit Sub
' Handle error
End Sub