Logging a message in test result as part of an automated test

Multiple folks have asked in forums on how to log a message from a Coded UI Test for tracing\debugging purpose.  I know of as many as 5 ways of doing this from any test type (Coded UI Test, Unit Test etc).  Try this code out -

    1:          [TestMethod]
    2:          public void CodedUITestMethod1()
    3:          {
    4:              Console.WriteLine("Console.WriteLine()");
    5:              Console.Error.WriteLine("Console.Error.WriteLine()");
    6:              TestContext.WriteLine("TestContext.WriteLine()");
    7:              Trace.WriteLine("Trace.WriteLine()");
    8:              Debug.WriteLine("Debug.WriteLine()");
    9:          }

If you check the test result window, you should see output like -

image

So clearly all 5 ways are working i.e. corresponding messages are getting logged.  Each of these ways have their own advantages and disadvantages -

  1. TestContext.WriteLine:
    • Advantage - Shows up property in the separate section in test result.  It is meant for this purpose only.
    • Disadvantage - You will have to pass TestContext around from you main test class to other classes (like UIMap class in Coded UI Test) to use it.
    • Recommendation – Prefer this over other methods for tracing in test code.
  2. Trace.WriteLine:
    • Advantage – Shows up in VS Output window too during debugging.
    • Disadvantage - The message here could be lost among other trace messages from other components (or product code).
    • Recommendation – Use it in your product code but avoid in test code.  That is good way to separate out messages from product code vs test code. You can use this though in certain scenarios where either TestContext is not there or you want to determine the sequence of operation between test & product code
  3. Debug.WriteLine: Same as Trace.WriteLine except that this shows up only for Debug build and is no-op in Retail build.
  4. Console.WriteLine and Console.Error.WriteLine: These, though work, are tricky. The test harness redirects the Standard Output and Standard Error to capture the output\error from your product code and not for tracing. So, the recommendation is to avoid using these.

In short, use TestContext.WriteLine wherever possible.