TFS API and report performance: get test case results by test plan?

365 views Asked by At

In a report, I need to present all test results for a certain test plan.

Currently I am using the following function:

foreach (TestCase testCase in testCasesByWorkItem)
{
    List<ITestCaseResult> testCaseResults = teamProject.TestResults.ByTestId(testCase.TestCaseId).Where(x => x.State == TestResultState.Completed).ToList();

    ....

}

My understanding is that it returns all results for this test case, and one test case may belong to many test plans.

My problem is performance. The operation takes up to 25 seconds (in debug mode), and I have thousands of test cases.

I only need those test case results, that belong to a certain test plan.

For example, TestCaseX may have been executed for test plans Release1.0, Release2.0, .... Release20.0. And I am only interested in results for Release15.0.

Currently I retrieve results as above, and later filter by a correct test plan.

Is there a way to optimize performance by somehow only selecting test results that belong to a given test plan?

2

There are 2 answers

0
Andy Li-MSFT On BEST ANSWER

You can use below sample to get test runs against a test plan id:

TfsTeamProjectCollection tfctc = new TfsTeamProjectCollection(new Uri("http://tfsservername:8080/tfs/DefaultCollection"));
        ITestManagementService testmanagementService = tfctc.GetService<ITestManagementService>();
        var teamproject = testmanagementService.GetTeamProject("teamprojectname");
        var testruns = testmanagementService.QueryTestRuns("select * From TestRun");
        List<ITestRun> testrunInPlan = new List<ITestRun>(); 
        foreach (var testrun in testruns)
        {
            if (testrun.TestPlanId==31) // in this case TestPlanId is 31
            {
                testrunInPlan.Add(testrun);
            }
        }

And below sample to get test case result for a particular test run:

ITestCaseResultCollection testcases = testrun.QueryResults();

            foreach (ITestCaseResult testcase in testcases)
            {
                Console.WriteLine("TestCase ID: " + testcase.TestCaseId);
                Console.WriteLine("TestCase Title: " + testcase.TestCaseTitle);
                Console.WriteLine("Error Message: " + testcase.ErrorMessage);
            }

Please check this blog for the details on Test Management API: http://blogs.msdn.com/b/aseemb/archive/2012/08/07/code-snippets-on-test-management-apis.aspx

0
Hamid Shahid On

You might find using the TFS Rest API useful for what you are trying to do.

https://www.visualstudio.com/en-us/docs/integrate/api/test/runs

You will be able to get the list of all test runs for the given planId using the first API listed on the page and then use the runId to get all test returns using the Test results API

https://www.visualstudio.com/en-us/docs/integrate/api/test/results_1_0

Personally, I find using the API's a lot easier than working with the object model.