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

2024-05-13 13:22:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我希望我的步骤看起来像:

Step with attachment

。。。当你运行它时,应该是这样的: enter image description here

但是,我一直没能找到这些信息存储在哪里。在

这是我的测试用例的工作项的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"
}

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

而且,如果您熟悉pythonrestapi,那么如何从文件中添加附件并将其链接到测试步骤?在

非常感谢


Tags: httpsltgtcomservermyproductsystem
1条回答
网友
1楼 · 发布于 2024-05-13 13:22:04

下面是只使用azuredevopsrestapi的流程

Create the attachment:

请求:

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

正文:

^{pr2}$

回应:

{
    "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. {cd6>}

所以类似的事情。。。在

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)

相关问题 更多 >