如何使用 Rest API 以编程方式将测试结果添加到 VSTS 中的测试 运行
How to Add test results to a test run in VSTS using Rest API programatically
我有 API link 的任务
POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}
https://www.visualstudio.com/en-us/docs/integrate/api/test/results#add-test-results-to-a-test-run
我需要程序通过API方式完成。
替代方案,我已尝试使用以下程序
public void GetResult()
{
var u = new Uri("https://{UserAccount}.visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "MyPAT"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
int testpointid = 100;
string teamProject = "Project12345";
RunCreateModel run = new RunCreateModel(name: "APIRun7", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100"), pointIds: new int[] { testpointid });
TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;
TestCaseResultUpdateModel testCaseUpdate = new TestCaseResultUpdateModel() { State = "Completed", Outcome = "Passed", TestResult = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100000") };
//var testResults = testClient.UpdateTestResultsAsync(new TestCaseResultUpdateModel[] { testCaseUpdate }, teamProject, testrun.Id).Result;
RunUpdateModel runmodel = new RunUpdateModel(state: "Completed");
TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel ,teamProject, testrun.Id, runmodel).Result;
}
我收到错误:
cannot convert from 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResultUpdateModel[]'
to 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResult[]'
参考这些步骤:
:
var u = new Uri("https://[account].visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
string teamProject = "scrum2015";
int testRunId = 286;
int testSuiteId = 591;
RunUpdateModel runmodelUpdate = new RunUpdateModel(state: "InProgress");
TestRun testRunUpdateResult = testClient.UpdateTestRunAsync(teamProject, testRunId, runmodelUpdate).Result;
var results = testClient.GetTestResultsAsync(teamProject, testRunId).Result.First();
var testPoints= testClient.GetPointsAsync(teamProject,Int32.Parse(testRunUpdateResult.Plan.Id), testSuiteId).Result;
TestRun testRunUpdate = testClient.GetTestRunByIdAsync(teamProject, testRunId).Result;
TestResultCreateModel newTestResult = new TestResultCreateModel() { TestCase=new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(id: results.TestCase.Id.ToString()), TestPoint = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(id: testPoints.First().Id.ToString()), Outcome="Failed",State= "Completed" };
var updateResult= testClient.AddTestResultsToTestRunAsync(new TestResultCreateModel[] { newTestResult }, teamProject, testRunId).Result;
RunUpdateModel runmodelUpdate2 = new RunUpdateModel(state: "Completed");
TestRun testRunUpdateResult2 = testClient.UpdateTestRunAsync(teamProject, testRunId, runmodelUpdate2).Result;
try
{
var u = new Uri("https://{My Account}.visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "PAT"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
int testpointid = 1;
string teamProject = "MyProjectName";
RunCreateModel run = new RunCreateModel(name: "TestCase Name", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("TestPlan Id"), pointIds: new int[] { testpointid });
TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;
TestCaseResult caseResult = new TestCaseResult() { State = "Completed", Outcome = "passed", Id = 100000 };
var testResults = testClient.UpdateTestResultsAsync(new TestCaseResult[] { caseResult }, teamProject, testrun.Id).Result;
RunUpdateModel runmodel = new RunUpdateModel(state: "Completed");
TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel, teamProject, testrun.Id, runmodel).Result;
}
catch (AggregateException e)
{
Console.WriteLine(e.InnerException.Message);
}
注意:需要时点配置
- 安装 Microsoft Team Foundation Server 扩展客户端包 [
安装包 Microsoft.TeamFoundationServer.ExtendedClient
-版本 15.112.1].
- 安装测试管理器扩展 - 在中创建测试计划、测试套件
测试选项卡。
- Testpointid 是测试用例编号 [order/index 中的测试用例
测试计划],而不是测试用例 ID。
- Name为Testcase名称,testrun id为通过自动捕获
testpointid[顺序为 1,2,3...]
我有 API link 的任务
POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}
https://www.visualstudio.com/en-us/docs/integrate/api/test/results#add-test-results-to-a-test-run
我需要程序通过API方式完成。
替代方案,我已尝试使用以下程序
public void GetResult()
{
var u = new Uri("https://{UserAccount}.visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "MyPAT"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
int testpointid = 100;
string teamProject = "Project12345";
RunCreateModel run = new RunCreateModel(name: "APIRun7", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100"), pointIds: new int[] { testpointid });
TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;
TestCaseResultUpdateModel testCaseUpdate = new TestCaseResultUpdateModel() { State = "Completed", Outcome = "Passed", TestResult = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100000") };
//var testResults = testClient.UpdateTestResultsAsync(new TestCaseResultUpdateModel[] { testCaseUpdate }, teamProject, testrun.Id).Result;
RunUpdateModel runmodel = new RunUpdateModel(state: "Completed");
TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel ,teamProject, testrun.Id, runmodel).Result;
}
我收到错误:
cannot convert from 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResultUpdateModel[]' to 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResult[]'
参考这些步骤:
:
var u = new Uri("https://[account].visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
string teamProject = "scrum2015";
int testRunId = 286;
int testSuiteId = 591;
RunUpdateModel runmodelUpdate = new RunUpdateModel(state: "InProgress");
TestRun testRunUpdateResult = testClient.UpdateTestRunAsync(teamProject, testRunId, runmodelUpdate).Result;
var results = testClient.GetTestResultsAsync(teamProject, testRunId).Result.First();
var testPoints= testClient.GetPointsAsync(teamProject,Int32.Parse(testRunUpdateResult.Plan.Id), testSuiteId).Result;
TestRun testRunUpdate = testClient.GetTestRunByIdAsync(teamProject, testRunId).Result;
TestResultCreateModel newTestResult = new TestResultCreateModel() { TestCase=new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(id: results.TestCase.Id.ToString()), TestPoint = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(id: testPoints.First().Id.ToString()), Outcome="Failed",State= "Completed" };
var updateResult= testClient.AddTestResultsToTestRunAsync(new TestResultCreateModel[] { newTestResult }, teamProject, testRunId).Result;
RunUpdateModel runmodelUpdate2 = new RunUpdateModel(state: "Completed");
TestRun testRunUpdateResult2 = testClient.UpdateTestRunAsync(teamProject, testRunId, runmodelUpdate2).Result;
try
{
var u = new Uri("https://{My Account}.visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "PAT"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
int testpointid = 1;
string teamProject = "MyProjectName";
RunCreateModel run = new RunCreateModel(name: "TestCase Name", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("TestPlan Id"), pointIds: new int[] { testpointid });
TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;
TestCaseResult caseResult = new TestCaseResult() { State = "Completed", Outcome = "passed", Id = 100000 };
var testResults = testClient.UpdateTestResultsAsync(new TestCaseResult[] { caseResult }, teamProject, testrun.Id).Result;
RunUpdateModel runmodel = new RunUpdateModel(state: "Completed");
TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel, teamProject, testrun.Id, runmodel).Result;
}
catch (AggregateException e)
{
Console.WriteLine(e.InnerException.Message);
}
注意:需要时点配置
- 安装 Microsoft Team Foundation Server 扩展客户端包 [
安装包 Microsoft.TeamFoundationServer.ExtendedClient -版本 15.112.1]. - 安装测试管理器扩展 - 在中创建测试计划、测试套件 测试选项卡。
- Testpointid 是测试用例编号 [order/index 中的测试用例 测试计划],而不是测试用例 ID。
- Name为Testcase名称,testrun id为通过自动捕获 testpointid[顺序为 1,2,3...]