title: TFS API Add multiple test points and results to one test run
body: <p>I am using the TFS API to add a test run and would like to add multiple test points to a test run and add one test result to each test point in the test run. When I try to retrieve the test results after adding the second test point I only get one test result back (the one that corresponds to the first test point).</p>
  
  <p>I am using C# 4.5.2 in Visual StudioEnterprise 2015 on Windows 7 my code is:</p>
  
  <p>setup test run (I run this once at the beginning of my tests):</p>
  
  <pre><code>    TfsConfigurationServer configurationServer =
          TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
      CatalogNode collectionNode = configurationServer.CatalogNode.QueryChildren(
         new[] { CatalogResourceTypes.ProjectCollection },
         false, CatalogQueryOptions.None).Single();
      Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
      TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
      ITestManagementService testManagementService = teamProjectCollection.GetService&lt;ITestManagementService&gt;();
      ITestManagementTeamProject testProject = testManagementService.GetTeamProject(teamProjectName);
      ITestPlan testPlan = testProject.TestPlans.Find(TestPlanId);
      ITestRun testRun = testPlan.CreateTestRun(true);
      testRun.DateStarted = DateTime.Now;
      testRun.IsAutomated = true;
      testRun.Title = "Automated test run " + testRun.DateStarted.ToString();
      testRun.State = TestRunState.InProgress;
  </code></pre>
  
  <p>Add test result to test run (I run this after each test scenario finishes):</p>
  
  <pre><code>    public void AddTestResult(int testCaseId, string testResult,DateTime startedTime, DateTime endedTime, ITestRun testRun)
      {
          if (testRun == null)
          {
              CreateTestRun();
          }
  
  
          TfsConfigurationServer configurationServer =
              TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
          ReadOnlyCollection&lt;CatalogNode&gt; collectionNodes = configurationServer.CatalogNode.QueryChildren(
             new[] { CatalogResourceTypes.ProjectCollection },
             false, CatalogQueryOptions.None);
          var collectionNode = collectionNodes.Single();
          // List the team project collections
  
          // Use the InstanceId property to get the team project collection
          Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
          TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
          ITestManagementService testManagementService = teamProjectCollection.GetService&lt;ITestManagementService&gt;();
          ITestManagementTeamProject testProject = testManagementService.GetTeamProject(teamProjectName);
  
          ITestPlan testPlan = testProject.TestPlans.Find(TestPlanId);
  
          var testPoints = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseID = '" + testCaseId + "'");
          var testPoint = testPoints.First();
          testRun.AddTestPoint(testPoint,null);
          testRun.TestEnvironmentId = testPlan.AutomatedTestEnvironmentId;
          testRun.Save();
  
          var tfsTestResult = testRun.QueryResults().Single(r=&gt;r.TestPointId==testPoint.Id);
          tfsTestResult.State = TestResultState.Completed;
          tfsTestResult.DateCompleted = endedTime;
          tfsTestResult.DateStarted = startedTime;
  
          tfsTestResult.Duration = endedTime - startedTime;
  
          if (testResult == "passed" &amp;&amp; tfsTestResult.Outcome!=TestOutcome.Failed)
          {   // ^ if multiple specflow scenarios have been run with the same test case ID then don't set it to pass if a previous one in this test run has failed
              tfsTestResult.Outcome = TestOutcome.Passed;
          }
          else
          {
              tfsTestResult.Outcome = TestOutcome.Failed;
          }
          tfsTestResult.Save();
  
          testRun.Save();
  
      }
  </code></pre>
  
  <p>For the first scenario it works perfectly well, but after the next scenario with a different testCaseId it throws an exception when trying to find the corresponding test result to that test point (the test results query only returns one test result that corresponds to the first test point that I added the first time I run the method).</p>
  
  <p>This is the line that throws the exception when I run the method with a second, different ID:
              var tfsTestResult = <code>testRun.QueryResults().Single(r=&gt;r.TestPointId==testPoint.Id);</code>
  If I run the method again with the same Id as the first time it works.
  The exception is:</p>
  
  <blockquote>
    <p>An exception of type 'System.InvalidOperationException' occurred in
    System.Core.dll but was not handled in user code</p>
    
    <p>Additional information: Sequence contains no matching element</p>
  </blockquote>
  
  <p>I tried skipping the bit that updates the test result if there's no matching test result and it looks like in MTM the second test point isn't being added, so I guess this is related.</p>