首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用pyral 0.9.3向TestCaseResults添加附件

使用pyral 0.9.3向TestCaseResults添加附件
EN

Stack Overflow用户
提问于 2014-05-01 18:31:04
回答 2查看 810关注 0票数 2

我试图使用如下方式在一个测试器中添加一个附件:

代码语言:javascript
复制
   testCaseResult = rally.create('TestCaseResult', {'TestCase': tc.ref , 'Build': revision,
                          'Verdict': verdict[test.result], 'Date': resultdate, 'Notes': note,
                          'Tester': tester, 'Duration': runtime })


   res = rally.addAttachment(testCaseResult.oid, file);

TestaseResult已成功创建,但res为False。

我做错了什么?我不应该用那个开关吗?我试过传递testCaseResult、testCaseResult.oid和"TestCaseResult/“+ testCaseResult.oid,但似乎没有一种有效.

更新:

基于Mark以下的回答(pyral不直接支持向测试结果添加附件),我编写了以下子程序:

代码语言:javascript
复制
def addTestCaseResultAttachment(testCaseResult, filename, contentType='text/plain'):
    if not os.path.exists(filename):
        raise Exception('Named attachment filename: %s not found' % filename)
    if not os.path.isfile(filename):
        raise Exception('Named attachment filename: %s is not a regular file' % filename)

    attachment_file_name = os.path.basename(filename)
    attachment_file_size = os.path.getsize(filename)

    if attachment_file_size == 0:
        raise Exception('Cannot attach zero length file')

    if attachment_file_size > 5000000:
        raise Exception('Attachment file size too large, unable to attach to Rally Artifact')

    contents = ''
    with open(filename, 'r') as af:
        contents = base64.encodestring(af.read())

    # create an AttachmentContent item
    ac = rally.create('AttachmentContent', {"Content" : contents}, project=None)
    if not ac:
        raise RallyRESTAPIError('Unable to create AttachmentContent for %s' % attachment_file_name)

    attachment_info = { "Name"              :  attachment_file_name,
                        "Content"           :  ac.ref,       # ref to AttachmentContent
                        "ContentType"       :  contentType,
                        "Size"              :  attachment_file_size, # must be size before encoding!!
                        "User"              :  'user/%s' % me.oid,
                        "TestCaseResult"    :  testCaseResult.ref
                      }

    # and finally, create the Attachment
    attachment = rally.create('Attachment', attachment_info, project=None)
    if not attachment:
        raise RallyRESTAPIError('Unable to create Attachment for %s' % attachment_file_name)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-01 20:54:10

问题是,用于addAttachment的[医][医]肾盂后动脉方法为附件对象的"Artifact“属性设置了一个ref,如下所示(雷斯特皮的第1241至1251行):

代码语言:javascript
复制
    attachment_info = { "Name"        :  attachment_file_name,
                        "Content"     :  ac.ref,       # ref to AttachmentContent
                        "ContentType" :  mime_type,    
                        "Size"        :  attachment_file_size, # must be size before encoding!!
                        "User"        :  'user/%s' % self.contextHelper.user_oid,
                       #"Artifact"    :  artifact.ref  # (Artifact is an 'optional' field)
                      }

    # While it's actually possible to have an Attachment not linked to an Artifact,
    # in most cases, it'll be far more useful to have the linkage to an Artifact than not.
    if artifact:  
        attachment_info["Artifact"] = artifact.ref

因此,addAttachment方法实际上只适用于从工件继承的对象,即故事、缺陷、任务、TestCases等。如WSAPI文档所示,要将附件关联到TestCaseResult,所需的属性实际上是"TestCaseResult“。之所以选择这个语法,是因为TestCaseResult实际上是一个WorkspaceDomainObject,而不是一个人工制品。

下面是一个创建新TestCaseResult并添加附件的示例

代码语言:javascript
复制
#!/usr/bin/env python

#################################################################################################
#
# create_tcr_and_attachment.py -- Create a New TestCaseResult and attach a file to it
#
USAGE = """
Usage: py create_tcr_and_attachment.py <TestCaseFormatedID> <filename>
"""
#################################################################################################

import sys, os
import re
import string
import base64

from pyral import Rally, rallySettings

#################################################################################################

errout = sys.stderr.write

ATTACHMENT_ATTRIBUTES = ['oid', 'ObjectID', '_type', '_ref', '_CreatedAt', 'Name',
                         'CreationDate', 'Description', 
                         'Content', 'ContentType', 'Size', 
                         'Subscription', 
                         'Workspace',
                         'Artifact', 
                         'User'
                        ] 

ATTACHMENT_IMPORTANT_ATTRS = """
    Subscription   ref     (supplied at creation)
    Workspace      ref     (supplied at creation)

    Name           STRING      Required    (name of the file, like foo.txt or ThePlan.doc)
    User           ref to User Required   Settable  (User who added the object)

    Content        ref to AttachmentContent
    Size           INTEGER     Required
    ContentType    STRING      Required


    Artifact       ref to Artifact            (optional field)

    Description    TEXT        Optional

"""

#################################################################################################

def main(args):
    options = [opt for opt in args if opt.startswith('--')]
    args    = [arg for arg in args if arg not in options]
    server = "rally1.rallydev.com"
    user = "user@company.com"
    password = "topsecret"
    workspace = "My Workspace"
    project = "My Project"
    print " ".join(["|%s|" % item for item in [server, user, '********', workspace, project]])
    rally = Rally(server, user, password, workspace=workspace, version="1.43")  # specify the Rally server and credentials
    rally.enableLogging('rally.hist.create_tcr_and_attachment') # name of file you want logging to go to

    if len(args) != 2:
        errout('ERROR: You must supply a Test Case FormattedID and an attachment file name')
        errout(USAGE)
        sys.exit(1)

    targetTCID, filename = args

    me = rally.getUserInfo(username=user).pop(0)
    print "%s user oid: %s" % (user, me.oid)

    target_project = rally.getProject()
    target_tc  = rally.get('TestCase', query='FormattedID = %s' % targetTCID, instance=True)    

    datestring = "2014-05-01"

    tcr_info = {
         "TestCase"     : target_tc.ref,
         "Build"        : "master-91321",
         "Date"         : datestring,
         "Verdict"      : "Pass",
         "Notes"        : "Honeycomb harvest project."
       }

    print "Creating Test Case Result ..."
    tcr = rally.put('TestCaseResult', tcr_info)
    print "Created  TCR: %s" % (tcr.oid)

    print "Creating AttachmentContent"

    if not os.path.exists(filename):
        raise Exception('Named attachment filename: %s not found' % filename)
    if not os.path.isfile(filename):
        raise Exception('Named attachment filename: %s is not a regular file' % filename)

    attachment_file_name = os.path.basename(filename)
    attachment_file_size = os.path.getsize(filename)
    if attachment_file_size > 5000000:
        raise Exception('Attachment file size too large, unable to attach to Rally Artifact')

    contents = ''
    with open(filename, 'r') as af:
        contents = base64.encodestring(af.read())

    # create an AttachmentContent item
    ac = rally.create('AttachmentContent', {"Content" : contents}, project=None)
    if not ac:
        raise RallyRESTAPIError('Unable to create AttachmentContent for %s' % attachment_file_name)

    attachment_info = { "Name"              :  attachment_file_name,
                        "Content"           :  ac.ref,       # ref to AttachmentContent
                        "ContentType"       :  "image/jpeg",    
                        "Size"              :  attachment_file_size, # must be size before encoding!!
                        "User"              :  'user/%s' % me.oid,
                        "TestCaseResult"    :  tcr.ref
                      }

    # and finally, create the Attachment
    attachment = rally.create('Attachment', attachment_info, project=None)
    if not attachment:
        raise RallyRESTAPIError('Unable to create Attachment for %s' % attachment_file_name)    


#################################################################################################
#################################################################################################

if __name__ == '__main__':
    main(sys.argv[1:])
票数 3
EN

Stack Overflow用户

发布于 2016-06-24 04:50:37

已更新拉力API中的函数拉力API以支持测试用例结果:

代码语言:javascript
复制
# While it's actually possible to have an Attachment not linked to an Artifact,
# in most cases, it'll be far more useful to have the linkage to an Artifact than not.
# A special case is where the "Artifact" is actually a TestCaseResult, which is not a
# subclass of Artifact in the Rally data model, but the WSAPI has been adjusted to permit
# us to associate an Attachment with a TestCaseResult instance.
if artifact:  
    attachment_info["Artifact"] = artifact.ref
    if artifact._type == 'TestCaseResult':
        del attachment_info["Artifact"]
        attachment_info["TestCaseResult"] = artifact.ref

函数调用rally.addAttachment(testCaseResult.oid, file);现在应该可以工作了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23414094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档