Stack Exchange
Add Test Result to a test run(testcase) in VSTS
I need to add test result to a testcase in VSTS. I'm new to VSTS and not sure what went wrong with my code
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();

I find this question  similar but dint get answer.
Note:
I don't find option to create Test Suite and then test case in that. Just created test case directly. Is it mandate to create Test Suite? If so, how?

My code is much similar and i got AggregateException error. Could someone help with us?

Does someone tried through API provided
https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}

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

        }

Note: Instructions to configure

Install Microsoft Team Foundation Server Extended Client package 

Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1

Install Test Manager extension, Create test plan, test suite in Test tab
testpointid is TestCase number (i.e. order/index of testcase in test plan) and not the ID of the TestCase
name is Testcase name, testrun.Id is auto-captured through testpointid (first index being 1)


@starain - Thanks a lot. Your suggestions helped me in suceeding. I've summarized your suggestions in Note Section.

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

Install Test Manager extension
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:

Go to backlog board
Add test



Go to Test page and check test plan, suite



Nice article to create Test Suite & Test Plan
