Search code examples
pythonrallypyral

Adding a picture to Rally artifact by using Pyral


I have been trying to add pictures in a Test Case in Rally by using Pyral.

I have been able to successfully add the attachment and the link to the picture.

However, the picture is not displayed:

Incorrect image

When I download the picture from the attachments, it is a 1 kB picture (while it should be 37 kB) and cannot be opened.

I am using the following code code

    TCID = "TC1234"
    attachment = rally.addAttachment(TCid, "picture_new.jpg", mime_type="image/jpeg")
    Step['ExpectedResult']='Test picture<br /><img src="/slm/attachment/{oid}/{Name}" />'.format(**attachment.__dict__)
    list_Steps.append(Step)
    #... and some code to update the Test Steps in the Test Case that works fine 

The following code works fine with a text file (the attached file has the correct size and content) but not for the picture.

Is it something wrong in my code or in the API?


Solution

  • The problem came from the way the Rest API read the content from the file

    #extract from function addAttachment in file 'restapi.py'
    with open(filename, 'r') as af:
        contents = base64.b64encode(af.read())
    

    This works fine for text files but not for binary files.

    A temporary solution is to patch function addAttachment of restapi.py by reading the file as binary open(filename, 'rb'), which would work as well for text files.

    with open(filename, 'rb') as af:
        contents = base64.b64encode(af.read())
    

    And that worked fine for me afterwards: Correct import of picture

    Note: On my computer (Windows), the file restapi.py can be found in:

    {Python Install dir}/Lib/site-packages/pyral/restapi.py