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

使用Azure DevOps API创建测试用例结果时详情字段填充问题

Azure DevOps测试结果详情(执行步骤)填充方案

我通过Azure DevOps的POST https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results?api-version=7.1 API创建测试用例结果,已经成功生成结果,但无法填充详情部分的执行步骤(创建Bug时会显示这部分内容)。目前仅填入了堆栈跟踪和错误消息,代码如下:

listOf(
    TestCaseResultRequestBody(
                    automatedTestName = testCaseResult.title,
                    testRun = ShallowReferenceString(testRunId),
                    durationInMs = testCaseResult.testCaseResult.duration.toMillis(),
                    state = "Completed",
                    outcome = testCaseResult.status.lowercase().capitalize(),
                    testCase = ShallowReference(testCaseResult.workItemMeta.Id.toInt(), name = testCaseResult.annotatedTitle),
                    testPlan = ShallowReferenceString(testPlanId),
                    testPoint = ShallowReferenceString(testPointId),
                    testCaseRevision = testCaseResult.workItemMeta.Revision.toInt(),
                    testCaseTitle = testCaseResult.annotatedTitle,
                    comment = "test",
                    stackTrace =  testCaseResult.stackTrace,
                    errorMessage = if (testCaseResult.failureMessage.isEmpty()) {
                        null
                    } else {
                        val failure = testCaseResult.steps.map {
                            "${it.status.name}: "+it.stepDescription
                        }.joinToString("\n")
                        failure
                    }
                )
            }
    )
            val jsonPayload =  JsonConfig.json.encodeToString(ListSerializer(TestCaseResultRequestBody.serializer()), testCaseResults)
    
            val client = HttpClient.newHttpClient()
            val request = httpRequestBuilder("$AZURE_BASE_URL/test/runs/$testRunId/results?api-version=7.1")

                .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
                .build()
    
            val response = client.send(request, HttpResponse.BodyHandlers.ofString())

解决方案

要填充详情里的执行步骤,需要在TestCaseResultRequestBody中添加subResults字段,对应API的测试子结果数组。每个测试步骤对应一个TestSubResult对象,包含步骤描述、状态、结果等信息,这样创建Bug时就能自动带出这些步骤详情。

修改后的代码示例:

listOf(
    TestCaseResultRequestBody(
        automatedTestName = testCaseResult.title,
        testRun = ShallowReferenceString(testRunId),
        durationInMs = testCaseResult.testCaseResult.duration.toMillis(),
        state = "Completed",
        outcome = testCaseResult.status.lowercase().capitalize(),
        testCase = ShallowReference(testCaseResult.workItemMeta.Id.toInt(), name = testCaseResult.annotatedTitle),
        testPlan = ShallowReferenceString(testPlanId),
        testPoint = ShallowReferenceString(testPointId),
        testCaseRevision = testCaseResult.workItemMeta.Revision.toInt(),
        testCaseTitle = testCaseResult.annotatedTitle,
        comment = "test",
        stackTrace = testCaseResult.stackTrace,
        errorMessage = if (testCaseResult.failureMessage.isEmpty()) {
            null
        } else {
            testCaseResult.steps.joinToString("\n") { "${it.status.name}: ${it.stepDescription}" }
        },
        // 添加测试步骤子结果,填充详情部分
        subResults = testCaseResult.steps.map { step ->
            TestSubResult(
                description = step.stepDescription,
                outcome = step.status.name.lowercase().capitalize(),
                state = "Completed",
                // 按需补充预期/实际结果字段
                expectedResult = step.expectedResult,
                actualResult = step.actualResult
            )
        }
    )
)

val jsonPayload = JsonConfig.json.encodeToString(ListSerializer(TestCaseResultRequestBody.serializer()), testCaseResults)
val client = HttpClient.newHttpClient()
val request = httpRequestBuilder("$AZURE_BASE_URL/test/runs/$testRunId/results?api-version=7.1")
    .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
    .build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())

关键说明

  • subResults数组中的每个TestSubResult对应详情里的一条执行步骤
  • 确保TestSubResult类的字段与API结构匹配(核心字段:description、outcome、state)
  • 补充expectedResult、actualResult等字段可让步骤详情更完整,这些内容会同步到Bug中

内容的提问来源于stack exchange,提问作者Rashad.Z

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 01:22:45