Update status of the test case in MTM using c#
Asked Answered
O

0

8

I have assigned to create C# application to update the test case status in MTM.

I tried the below code and it is working fine. But the problem is the below code is updating the result If the test case is already executed, it is not updating the status of the newly created test case(no history of run)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.TestManagement.Common;
using System.IO;

namespace UpdateTFS
{
    class Program
    {
        static void Main(string[] args)
        {
            //args[0] = TestplanId; args[1] = TestcaseId; args[2] = Status; args[3] = configuration
            int test_plan = Int32.Parse(args[0]);
            int test_case = Int32.Parse(args[1]);
            int test_config = Int32.Parse(args[3]);
            int result_updated = 0;
            TextWriter tw = new StreamWriter(@"C:\Users\*\Desktop\MTM.txt", true);

            tw.WriteLine("Attempting the test case " +test_case+ "to be marked as "+args[3]);

            //Create TFS connection
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName("<tfs_url>"));

            ITestManagementService tms = tfs.GetService<ITestManagementService>();

            //Connect to the project
            ITestManagementTeamProject proj = tms.GetTeamProject("<project>");

            ITestPlan Plan = proj.TestPlans.Find(test_plan);


            if (Plan != null)
            {
                ITestCase tc = proj.TestCases.Find(test_case);
                if (tc != null)
                {
                    ITestPointCollection points = Plan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseId =" + args[1]);


                    foreach (ITestPoint p in points)
                    {

                        var testResults = proj.TestResults.ByTestId(test_case);
                       foreach (ITestCaseResult result in testResults)
                        {
                            if (p.ConfigurationId == test_config)
                            {
                                if (args[2] == "Failed")
                                {
                                    result.Outcome = TestOutcome.Failed;
                                }
                                else if (args[2] == "Passed")
                                {
                                    Console.WriteLine("Passed");
                                    result.Outcome = TestOutcome.Passed;
                                }
                                else if (args[2] == "NotExecuted")
                                {
                                    result.Outcome = TestOutcome.NotExecuted;
                                }
                                result.Save(true);
                                result_updated = 1;
                                tw.WriteLine("Test case ID =>" + test_case + "is marked as " + args[2] + "MTM");
                            }
                        }
                        if (result_updated == 1) { break; }
                    }
                    if (result_updated == 0)
                    {
                        tw.WriteLine(args[3] + " => Configuration is not found in the test case " + test_case);
                    }
                }
                else
                {
                        tw.WriteLine(args[1] + " Test case not found the plan");
                }
            }
            else
            {
                tw.WriteLine(args[0] + " Plan not found in MTM");
            }
         tw.Close();
        }
    }
}

someone help me on this.

Overtax answered 28/6, 2016 at 10:22 Comment(5)
The code is updating ITestCaseResult result outcomes only for test cases that were executed against a test run only. You can create a new test run then collect result from that test run and iterate through results and set: result.Outcome = TestOutcome.NotExecuted;Bothwell
Is there a way to make a test case as passed, without referring its test result?Overtax
You can set: result.Outcome based on args[2] value. I have assumed that no result means it was not executed yet.Bothwell
Yes, no result means it was not executed yet. I tried with result.Outcome.passed but it is not updating the result since we don;t have results for the testcase.Overtax
Sorry it is too late commnet.I gues you need to add the new test run to you test point andthen update the status. I worked on it very long back so not able to recollect the code snippet.Agnosticism

© 2022 - 2024 — McMap. All rights reserved.