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 -
So clearly all 5 ways are working i.e. corresponding messages are getting logged. Each of these ways have their own advantages and disadvantages -
- 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.
- 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
- Debug.WriteLine: Same as Trace.WriteLine except that this shows up only for Debug build and is no-op in Retail build.
- 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.
Comments
Anonymous
June 13, 2010
How can I print something out and make it appear as a link?Anonymous
July 12, 2010
Sorry for late reply. For some reason, my blog settings got messed up and I had trouble login in. In general, the Coded UI Test forum (social.msdn.microsoft.com/.../threads) is better place to post your questions as that is monitored by many folks & you will get faster response. AFAIK - Customizing the test results to add link or other custom data is not possible today. Thanks, GautamAnonymous
September 08, 2010
Hi, I need to make my TEST RESULT to FAIL through my code after some conditions what i am expecting. How to code this? What method i have to use? thx... SCSVELAnonymous
September 12, 2010
You can simply say Assert.Fail.Anonymous
July 13, 2011
Could you please expalin where to copy this code in my script, i am new to vsts and C# aswell. i greately appreciateAnonymous
July 29, 2011
@Ravi - Not sure I follow you. You should check various getting started material that is already there in webmsdn.Anonymous
January 06, 2012
@Gautam Can you please tell how can I write TestContext.Writeline("") to a log file? Thanks in advanceAnonymous
January 16, 2012
@ankur - For writing to a file, you should use standard .NET API and write.Anonymous
February 29, 2012
Useful info, thanks for posting this! Is there any way to show output during an individual test (which is running for a long time)? I tried the above methods when running mstest.exe from the command prompt, but they all seem to output when the test completes, not while it is running.Anonymous
March 11, 2012
@Brian - Yes, you are right. This does not work while the test is in progress because we flush it only during the end of the test.