因此,根据我在问题中链接的示例/示例回答我自己的问题:
您需要使用TestManagementHttpClient.GetTestConfigurationsAsync()获取您想要的测试配置
然后你会想要使用TestManagementHttpClient.GetPointsAsync()获取该测试用例/测试配置组合的所有测试点
然后您需要创建一个测试运行。这是通过至少声明一个新的RunCreateModel 对象来完成的,指定您之前获取的测试点ID。您可能还需要填写大量参数(buildId、isAutomated 等)。然后您需要调用TestManagementHttpClient.CreateTestRunAsync() 来实际创建它。
步骤 3 实际上在测试运行下创建了空的测试结果,对于您在创建时指定的每个测试点。您需要使用TestManagementHttpClient.GetTestResultsAsync() 获取它们并修改它们的Outcome 属性,使用TestCaseResult.TestCase.Id 属性来了解哪个结果适用于哪个测试用例。您可能还想填写其他属性,例如 State 等。同样,您需要使用 TestManagementHttpClient.UpdateTestResultsAsync() 将这些修改推送到 TFS
最后一步是通过使用state = "Completed" 创建一个RunUpdateModel 对象,然后调用TestManagementHttpClient.UpdateTestRunAsync(),将您的测试运行设置为已完成
这是我最终编写的用 F# 编写的函数:
// A test point is a pairing of a test case with a test configuration
let createTestRun (httpClient:TestManagementHttpClient) (testRunName:string) (teamProjectName:string) (testPlanId:int) (testSuiteId:int) (testCaseIdsAndResults:seq<(int * string)>) (buildId:int) (cancellationToken:CancellationToken) = async {
let testPlanIdString = testPlanId.ToString()
let plan = new ShallowReference(testPlanIdString)
let! testConfigurations = httpClient.GetTestConfigurationsAsync(teamProjectName, cancellationToken = cancellationToken) |> Async.AwaitTask
let defaultTestConfiguration = testConfigurations |> Seq.find (fun c -> c.IsDefault) // TODO: We only use the default configuration for now. Do we always want this?
let rec getTestPoints (testIdsAndResults:(int * string) list) (testPoints:TestPoint[]) = async {
match testIdsAndResults with
| (testId, _)::rest ->
let! fetchedTestPoints = httpClient.GetPointsAsync(teamProjectName, testPlanId, testSuiteId, testCaseId = testId.ToString(), cancellationToken = cancellationToken) |> Async.AwaitTask
let testPoint = fetchedTestPoints |> Seq.find (fun p -> p.Configuration.Id = defaultTestConfiguration.Id.ToString())
let newTestPointsList = Array.append testPoints [|testPoint|]
return! getTestPoints rest newTestPointsList
| _ ->
return testPoints
}
let! testPoints = getTestPoints (List.ofSeq testCaseIdsAndResults) Array.empty
let testPointIds = testPoints |> Array.map (fun p -> p.Id)
let runCreateModel = new RunCreateModel(name = testRunName, plan = plan, buildId = buildId, isAutomated = new Nullable<bool>(true), pointIds = testPointIds)
let! testRun = httpClient.CreateTestRunAsync(runCreateModel, teamProjectName, cancellationToken = cancellationToken) |> Async.AwaitTask
let! emptyResults = httpClient.GetTestResultsAsync(project = teamProjectName, runId = testRun.Id, outcomes = new List<TestOutcome>(), cancellationToken = cancellationToken) |> Async.AwaitTask
let rec createCaseResults (testIdsAndResults:(int * string) list) (results:TestCaseResult[]) = async {
match testIdsAndResults with
| (testId, testResult)::rest ->
let caseResult = emptyResults |> Seq.find (fun r -> r.TestCase.Id = testId.ToString())
caseResult.State <- "Completed"
caseResult.Outcome <- testResult // "passed", "failed", "never run", "not applicable"
let newResultsList = Array.append results [|caseResult|]
return! createCaseResults rest newResultsList
| _ ->
return results
}
let! results = createCaseResults (List.ofSeq testCaseIdsAndResults) Array.empty
let! _ = httpClient.UpdateTestResultsAsync(results, teamProjectName, testRun.Id, cancellationToken = cancellationToken) |> Async.AwaitTask
let runmodel = new RunUpdateModel(state = "Completed");
let! _ = httpClient.UpdateTestRunAsync(runmodel, teamProjectName, testRun.Id, cancellationToken = cancellationToken) |> Async.AwaitTask
()
}