【问题标题】:How to add test steps in a test case work item using Rest API - TFS2018 / Python如何使用 Rest API 在测试用例工作项中添加测试步骤 - TFS2018 / Python
【发布时间】:2019-01-09 18:02:07
【问题描述】:

我正在使用 Microsoft 的 TFS 2018,并且我已经开始在 Visual Studio 2018 中使用 Python 3.7 编写一些 Selenium 测试用例。

我已经设法使用 TFS 的 REST API 返回我的 TFS 项目并创建新的测试用例。

我找不到的是如何使用此 API 传递包含此测试用例的所有测试步骤的列表。我不确定如何以及是否可以将它们作为字符串或数组添加到请求正文中。

目前我正在尝试先在 Postman 上进行这项工作,然后我也将在 python 中尝试。

这是请求:

curl -X POST \
  'https://TFSLINK:443/DefaultCollection/TFS/_apis/wit/workitems/$Test%20Case?api-version=4.1' \
  -H 'Authorization: Basic MYKEY' \
  -H 'Content-Type: application/json-patch+json' \
  -H 'cache-control: no-cache' \
  -d '[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task 2"
  }
]'

有没有办法实现添加步骤? API 没有提及此事。

在创建测试用例后得到的响应中,我得到一个名为“字段”的部分,其中应该包含这些步骤,但我在响应中看不到它们。

{
    "id": 731,
    "rev": 1,
    "fields": {
        "System.AreaPath": "TFS",
        "System.TeamProject": "TFS",
        "System.IterationPath": "TFS",
        "System.WorkItemType": "Test Case",
        "System.State": "Design",
        "System.Reason": "New",
        "System.AssignedTo": "Marialena <TFS\\marialena>",
        "System.CreatedDate": "2019-01-09T08:00:50.51Z",
        "System.CreatedBy": "Marialena <TFS\\marialena>",
        "System.ChangedDate": "2019-01-09T08:00:50.51Z",
        "System.ChangedBy": "Marialena <TFS\\marialena>",
        "System.Title": "Sample task 2",
        "Microsoft.VSTS.Common.StateChangeDate": "2019-01-09T08:00:50.51Z",
        "Microsoft.VSTS.Common.ActivatedDate": "2019-01-09T08:00:50.51Z",
        "Microsoft.VSTS.Common.ActivatedBy": "Marialena <TFS\\marialena>",
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.TCM.AutomationStatus": "Not Automated"
    },
    "_links": {
        "self": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731"
        },
        "workItemUpdates": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731/updates"
        },
        "workItemRevisions": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731/revisions"
        },
        "workItemHistory": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731/history"
        },
        "html": {
            "href": "https://TFSLINK/web/wi.aspx?pcguid=07b658c4-97e5-416f-b32d-3dd48d7f56cc&id=731"
        },
        "workItemType": {
            "href": "https://TFSLINK/DefaultCollection/18ca0a74-cf78-45bf-b163-d8dd4345b418/_apis/wit/workItemTypes/Test%20Case"
        },
        "fields": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/fields"
        }
    },
    "url": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731"
}

我已尝试创建此 PATCH 请求以更新步骤,但没有成功

curl -X PATCH \
  'https://TFSLINK:443/DefaultCollection/TFS/_apis/wit/workItems/730?api-version=4.1' \
  -H 'Authorization: Basic MYKEY' \
  -H 'Content-Type: application/json-patch+json' 
  -d '[
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.TCM.Steps",
    "from": null,
    "value": "Test"
  },
  {
    "op": "add",
    "path": "/fields/Steps",
    "from": null,
    "value": "Test"
  }
]'

也许这是另一个话题,但如果上述是可以实现的,你是否也可以在运行测试并更新测试计划后通过结果?如果这无关,请仅帮助我完成测试步骤并忽略此问题。

非常感谢。

【问题讨论】:

    标签: api testing tfs request postman


    【解决方案1】:

    这是在 Test Case with Rest API 中添加测试步骤的方法:

    { 
        "op": "add", 
        "path": "/fields/Microsoft.VSTS.TCM.Steps",
        "value": "<steps id=\"0\" last=\"1\"><step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 1</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 1</parameterizedString><description/></step></steps>"  
    } 
    

    几个步骤(本例中为 3 个):

    { 
        "op": "add", 
        "path": "/fields/Microsoft.VSTS.TCM.Steps",
        "value": "<steps id=\"0\" last=\"4\"><step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\"><P>step 1 \"Action\"</P></parameterizedString><parameterizedString isformatted=\"true\"><P>step 1 \"Expected\"<BR/></P></parameterizedString><description/></step><step id=\"3\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\"><P>step 2 \"Action\"<BR/></P></parameterizedString><parameterizedString isformatted=\"true\"><P>step 2 \"Expected\"<BR/></P></parameterizedString><description/></step><step id=\"4\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\"><P>step 3 \"Action\"<BR/></P></parameterizedString><parameterizedString isformatted=\"true\"><P>step 3 \"Expected\"<BR/></P></parameterizedString><description/></step></steps>"  
    } 
    

    【讨论】:

    • 这是有效的。非常感谢。只是为了确定您能否粘贴两个步骤的示例,以便我了解每行需要发送的内容(步骤 ID、步骤、预期)。类型也可以是别的东西还是总是“ValidateStep”?
    • 我更新了几个步骤的答案。关于“ValidateStep”我真的不知道......
    • 非常感谢.. 看来您在第一步中也不需要last。我删除了它,这有效:"&lt;steps id=\"0\"&gt; &lt;step id=\"2\" type=\"ValidateStep\"&gt;&lt;parameterizedString isformatted=\"true\"&gt;Input step 1&lt;/parameterizedString&gt;&lt;parameterizedString isformatted=\"true\"&gt;Expectation step 1&lt;/parameterizedString&gt;&lt;description/&gt;&lt;/step&gt; &lt;step id=\"3\" type=\"ValidateStep\"&gt;&lt;parameterizedString isformatted=\"true\"&gt;Input step 2&lt;/parameterizedString&gt;&lt;parameterizedString isformatted=\"true\"&gt;Expectation step 2&lt;/parameterizedString&gt;&lt;description/&gt;&lt;/step&gt;&lt;/steps&gt;"
    • 有点不相关,但您是否设法将测试结果也发送到 tfs?我会问一个不同的问题,因为这已经解决了,但我想知道你是否可以使用 API 完成整个周期
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多