Monday, July 8, 2019

How to get a custome HTML report from Azure build pipeline for a unit test project

...This might not be the best option but it works for me...


Normaly you will have your test cases on a one test project.
In our case I add another project to the same soulution as on the below image.


That 2nd project will contain nessosory steps to:
  1. Locate the TRX file which is genarated by the Azure build pipeline (vstestconsol.exe).
  2. Convert that file to HTML.
  3. Email it to a given email address.
So the plan will be, I will copy the original TRX file to a different location / folder.
Rename the TRX file.
Then using "TrxerConsole.exe" genarate the HTML file and email it.

So as the 1st step add the 2nd project it will also a unit test type project.

Add below methods to your newly created unit test project which rename the original TRX.



        [TestMethod]
        public void RenameOrgTRX()
        {
            string sourcePath = @"d:\a\1\s\UnitTestProject1\CleanUp\ReportCreator\";
          
           string[] sourcefiles = Directory.GetFiles(sourcePath,"*.trx");

            foreach (string sourcefile in sourcefiles)
            {
                string fileName = Path.GetFileName(sourcefile);
                File.Move(sourcePath+fileName, sourcePath+"report.trx");
            }
        }

Then 2nd method to genarate the HTML file.
     [TestMethod]
        public void GenerateHTML()
        {
            Process proc = null;
            proc = new Process();
            proc.StartInfo.WorkingDirectory = @"d:\a\1\s\UnitTestProject1\CleanUp\ReportCreator";
           
            proc.StartInfo.FileName = "TrxtoHTML.bat";
            proc.StartInfo.CreateNoWindow = false;
            proc.Start();
            proc.WaitForExit();
        }

At this moment create a new folder at your 2nd project named "ReportCreator".
Then create a bat file inside the ReportCreator folder.

bat file will looks like this.
"TrxerConsole.exe report.trx" - So in side the bat file im calling TRXERconsole.exe which can convert trx to HTML.
Report.trx will be the TRX name.

At the 1st method we have successfully rename the autogenarated TRX file to report.trx so im sure that the TRX name will alwasy be the same.
After runinng this method you will get a HTML file named report.trx.html which is the final test report.


As the 3rd method add a send email method to send an email with attaching the newly created report.trx.html file.

 [TestMethod]
        public void SendEmail()
        {
            string fromAddress = "XXXXX@gmail.com";
            string mailPassword = "XXXX";
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Port = 587;
            SmtpServer.EnableSsl = true;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);

            MailMessage myMail = new System.Net.Mail.MailMessage();
            myMail.From = new MailAddress("XXXXX@gmail.com");
            myMail.To.Add("XXXX@gmail.com");
            myMail.To.Add("XXXX@tiqri.com");
        

            myMail.Subject = "TeckTalk";
            myMail.Body = "Please download the attached HTML file to view the API status";
            string attachmentFile = "d:\\a\\1\\s\\ReportGen\\NewFolder1\\report.trx.html";
            Attachment attachment = new Attachment(attachmentFile, MediaTypeNames.Application.Octet);
            myMail.IsBodyHtml = true;
            myMail.Attachments.Add(attachment);
            SmtpServer.Send(myMail);


        }



Lets create the pipeline.


This is how the pipeline looks like.
Red color task will be the original test task whic contain all the the unit tests.
Green task will be the copy file task.
Yellow task will contain 2nd test method which contain  Rename, GenerageHTM and SendEmail methods.

YOU ARE DONE.