April 10, 2011

Update Multiple Result to Quality Center using code

This code is given in Open API's of Quality Center. It will read result from csv or txt file and upload it in QC:

Option explicit

Dim qcUrl, qcLoginID, qcPassword, qcProject, qcTestSet, qcTestSetPath
Dim QCConnection, tsTreeMgr, tSetFolder, TestSetsList, theTSTest, TestSetTestsList
Dim theTestSet, TSTestFact, tsFilter, testID, passFailResult, requestType
Dim myTestList

qcUrl = "http://myQClink.com"
qcLoginID = "username"
qcPassword = "password"


'====================================================
'The next three lines determine the test set
'====================================================
qcProject = "MyProject"
qcTestSet = "MyTestSet"
qcTestSetPath = "Root\test"


'=====================================================
'CONNECT TO QC AND GET THE LIST OF TESTS FROM THE TEST SET
'=====================================================

'Connect to QC and log in; throw an error if connection fails
Set QCConnection = CreateObject("TDApiOle80.TDConnection")
QCConnection.InitConnectionEx qcUrl
QCConnection.login qcLoginID, qcPassword
QCConnection.Connect "QA", qcProject
    If not QCConnection.LoggedIn Then
        Msgbox "Failed to connect to QC."
        QCConnection.Disconnect
    End If

'get the test set folder indicated in qcTestSetPath environment variable
Set tsTreeMgr = QCConnection.TestSetTreeManager
Set tSetFolder = tsTreeMgr.NodeByPath(qcTestSetPath)


'Get the test set indicated in the qcTestSet environment variable
Set TestSetsList = tSetFolder.FindTestSets(qcTestSet)

'The method for finding a test set always returns a list; since we only have 1 item in the list, set item(1) as our active test set.
Set theTestSet = TestSetsList.Item(1)

'The TSTestFactory is the interface for managing test cases in a test set. Get this from the active test set
Set TSTestFact = theTestSet.TSTestFactory

'Create a list of tests from the selected test set
set TestSetTestsList = TSTestFact.NewList("")

'Open the list of results, split into an array
Dim objFSO, objTextFile, arrResults, testResults
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\App\release\result\result.csv",ForReading,False)
Do While Not objTextFile.AtEndOfStream
    arrResults = split(objTextFile.Readline, ",")
    testResults = testResults & "," & arrResults(0) & "," & arrResults(1)
Loop

objTextFile.Close
testResults = Split(testResults, ",")

'Find the test names in the testResults array that match the test case
'names in the test set, and upate with status

Dim testIdCounter,resultCounter, x
testIdCounter=3 'even numbered indexes are test IDs
resultCounter=4 'odd numbered indexes are results

Do While testIdCounter < Ubound(testResults)
    testID = "[1]" & testResults(testIdCounter)
    passFailResult = testResults(resultCounter)

    'Iterate through the list of test cases
    For Each theTSTest in TestSetTestsList

      With theTSTest
        'try to match the name of the test to the testID
        If .Name = testID Then
            theTSTest.Status = passFailResult
            theTSTest.Post
        End If
      End With
    Next

    testIdCounter = testIdCounter + 2
    resultCounter = resultCounter + 2

Loop
Msgbox "Test Results Uploaded"


csv file format (result.csv) :