-->

TFS API Add multiple test points and results to on

2019-07-20 19:11发布

问题:

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.

回答1:

I finally found out that what I was trying to do isn't currently possible. Once you save a TestRun you cannot add any more TestPoint entries. The only way to add multiple test points to a test run is to add them all before you save the test run. Reference: TFS API: Cannot add testpoint after testrun is saved

https://blogs.msdn.microsoft.com/nidhithakur/2011/04/08/tfs-programatically-importing-testcase-results-to-mtm/

Ideally, you should be able to add the results while adding the test point to the run but the run.Save() API is only working for single Save right now. So, you will need to add all the testpoints, save the test run and then iterate over the run collection to add results individually.

I've changed my code to store test results during the run, then batch add them to a new TestRun and save the TestRun after all tests have finished. My new code, which works is:

    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(investigateRelease1TestPlanId);
    foreach (MtmTestResultInfo result in testResults)
    {
        var testPoints = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseID = '" + result.TestCaseId + "'");
        var testPoint = testPoints.First();
        testRun.AddTestPoint(testPoint, null);
    }

    testRun.DateStarted = dateStarted;
    testRun.DateCompleted = dateCompleted;
    TimeSpan timeTaken = dateCompleted - dateStarted;
    testRun.State = TestRunState.Completed;
    testRun.Save();
    //cannot add comment until after test run is saved
    testRun.Comment = "my comment"

    var tfsTestResults = testRun.QueryResults();
    foreach (MtmTestResultInfo result in testResults)
    {
        ITestCaseResult tfsTestResult = tfsTestResults.Single(r => r.TestCaseId == result.TestCaseId);
        tfsTestResult.DateStarted = result.DateStarted;
        tfsTestResult.DateCompleted = result.DateCompleted;
        tfsTestResult.Outcome = result.Outcome;
        tfsTestResult.Comment = result.Comment;
        tfsTestResult.ErrorMessage = result.ErrorMessage;
        tfsTestResult.RunBy = testRun.Owner;
        tfsTestResult.Duration = result.DateCompleted - result.DateStarted;
        tfsTestResult.State=TestResultState.Completed;

        tfsTestResult.Save();
        testRun.Save();
    }

    testRun.Save();

Supporting class to store test results:

public class MtmTestResultInfo
{
    public DateTime DateStarted { get; set; }
    public DateTime DateCompleted { get; set; }
    public TestOutcome Outcome { get; set; }
    public int TestCaseId { get; set; }
    public string Comment { get; set; }
    public string ErrorMessage { get; set; }

}


标签: c# tfs mtm