TFS 2018 API(4.1 版)- Python:无法在 运行 使用 4.1 或 5.1 版的结果中添加附件

TFS 2018 API (4.1 version) - Python : Can't add an attachment in a result of a run using either 4.1 or 5.1 version

我已通过创建 运行 并向其添加结果,成功地将我的测试结果从我的自动化 python 脚本发送到 TFS 2018。

现在,我正在尝试上传我使用 Create Test Result Attachment API call 生成的包含测试执行详细结果的 xml 文件。据我所知,我们 API 的版本是 4.1 而不是 5.1。

这是我为了实现我想要的而写的方法:

def add_result_attachment(result_id, run_id, filename, stream, conf_object):    

    url = conf_object.config_map('TFS')['url'] + "TFS/_apis/test/Runs/" + str(run_id) + "/Results/" + str(result_id) + "/attachments"

    payload = "{\n    \"attachmentType\": \"GeneralAttachment\",\n    \"fileName\": \"" + filename + "\",\n    \"stream\": \"" + str(stream) + "\",\n    \"comment\": \"Test\"\n}"

    # specify version of tfs and its rest api
    query_string = {"api-version": "5.1-preview"}

    headers = {
        'Content-Type': "application/json",
        'Authorization': MYKEY
    }

    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    response = requests.request("POST", url, data=payload, headers=headers, params=query_string,  verify=False)

    print(str(response.status_code) + "\n" + json.dumps(json.loads(response.text), indent=4) + '\n')

我检查过 URL 给我正确的 ID,所以它是有效的。 filename 包含我使用 pytest 命令生成的 xml 文件的绝对路径。

stream 包含此 xml 文件的 base64 编码形式的字符串。

当我执行这个时,我得到了响应:

Traceback (most recent call last):
  File "C:\Users\marialena\source\repos\UI-Tester\test_signin.py", line 210, in tearDown
    tfs_api.tfs_api.tfs_process(run_start_time, run_completed_time, class_name, method_name, steps, self.outcome)
  File "C:\Users\marialena\source\repos\UI-Tester\tfs_api.py", line 263, in tfs_process
    tfs_api.send_run_results(run_start_time, run_completed_time, json_test_cases['id'], test_name, outcome)
  File "C:\Users\marialena\source\repos\UI-Tester\tfs_api.py", line 586, in send_run_results
    tfs_api.add_result_attachment(result_id, run_id, filename, stream, conf_object)
  File "C:\Users\marialena\source\repos\UI-Tester\tfs_api.py", line 457, in add_result_attachment
    print(str(response.status_code) + "\n" + json.dumps(json.loads(response.text), indent=4) + '\n')
  File "C:\Users\marialena\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\marialena\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\marialena\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)

如果我将 5.1-preview 换成 4.1-preview 版本,我会得到状态代码 400 和以下响应:

{
    "$id": "1",
    "innerException": null,
    "message": "Value cannot be null.\r\nParameter name: attachmentRequestModel",
    "typeName": "System.ArgumentNullException, mscorlib",
    "typeKey": "ArgumentNullException",
    "errorCode": 0,
    "eventId": 0
}

如果此调用支持 4.1 版本或以某种方式使用 5.1,即使我们没有最新的 TFS,是否有办法解决该问题?我也尝试将附件发送到 运行 本身而不是它的结果,但也没有运气。

如有任何帮助,我们将不胜感激。谢谢!

如果有人仍然感兴趣,有人已经回答了我的问题here

基本上需要更改的是:

  • 在项目的根目录上使用更简单的路径 => TestsOut.log
  • 以不同的方式转换 stream => stream = str(base64.b64encode(data), 'utf-8')