步骤的附件存储在VSTS REST API中的何处?

时间:2019-01-03 02:23:41

标签: python azure-devops azure-devops-rest-api

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

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

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

Step with attachment

...,当您运行它时,将如下所示: enter image description here

但是,我无法找到此信息的存储位置。

这是我的测试用例的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,如何从文件添加附件并将其链接到测试步骤?

非常感谢

1 个答案:

答案 0 :(得分:1)

这里是仅使用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。

TFS TestCase

关键是要在附件的属性中添加带有"[TestStep=3]:"注释和附件的name


Python 中,会给出类似以下内容:

  1. 使用功能create_attachment

  2. 创建附件
  3. 使用urlcommentfilename

  4. 更新测试用例

类似的东西...

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)