You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Java Selenium自动化测试结果更新至Azure DevOps触发的测试运行?

解决Azure DevOps中Java Selenium测试结果回传至原有Test Run ID的问题

要将Selenium测试结果更新到触发测试的原有VS Test Run ID中,核心是复用已有Run ID而非创建新的,具体步骤如下:

1. 从管道中获取原有Test Run ID

Azure DevOps构建/发布管道中,系统变量$(Test.RunId)已包含当前触发的VS Test Run的ID。你需要将这个变量传递给Java测试脚本:

  • 如果用Maven执行测试,在管道的Maven任务中添加参数:-Dtest.run.id=$(Test.RunId)
  • 如果是自定义脚本执行,可通过环境变量传递,比如设置环境变量TEST_RUN_ID=$(Test.RunId),再在Java代码中读取System.getenv("TEST_RUN_ID")

2. 调用Azure DevOps REST API更新指定Run的测试结果

不要依赖默认的结果上传逻辑(默认会创建新Run),直接调用REST API更新已有Run的结果:

  • 先创建Azure DevOps个人访问令牌(PAT),需赋予Test Management的读写权限
  • 核心Java代码示例:
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class TestResultUpdater {
    public static void pushResultToExistingRun(String orgUrl, String projectName, String runId, String testCaseId, String outcome) {
        try {
            String patToken = "你的PAT令牌";
            String authHeader = "Basic " + Base64.getEncoder().encodeToString((":" + patToken).getBytes());

            // 构造API请求地址
            URL apiUrl = new URL(String.format("%s/%s/_apis/test/runs/%s/results?api-version=7.1-preview.6", orgUrl, projectName, runId));
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", authHeader);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            // 构造测试结果JSON,支持批量更新多个用例
            String payload = String.format(
                "[{\"testCase\":{\"id\":\"%s\"},\"outcome\":\"%s\",\"state\":\"Completed\"}]",
                testCaseId, outcome
            );

            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = payload.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            int responseCode = connection.getResponseCode();
            if (responseCode == 200 || responseCode == 201) {
                System.out.println("测试结果已成功更新到指定Run");
            } else {
                System.err.println("更新失败,响应码:" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 在测试结束后调用
    public static void main(String[] args) {
        String orgUrl = "https://dev.azure.com/你的组织名";
        String projectName = "你的项目名";
        String runId = System.getProperty("test.run.id"); // 从参数读取Run ID
        String testCaseId = "你的测试用例ID";
        String outcome = "Passed"; // 或Failed/NotExecuted等

        pushResultToExistingRun(orgUrl, projectName, runId, testCaseId, outcome);
    }
}

3. 管道配置注意事项

  • 如果使用Azure DevOps自带的Visual Studio Test任务,确保未勾选“创建新测试运行”选项(默认会复用传入的Run ID)
  • 确保测试任务的执行上下文能正确获取到$(Test.RunId)变量,避免因变量未传递导致脚本无法定位原有Run

4. 验证结果

执行管道后,进入Azure DevOps的测试>测试运行页面,找到对应的Run ID,检查测试结果是否更新到该运行下,而非生成新的测试运行。

内容的提问来源于stack exchange,提问作者ClinOne Test2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:34:51