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).
I am using C# 4.5.2 in Visual StudioEnterprise 2015 on Windows 7 my code is:
setup test run (I run this once at the beginning of my tests):
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<ITestManagementService>();
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;
Add test result to test run (I run this after each test scenario finishes):
public void AddTestResult(int testCaseId, string testResult,DateTime startedTime, DateTime endedTime, ITestRun testRun)
{
if (testRun == null)
{
CreateTestRun();
}
TfsConfigurationServer configurationServer =
TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
ReadOnlyCollection<CatalogNode> 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<ITestManagementService>();
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=>r.TestPointId==testPoint.Id);
tfsTestResult.State = TestResultState.Completed;
tfsTestResult.DateCompleted = endedTime;
tfsTestResult.DateStarted = startedTime;
tfsTestResult.Duration = endedTime - startedTime;
if (testResult == "passed" && 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();
}
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).
This is the line that throws the exception when I run the method with a second, different ID:
var tfsTestResult = testRun.QueryResults().Single(r=>r.TestPointId==testPoint.Id);
If I run the method again with the same Id as the first time it works.
The exception is:
An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code
Additional information: Sequence contains no matching element
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.