首页 > 解决方案 > 使用 Pytest/Python 在 Azure DevOps 中创建工作项时出错

问题描述

我正在尝试使用 Python 和requests库创建一个工作项。

def test_create_work_item(work_items):
    payload = {
                'op': 'add',
                'path': '/fields/System.Title',
                'value': 'Sample bug'
            }
    pl = json.dumps(payload)
    work_item = work_items.create(body=pl, type='bug')
    assert work_item.status_code == 200

我收到以下错误:

{"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.","typeName":"Microsoft.VisualStudio.Services.Common.VssPropertyValidationException,Microsoft.VisualStudio.Services.Common","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}

Postman 可以使用相同的主体。所以不确定这里还需要什么才能让它工作。

标签: pythonexceptionazure-devopspytestworkitem

解决方案


我对 Python 不熟悉....查看此示例:创建工作项

API 使用一组新字段:

[   
    {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task"
    } 
]

在您的情况下,您在请求中只使用一个字段:

{
    'op': 'add',
    'path': '/fields/System.Title',
    'value': 'Sample bug'
}

推荐阅读