Thursday, February 13, 2014

MTM ( Microsoft Test Manager) - Getting latest results as email notification

Author: Ranjit Gupta and Raj Kamal

Today, MTM (Microsoft Test Manager) doesn't provide an email report with the latest test results for a given test plan. Well, you can do this  by using the below snippets of code and configure it for your test team and other stakeholders. We hope you find this useful. Your comments are welcome.

1.      Query test plan

ITestPlanCollection mTestPlanCollection = testProject.TestPlans.Query((string.Format("Select * From TestPlan where PlanName = '{0}'", testPlan)));

2.      Getting the status of the desired Test Plan


ITestPointCollection teamtestPass = testplan.QueryTestPoints(string.Format("SELECT * FROM TestPoint where LastResultOutcome='Passed'"));

                ITestPointCollection teamtestFail = testplan.QueryTestPoints(string.Format("SELECT * FROM TestPoint where  LastResultOutcome='Failed'"));

                ITestPointCollection teamtestBlocked = testplan.QueryTestPoints(string.Format("SELECT * FROM TestPoint where  LastResultOutcome='Blocked'"));

                ITestPointCollection teamtot = testplan.QueryTestPoints(string.Format("SELECT * FROM TestPoint "));

                teampass = teampass + teamtestPass.Count;

                teamfail = teamfail + teamtestFail.Count;

                teamblock = teamblock + teamtestBlocked.Count;

                teamtotal = teamtotal + teamtot.Count;


teamtestPass contains all those testpoints which has Outcome as Passed. So above queries gives you the status of your test plan


3.      In your report you might want to list all test cases or all failed/blocked test cases and with some details

Below statement will give you the ID, title, configuration, assigned to, outcome and the duration for the test point

foreach (ITestPoint point in teamtot)


Console.WriteLine(point.Id + "-- " + point.TestCaseWorkItem.Title + "-- " + point.ConfigurationName + "-- " + point.MostRecentResult.Outcome.ToString() + "-- " + point.AssignedToName + "-- " + point.MostRecentResult.Duration);


PS: The testpoint which are in Active state “point.MostRecentResult.Outcome.ToString()” would throw null exception, you need to handle that in your code

4.      You can capture all this information into a html file for better reporting and send it as automated mailer


SmtpClient client = new SmtpClient("smtphost");

            client.UseDefaultCredentials = true;



            string fromAddress = Environment.GetEnvironmentVariable("USERNAME") + "";

            MailAddress from = new MailAddress(

                fromAddress, ProjectSettings.Default.FromName, System.Text.Encoding.ASCII);

            List<MailAddress> to = new List<MailAddress>();


            string address = ProjectSettings.Default.toAddress;

            string[] toRecipent = address.Split(';');


            foreach (string add in toRecipent)


                to.Add(new MailAddress(add));


MailMessage message = new MailMessage();

       message.IsBodyHtml = true;

       message.From = from;

       to.ForEach(entry => message.To.Add(entry));

       message.Body = report.ToString();

            message.BodyEncoding = System.Text.Encoding.UTF8;

            message.Subject = ProjectSettings.Default.mailSubject;

            message.SubjectEncoding = System.Text.Encoding.UTF8;




No comments: