QC & QTP – How to Attach a File to a Test Sets Attachment Tab

QC/ALM Published on:

Do you need your test scripts to save test files in QC? Or do you may have a file that contains test data that you want your scripts to read from? Yesterday I received an email form my blog friend Kumar that asked how this can be done using Quality Center and QTP. Hopefully this post will explain how this functionality can be achieved. You also might want to check out How to use QTP to save and retrieve a Quality Center Test Resource

Steps to attach a file in QC

1. In QTP Create a new test named TestLabAttachment and save in a folder in Quality Center:

2. In QC add the QTP script ‘TestLabAttachment' into a test set in the QC ‘Test Lab” section

3. Create a local text file on your QTP machine that you want to add to the QC ‘Attachments' tab. For example:

3. In QTP add the following lines of code:

Set nowTest = QCUtil.CurrentTestSet

Set attachmentPath = nowTest.Attachments

Set nowAttachment = attachmentPath.AddItem(Null)

‘Replace with the path to your file:

nowAttachment.FileName = “C:\QTPAUTOMATION\DATA\testenv.txt”

nowAttachment.Type = 1


4. Run QTP script. In the Run dialog select ‘Results Location' tab and click on ‘New run Results in QC project”. Select the Test Set that the current script is saved in.

5. After the QTP script runs go back to QC and look under the test set's ‘Attachments' tab. The file should appear:

Steps to retrieve file from the QC Test Set ‘Attachments' tab

    1. Open QTP
    2. In the same script that you created above TestLabAttachment enter the following code (“I admit this code is ugly – but it works. If you know a better way please let me know”)

  1. Run QTP script. In the Run dialog select ‘Results Location' tab and click on ‘New run Results in QC project”. Select the Test Set that the current script is saved in.

4. After the QTP script runs go the folder where you saved your file. The QC file should now be in it.

Also be sure to check out Application Lifecycle Management for the Whole Team

36 responses to “QC & QTP – How to Attach a File to a Test Sets Attachment Tab”

  1. Thanks, this worked fine. The problem i am having is how do i skip this function when i am running a test locally vs through quality center. I need to confirm a QC connection and skip this when it’s not connected (running locally) .

    • greg woffindin » Hi Greg – sorry for my late response. If you have not figured this out already you could use something like this:
      ‘This is if the test is truly local meaning the QTP script is not stored in QC
      qcEnabled = QCUtil.IsConnected
      if qcEnabled = “True” then
      yourcode QC attachement code
      end if

      If the script is stored in QC and you want to run the script from QTP not QC you need to store the script in a test lab. When you click run in the ‘Results Location” tab point to the scripts test lab in the ‘new run results in QC’ ‘Test Set’ option.

      Hope this helps you.


  2. Thanks, I figured out what i needed. Actually what i have is first a check to see if an attachment is stored in QC in the test lab section, if not it then checks for a copy of the attachment on the pc running the script and then if it can’t find it uses a default value. I was loading some environment variables and wanted to allow someone running the script via QC to easily change them and have a way to run the scripts on a local pc for creating/troubleshooting and be able to change them.

  3. Hello Joe,
    You have an excellent site with tons of resources. I am glad to say I have been a beneficiary of this site since the time I found. Many Thanks for maintaing such a wonderful site.
    Have a question about creating shortcut keys for menu items of an AUT. We have a custom written .Net application which doesnt lend itself easily to the recording feature of QTP(11.0), as it appears all the objects are private. Someone suggested the only way of accessing the Menu Items would be to custom map shortcut keys and then navigate to the objects on the pages opened.
    Wondering if you ran into a similar situation and considered taking this approach. would appreciate your thoughts and guidance on how this may be achieved.


    • Murali » Hi Murali – thanks! I’m glad that you find the site helpful! I have seen instances when I do need to use either SendKeys or a Windows type function to access a AUT functionality. I would give either method a try and see how reliable it works for your app. Also I would ask a developer if there was a way to make the menu more automation friendly. Either by using standard controls or exposing a method the help QTP recognize it. Hope this helps. Cheers~Joe

  4. Joe,

    Can you help me in explaining how we can able to attach a file to the current step at runtime using QTP? We have a requirement to upload a snapshot for each of the step that has been executed using automated script. We were able to upload a file or snapshot to current test run, but not to step. Appreciate your help.


    • Venkat » Hi Venkat I haven’t had time to try to write to a specific test step in a test. BUt does the QTP>Options>Run>Screen capture set to “always” options capture enough info to meet your requirements?

      • Hey, it is set as always but still the screenshot is not attached fr each step.

        Actually, i have used a report fail fn and within that upload function exist. I’ve used the below fn

        Public Function UploadScreenshotQC(ScrnshotFileName)
        Dim Test_CurrentRun,Test_Attachment,Test_AttachItem’,Test_AttachStore
        Dim Local_Path,Local_FileName

        Set Test_CurrentRun=QCUtil.CurrentRun

        ‘Check that the test is running from QC, otherwise it will exit
        If (Test_CurrentRun Is Nothing) Then
        Exit Function
        End If

        ‘Attach the File to the current test
        Set Test_Attachment=Test_CurrentRun.Attachments
        Set Test_AttachItem=Test_Attachment.AddItem(Null)

        End Function

        • Try:
          ‘sFilePath is the location and name you want the file captured to;
          ‘oToCapture is the object you want too screenshot;
          ‘NOTE: If no object exists and you want the whole desktop (as in the case of a catastrophic failure in test) just supply NULL as the object;
          Function ScreenshotForTestResult(sFilePath,oToCapture)
          If isNull(oToCapture) or isEmpty(oToCapture)
          Desktop.CaptureBitmap sFilePath,True
          oToCapture.CaptureBitmap sFilePath,True
          End If
          Reporter.ReportEvent micDone,”Screen Capture”,”Screen Capture”,sFilePath
          End Function

  5. Joe, Is there any way to provide a link to the attachments in that particular test set inside the QC Report? This would help the person looking into the QC report as they can directly click on the link to go to the uploaded attachments

  6. Can I use above code (attachment to QC) with others tools as well like Testing anywhere as for those tools we are not storing scripts in QC only uploading results .

  7. Using QCUtil it is possible to add screenshot to current test, current run, test set level. But is it posible to add the screensot in test step level….Pls advice

  8. I have results from a SOAPUI Pro script that I would like to attach to the Test Lab Test Suite, but I can only see how to do it one at a time. Is there a way to attach multiple files at once?

  9. how to attach multiple files in test lab module for each test case in ALM 11.0 version? Is there any script other than QTP?

  10. Hi Joe-
    I have a BPT in which an XML file is generated; the XML file is then attached to the test set (via QTP). Is there a way within QTP to ‘refresh’ the connection to QC/ALM so that the next test in the (same) BPT can access the attached file? Maybe something using QCUtil?

    Thanks in advance for any suggestions!!

  11. Hi the above code is to attach a file to test plan module right!
    but not test lab module….can you please explain me how to attach a file to test lab module in qc. Appreciate your help.

  12. Joe – Please ignore my previous request about ‘How to delete an attachment from test lab/test set’. i found my answer, below is the code to delete an attachment for test lab.

    Function RemoveQCAttachements(NameOfTheFile)
    Set nowTest = QCUtil.CurrentTestSet
    Set otaAttachmentFactory = nowTest.Attachments
    Set attachList = otaAttachmentFactory.NewList (“”)
    var_count = attachList.Count
    For i= 1 to var_count
    strFileName = attachList.Item(i).Name ‘Name of the file
    strFilePathwithName = attachList.Item(i).FileName ‘File complete path with name
    intAttachmentID = attachList.Item(i).ID ‘File Attachment ID
    otaAttachmentFactory.RemoveItem(intAttachmentID) ‘remove attachment
    End Function

    Sometimes late response also resolves the issues :) thank you !!

  13. I am getting my connection lost with the current test which i am executing but i Have the QC CONNECTION LIVE.

  14. Hi Joe, I’m naveen from CSC, I got a requirement like… I’ve to download excel spread sheet from qc, modify it and then upload it to same place, but here QC is not accepting to override the file, its creating new file by addir +1 +2 etc, please suggest me how can I save/override same file with modifications.


  15. Hi Joe.. I just want to check whether all the testcases inside the testlab are executed and it has attachments for the verifying results I need to generate the report for the same to validate the attachments are there or not.. with out using QTP.. I think we need to use some SQL queries for that.. Please help me in that..ASAP..

  16. I need to search for a particular attached file name in test plan module.how to get the test case name with which the file attached.

  17. Hi Joe. Thanks a lot for sharing all these. Really appreciate your efforts and support. Thanks again….!!!!

  18. hi Joe,

    Please help me with the code for the below scenario:

    QTP script to write a sql query result into an excel sheet and then upload this into the test lab folder as attachment in QC..

    Can this be done..??


  19. The below code work fine to update any file to ALM – QC

    Public Function addAttachToTDRun (strFileName)
    Dim CurrentRun, attachment

    On error resume next
    If QCUtil.IsConnected Then
    Set CurrentRun =QCUtil.CurrentRun
    Set attachment = CurrentRun.Attachments
    If Err.Number = 424 Then
    Reporter.ReportEvent micWarning, “Not running through QC so can’t save results”, “”
    Reporter.ReportEvent micDone, “Attachment Saved to QC run data”, strFileName
    Set attlist = attachment.NewList(“”)
    Set att = attachment.AddItem(Null)
    att.FileName = strFileName
    att.Type = 1
    att.Save False
    End If

    Reporter.ReportEvent micWarning, “Not connected”, “Not connected to Quality Center”

    End If

    On error goto 0

    End Function

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.