This post shares code snippets on how to do some common operations using Test Management APIs.

Snippet #1 : – Given a test run, find associated test suite.

// Query results from the run.
ITestCaseResult result = testManagementRun.QueryResults()[0];

// Find the test plan
ITestPlan testPlan = testManagementProject.TestPlans.Find(testManagementRun.TestPlanId);

// Get test point from result.
ITestPoint testPoint = testPlan.FindTestPoint(result.TestPointId);

ITestSuiteBase testSuite = testManagementProject.TestSuites.Find(testPoint.SuiteId);
return testSuite.Title;

(Update 25/8/2012)

Snippet #2:- Find all test plans which are active and where the 'end date' is before a date.

ITestPlanCollection tpc = proj.TestPlans.Query("Select * from
TestPlan where PlanState = 'Active' AND EndDate < ‘2009-10-14’ ");

Snippet #3: - Find test results in a manual test run

// Get the test runs
IEnumerable<ITestRun> testRuns = testManagementService.QueryTestRuns(string.Format("select * From TestRun where TestRunID={0} AND IsAutomated=0", testRunId));

// Use the first test run assuming that there is a valid run
ITestRun testRun = testRuns.FirstOrDefault();

// Find results in the run
ITestCaseResultCollection results = testRun.QueryResults();

(Update 10/8/2012)

Snippet #4: – How to create/update a new test run

// Create a new test run
ITestRun testRun = testPlan.CreateTestRun(true);     
   
// Add the tests that are part of the run
testPoints = testPlan.QueryTestPoints("SELECT * from TestPoint");
foreach (ITestPoint testPoint in testPoints)
{
    testRun.AddTestPoint(testPoint, null);
}
testRun.Save();         


// Update the outcome of the tests      
ITestCaseResultCollection results = testRun.QueryResults();
foreach(ITestCaseResult result in results)
{
    result.Outcome = TestOutcome.Passed;
    result.State = TestResultState.Completed;
    result.Save();
}
testRun.Save();
testRun.Refresh();