通过c#将TFS测试运行链接到一个版本



使用新的TFS api:微软TeamFoundationServer。扩展客户端我已经成功地创建了一个测试运行,并为其附加了一个发布和发布环境,因此在测试运行摘要中会出现一个指向该发布的链接。但是,当我转到发布页面并单击"测试"选项卡时,我看不到测试运行及其统计信息。我如何"让"发行版知道测试运行,并通过c#代码将其添加到发行版中?

这是我的代码:

// Creates a TFS test run 
public static void CreateTestRun(ITestPlan testPlan, int testCaseId, string testResult, 
string buildIdStr, string releaseUri, string releaseEnvironmentUri, string testRunName)
{
// --------------------------------Biuld the RunCreateModel for the test run:------------------------------------------------
// Find the test points of the current test case
List<int> testPointIds = new List<int>();
ITestPointCollection testPoints = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE testPoint.TestCaseId='" + testCaseId + "'");
foreach (ITestPoint testPoint in testPoints)
{
testPointIds.Add(testPoint.Id);
}
int buildId;
int.TryParse(buildIdStr, out buildId);
// Init RunCreateModel:
RunCreateModel runCreateModel = new RunCreateModel(
name: testRunName,
startedDate: DateTime.Now.ToString("M/d/y h:m:s tt"),
plan: new ShallowReference(id: testPlan.Id.ToString()),
pointIds: testPointIds.ToArray(),
buildId: buildId,
releaseUri: releaseUri,
releaseEnvironmentUri: releaseEnvironmentUri
);
// ----------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------Create test run in progress--------------------------------------------
TestManagementHttpClient testManagementClient =
new TestManagementHttpClient(new Uri(TFS_COLLECTION_NAME), new VssCredentials());
// Use RunCreateModel to create a test run on TFS (using the extended API):
TestRun testRunExtended =
testManagementClient.CreateTestRunAsync(runCreateModel, TFS_TEAM_PROJECT_NAME).Result;
// ---------------------------------------------------------------------------------------------------------------------------
// Using the regular client api, add results to the test run to complete it:
TfsTeamProjectCollection tfsCollection = new TfsTeamProjectCollection(new Uri(TFS_COLLECTION_NAME), new VssCredentials());
ITestManagementService testManagement = tfsCollection.GetService<ITestManagementService>();
IEnumerable<ITestRun> testRuns = testManagement.QueryTestRuns(
"SELECT * FROM TestRun WHERE TestRunID='" + testRunExtended.Id + "'");
ITestRun testRun = testRuns.First();
// Update the outcome of the test       
ITestCaseResultCollection results = testRun.QueryResults();
foreach (ITestCaseResult result in results)
{
result.Outcome = testResult == "Pass" ?
Microsoft.TeamFoundation.TestManagement.Client.TestOutcome.Passed :
Microsoft.TeamFoundation.TestManagement.Client.TestOutcome.Failed;
result.State = TestResultState.Completed;
result.Save();
}
testRun.Save();
testRun.Refresh();
}

Thx。

通过将RunCreateModel.isAutomated属性设置为true来求解。

您可以使用REST API根据其ID更新测试运行,API如下:

PATCH https://{accountName}.visualstudio.com/{project}/_apis/test/runs/{runId}?api-version=5.0-preview.2

您需要在正文中修改或添加以下部分:

"releaseUri": "vstfs:///ReleaseManagement/Release/{releaseID}",
"releaseEnvironmentUri": "vstfs:///ReleaseManagement/Environment/{releaseID}",
"release": {
"id": {releaseID},
"name": "{releaseName}",
"environmentId": {releaseID},
"environmentName": "{EnvironmentName}",
"definitionId": {definitionId},
"environmentDefinitionId": {definitionId},
"environmentDefinitionName": null,
"attempt": 1
},

我已经测试过了,它正在发挥作用。

最新更新