将测试结果添加到VSTS中的测试运行(测试用例)

时间:2017-06-15 07:28:56

标签: c# tfs azure-devops-rest-api

我需要将测试结果添加到VSTS中的测试用例中。我是VSTS的新手,不知道我的代码出了什么问题

var ur = new Uri("https://{myaccount}.visualstudio.com");
VssCredentials cr = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "XXXXX"));
var connection = new VssConnection(ur, cr);
var testClient = connection.GetClient<TestManagementHttpClient>();
int testpointid = 123;
string teamProject = "myproj";
RunCreateModel run = new RunCreateModel(name: "name123", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("123"), pointIds: new int[] { testpointid });
TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;
Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100000") };
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;
Console.WriteLine("Success");
Console.ReadKey();

我发现this question相似,但得到答案。

注意:

我找不到创建Test Suite的选项,然后测试用例。刚刚创建了测试用例。是否要求创建Test Suite?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:1)

The test suite is required. There are some ways to create test suite:

Way 1:

  1. Install Test Manager extension
  2. Create test plan, test suite in Test tab

Way 2: Using MTM to create test plan, test suite: Organizing Test Cases Using Test Suites.

Note: MTM is include in Visual Studio (e.g. VS Ultimate, Premium, Enterprise (2015) and visual studio test professional).

Way 3:

  1. Go to backlog board
  2. Add test

enter image description here

  1. Go to Test page and check test plan, suite

enter image description here

答案 1 :(得分:1)

        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);

        }

注意:配置说明

  1. 安装Microsoft Team Foundation Server扩展客户端软件包
  2. Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1

    1. 在“测试”选项卡中安装测试管理器扩展,创建测试计划,测试套件

    2. testpointid是TestCase编号(即测试计划中测试用例的顺序/索引),而不是TestCase的ID

    3. name是测试用例名称,testrun.Id是通过testpointid自动捕获的(第一个索引为1