Wednesday, June 26, 2013

Create Test Case Step Result using TFS API

We can create test case step result using TFS APIs. Here is small code snippet I have created.

ITestRun testRun = _plan.CreateTestRun(false);
ITestCaseResultCollection results = testRun.QueryResults();
ITestIterationResult iterationResult;
 
foreach (ITestCaseResult result in results)
{
    iterationResult = result.CreateIteration(1);   
    foreach (ITestStep testStep in result.GetTestCase().Actions)   
   {      
       ITestStepResult stepResult = iterationResult.CreateStepResult(testStep.Id);      
       stepResult.Outcome = TestOutcome.Passed; //you can assign different states here
       iterationResult.Actions.Add(stepResult);   
   }   
   iterationResult.Outcome = TestOutcome.Passed;   
   result.Iterations.Add(iterationResult);
}
 
results.Save(true);
testRun.Save();
testRun.Refresh();

5 comments:

  1. I have been trying to get results for a shared step through TFS API, but cannot get it to function correctly.
    Have you tried to create a shared step result with success.If so could you post the code snippet for it.

    Thanks

    ReplyDelete
  2. Thanks for feedback. I will try to implement your ask. Hope to post it soon. Keep watching this space

    ReplyDelete
  3. I am watching this space since september.. please reply if found any result..

    ReplyDelete
  4. Hey, I am using following code, and getting test cases, test steps, everything.
    But after execution when i refresh MTM and see that particular test case status, IT DOES NOT CHANGES, Please help..

    string pathName = Path.GetTempPath();
    Uri tfsUri = (args.Length < 1) ?
    new Uri("http://Server:Port/VDir") : new Uri(args[0]);


    String server = "http://sknyprojects.corporate.XX.com:8080/tfs/Collection";
    String project = "Thor";

    TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(server));
    Console.WriteLine("Success!\n");
    Console.WriteLine("getting project {0}...\n", project);

    ITestManagementTeamProject teamProject = projectCollection.GetService().GetTeamProject(project);


    ITestRun run = teamProject.TestRuns.Create();
    run.DateStarted = DateTime.Now;
    run.Title = "TestRun1";

    ITestPlan myplan = teamProject.TestPlans.Find(56);
    string myTestCaseTitle;
    int myTestCasesCount;

    string a = myplan.RootSuite.Title;
    int b = myplan.RootSuite.SubSuites.Count();

    ITestSuiteEntryCollection myTestCases = myplan.RootSuite.SubSuites[1].TestCases;
    myTestCasesCount = myTestCases.Count;

    for (int ff = 0; ff < myTestCasesCount; ff++)
    {
    int testCaseId = myplan.RootSuite.SubSuites[1].TestCases[ff].Id;
    int testCaseConfigId = myplan.RootSuite.SubSuites[1].TestCases[ff].Configurations[0].Id;
    TeamFoundationIdentity testCaseOwnwer = myplan.RootSuite.SubSuites[1].TestCases[ff].ParentTestSuite.LastUpdatedBy ;

    run.AddTest(testCaseId, testCaseConfigId, testCaseOwnwer);
    run.Save();

    }

    ITestCaseResult results = run.QueryResults()[0];

    results.Outcome = TestOutcome.Failed;
    var iterationResult = results.CreateIteration(1);

    foreach (ITestStep testStep in results.GetTestCase().Actions)
    {
    ITestStepResult stepResult = iterationResult.CreateStepResult(testStep.Id);
    stepResult.Outcome = TestOutcome.Failed;//you can assign different states here
    iterationResult.Actions.Add(stepResult);
    }

    results.Iterations.Add(iterationResult);
    results.State = TestResultState.Completed;
    results.Save(true);

    results.State = TestResultState.Completed;
    run.Save();
    run.Refresh();

    ReplyDelete
  5. Hi,
    Thanks for comment.
    Please refer detail code mentioned in this post - http://vivekbansod.blogspot.in/2013/07/how-to-bulk-upload-test-case-results-to.html

    that should help resolve your concern.

    ReplyDelete