Alex Rivera | Logout

How to create a test run and result using the Team Foundation Server API?

Asked 2011-06-28T11:37:47.067
16

I have found several samples about retrieving test results using the TFS API, but no documentation on creating results programmatically. My aim is to create a lightweight alternative to using Microsoft Test Manager for running manual tests. Does anyone have experience with this? Are there any examples out there that I'm missing?

Here's what I have so far:

ITestCaseResult CreateNewTestCaseResult(ITestSuiteEntry testCaseEntry)
{
    var run = testCaseEntry.TestSuite.Plan.CreateTestRun(false /* not automated */);
    run.AddTest(testCaseEntry.TestCase.Id, suiteEntry.TestSuite.DefaultConfigurations[0].Id, suiteEntry.TestSuite.Plan.Owner);
    run.Save(); // so that results object is created
    return run.QueryResults()[0];
}

I'm not sure if this is the correct way to initate a new run, and I'm not sure how to record results for each action of the test.

Edit
Report

1 Answer

11

Test Action does not appear to have properties for setting pass/fail or to add attachments.

public interface ITestAction : INotifyPropertyChanged {
    int Id { get; }
    ITestBase Owner { get; }
    ITestActionGroup Parent { get; }

    ITestAction CopyToNewOwner(ITestBase newOwner);
    void MoveToNewOwner(ITestBase newOwner); }

That is done at the parent level (TestCase).

ITestCaseResult result = run.QueryResults()[0];
IAttachmentCollection collection = result.Attachments;
string x = result.Comment;

Here is how you correctly initiate a new run:

namespace SampleRunCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://pradeepn-tcm:8080/tfs/DefaultCollection"));
            ITestManagementTeamProject project = tfs.GetService<ITestManagementService>().GetTeamProject("Pradeep");

            // Create a test case.
            ITestCase testCase = CreateTestCase(project, "My test case");

            // Create test plan.
            ITestPlan plan = CreateTestPlan(project, "My test plan");

            // Create test configuration. You can reuse this instead of creating a new config everytime.
            ITestConfiguration config = CreateTestConfiguration(project, string.Format("My test config {0}", DateTime.Now));

            // Create test points. 
            IList<ITestPoint> testPoints = CreateTestPoints(project,
                                                            plan,
                                                            new List<ITestCase>(){testCase}, 
                                                            new IdAndName[] { new IdAndName(config.Id, config.Name) });

            // Create test run using test points.
            ITestRun run = CreateTestRun(project, plan, testPoints);

     
answered 2011-06-28T22:44:07.390

Your Answer