Lightweight Automation Framework: How to create your own log

Published Thursday, March 19, 2009 1:33 AM

 Update: With the newest release of LTAF there is a simplified way of attaching to events that can be used to generate a custom log. Read more about it in this post.

One question that I got from those using the Lightweight Test Automation Framework is how to create your own log. In this post I’ll go over the changes you need to make to create your own log that outputs an html table with the tests that ran and their results. The output will be really simple, but it is meant as a sample.

1. The first thing is to create a class that inherits from TestcaseExecutor. This class lets you override methods that are called when each test case starts and ends, and we’ll use it to create our custom log. We’ll also override a method that is called at the end of all test run, where we will write the log to disk (notice that in this sample asp.net needs write access to the Test directory)

   1: public class LogTestcaseExecutor: TestcaseExecutor
   2: {
   3:     private readonly string _testPath;
   4:     private StringBuilder _log;
   5:  
   6:     public LogTestcaseExecutor(string testPath)
   7:         : base()
   8:     {
   9:         _testPath = testPath;
  10:         _log = new StringBuilder();
  11:     }
  12:  
  13:     protected override void OnTestcaseExecuted(TestcaseExecutionEventArgs e)
  14:     {
  15:         string tableRow = @"
  16: <tr style='"background-color:{2};"">
  17:     <td>{0}</td>
  18:     <td>{1}</td>
  19: </tr>
  20: ";
  21:         _log.AppendFormat(tableRow,
  22:             e.WebTestName,
  23:             (e.Exception == null) ? "&nbsp" : e.Exception.Message,
  24:             (e.Passed) ? "green" : "red");  
  25:         
  26:         base.OnTestcaseExecuted(e);
  27:     }
  28:  
  29:     protected override void OnTestRunFinished(TestRunFinishedEventArgs e)
  30:     {
  31:         string log = @"
  32: <html>
  33:     <body>
  34:         <table style='"width:100%;border-style: solid;border-width: 1px;"">
  35:             <tr>
  36:                 <td>Testcase</td><td>Error</td>
  37:             </tr>
  38:             {0}
  39:         </table>
  40:     </body>
  41: </html>";
  42:  
  43:         File.WriteAllText(Path.Combine(_testPath, "RunLog.htm"), String.Format(log, _log.ToString()));
  44:         
  45:         base.OnTestRunFinished(e);
  46:     }
  47: }

 

 

2. Next you want to update the DriverPage.aspx to use our new LogTestcaseExecutor. We’ll override the RunTestcases method and set the TestcaseExecutor property to our custom one. (Notice that  I pass the path to where the log is going to get written).

   1: public partial class DriverPage : TestDriverPage
   2: {
   3:     protected override void RunTestCases()
   4:     {
   5:         this.TestcaseExecutor = new LogTestcaseExecutor(Server.MapPath(""));
   6:         base.RunTestCases();
   7:     }
   8:  
   9: }

 

That’s it. As an example, after running some tests the log will be under the Test directory.

image

 

And this is how the custom log looks like (yes, not fancy, but its the idea that matters :P)

image

You can use this feature to log testcase execution in anyway that you see fit. 

Federico Silva Armas
SDET ASP.NET QA

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)