Skip to main content
 首页 » 编程设计

python之步骤的附件存储在 VSTS REST API 中的什么位置

2024年12月31日6softidea

我正在使用 VSTS 的 Python REST API for TFS/Azure Dev Ops (https://github.com/Microsoft/azure-devops-python-api)。

我想在我的测试用例的某些步骤中添加附件,就像我在 Web 界面中所做的那样。

这就是我希望我的步骤看起来像的样子:

...当你运行它时,它看起来像这样:

但是,我没能找到这些信息的存储位置。

这是我的测试用例的 WorkItem 的 JSON 数据

{ 
id: 224, 
rev: 2, 
fields: { 
    System.AreaPath: "GM_sandbox\GM-Toto", 
    System.TeamProject: "GM_sandbox", 
    System.IterationPath: "GM_sandbox", 
    System.WorkItemType: "Test Case", 
    System.State: "Design", 
    System.Reason: "New", 
    System.AssignedTo: "Jeff", 
    System.CreatedDate: "2019-01-03T01:43:09.743Z", 
    System.CreatedBy: "Jeff", 
    System.ChangedDate: "2019-01-03T02:12:07.15Z", 
    System.ChangedBy: "Jeff", 
    System.Title: "Titi", 
    Microsoft.VSTS.Common.StateChangeDate: "2019-01-03T01:43:09.743Z", 
    Microsoft.VSTS.Common.ActivatedDate: "2019-01-03T01:43:09.743Z", 
    Microsoft.VSTS.Common.ActivatedBy: "Jeff", 
    Microsoft.VSTS.Common.Priority: 2, 
    Microsoft.VSTS.TCM.AutomationStatus: "Not Automated", 
    Microsoft.VSTS.TCM.Steps: "<steps id="0" last="2"><step id="2" type="ValidateStep"><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Click on the rainbow button&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted="true">&lt;P&gt;Screen becomes Blue (see picture)&lt;/P&gt;</parameterizedString><description/></step></steps>" 
}, 
_links: { 
    self: { 
        href: "https://my_server.com:8443/tfs/PRODUCT/23d89bd4-8547-4be3-aa73-13a30866f176/_apis/wit/workItems/224" 
    }, 
    workItemUpdates: { 
        href: "https://my_server.com:8443/tfs/PRODUCT/_apis/wit/workItems/224/updates" 
    }, 
    workItemRevisions: { 
        href: "https://my_server.com:8443/tfs/PRODUCT/_apis/wit/workItems/224/revisions" 
    }, 
    workItemHistory: { 
        href: "https://my_server.com:8443/tfs/PRODUCT/_apis/wit/workItems/224/history" 
    }, 
    html: { 
        href: "https://my_server.com:8443/tfs/PRODUCTi.aspx?pcguid=4107d6a2-eaaa-40b9-9a8d-f8fdbb31d4b7&id=224" 
    }, 
    workItemType: { 
        href: "https://my_server.com:8443/tfs/PRODUCT/23d89bd4-8547-4be3-aa73-13a30866f176/_apis/wit/workItemTypes/Test%20Case" 
    }, 
    fields: { 
        href: "https://my_server.com:8443/tfs/PRODUCT/23d89bd4-8547-4be3-aa73-13a30866f176/_apis/wit/fields" 
    } 
}, 
url: "https://my_server.com:8443/tfs/PRODUCT/23d89bd4-8547-4be3-aa73-13a30866f176/_apis/wit/workItems/224" 
} 

知道这些信息存储在哪里吗?

而且,如果您熟悉 Python REST API,如何从文件添加附件并将其链接到测试步骤?

非常感谢

请您参考如下方法:

这是仅使用 azure-devops-rest-api 的流程

Create the attachment:

请求:

POST https://dev.azure.com/{organization}/_apis/wit/attachments?fileName=info.txt&api-version=4.1 

正文:

{"User text content to upload"} 

响应:

{ 
    "id": "f5016cf4-4c36-4bd6-9762-b6ad60838cf7", 
    "url": "https://dev.azure.com/{organization}/_apis/wit/attachments/f5016cf4-4c36-4bd6-9762-b6ad60838cf7?fileName=info.txt" 
} 

Create the Work Item:

请求:

PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Test Case?api-version=4.1 

正文:

[ 
  { 
    "op": "add", 
    "path": "/fields/System.Title", 
    "from": null, 
    "value": "Sample test case" 
  }, 
  { 
    "op": "add", 
    "path": "/fields/Microsoft.VSTS.TCM.Steps", 
    "value": "<steps id=\"0\" last=\"4\"><step id=\"2\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;test&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step><step id=\"3\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;test&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step><step id=\"4\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;test&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step></steps>" 
  }, 
  { 
    "op": "add", 
    "path": "/relations/-", 
    "value": { 
        "rel": "AttachedFile", 
        "url": "https://dev.azure.com/{organization}/_apis/wit/attachments/f5016cf4-4c36-4bd6-9762-b6ad60838cf7?fileName=info.txt", 
        "attributes": { 
            "comment": "[TestStep=3]:", 
            "name": "info.txt" 
        } 
    } 
  } 
] 

创建的测试用例如下所示。评论中数字的步骤编号有问题。对于您要引用的实际步骤,它看起来需要 +1。

关键是要在附件的属性中包含注释 "[TestStep=3]:" 以及名称 用于附件。


在 Python 中,这将给出如下内容:

  1. 使用函数 create_attachment

    创建附件
  2. 使用 url、comment 和 filename

    更新测试用例

那么类似的东西......

from vsts.work_item_tracking.v4_1.models.json_patch_operation import JsonPatchOperation 
 
def add_attachment(wit_id: int, project: str, url:str, comment: str, step = 0, name = ""): 
    """Add attachment already uploaded to a WorkItem 
    """ 
    # For linking the attachment to a step, we need to modify the comment and add a name 
    if step: 
        attributes = { 
            "comment":f"[TestStep={step}]:{comment}", 
            "name": name if name else re.sub(r".*fileName=", "", url) 
        } 
    else: 
        attributes = {"comment": comment} 
 
    patch_document = [ 
        JsonPatchOperation( 
            op="add", 
            path="/relations/-", 
            value={ 
                "rel": "AttachedFile", 
                "url": url, 
                "attributes": attributes, 
            }, 
        ) 
    ] 
    return client.wit.update_work_item(patch_document, wit_id, project) 
 
attachment = client_wit.create_attachment(stream, project, 'smiley.png') 
add_attachment(tcid, project, attachment.url, 'Attaching file to work item', step=3)