How to write to the debugging window in unit testing?

Trace.WriteLine() does not work for some reason. Debug. not comfortable

  • And Log4Net did not try? He has a lot of flexible settings. - iluxa1810 September
  • If you are given an exhaustive answer, mark it as correct (tick the selected answer). - andreycha

2 answers 2

You can write to the debugging window during testing using either Console.WriteLine() or using Trace.WriteLine()

Console.WriteLine Example

 [TestMethod] public void TestConsoleWriteLine() { Assert.IsTrue(true); Console.WriteLine(@"Тест 'TestConsoleWriteLine' успешно пройден"); } 

enter image description here

enter image description here

enter image description here

Trace.WriteLine Example

 [TestMethod] public void TestTraceWriteLine() { Assert.IsTrue(true); Trace.WriteLine(@"Тест 'TestTraceWriteLine' успешно пройден"); } 

enter image description here

enter image description here

enter image description here

If none of these methods works, you need to look at the settings for VisualStudio and Resharper . Go to settings Resharper -> Options... -> Tools -> Unit Testing and see how you are configured there, try to make changes. Although, it all depends on the version of Resharper and VisualStudio .


Saw a comment about the exception: If your test does not expect Exception , you need to attach the ExpectedExceptionAttribute attribute to the test method, which indicates that an exception is expected during the execution of the test method.

  • Thanks, Console works great in resharper - Yaktens Tied

It actually works, but a little differently than usual. I ran the simplest test:

 [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Trace.WriteLine("Tracing from test"); } } 

and after it was run in the report (inside the test browser), the Output link appeared.

test explorer

On it the window opens:

and here it is

Yes, it is not as convenient as usual, and the output appears only all and only after the test run.

  • 2
    Plus, the output from Console.WriteLine() and TestContext.WriteLIne() (if you use MSTest). - andreycha
  • Hm Strange, when the test results are shown in the Resharper window, the results are empty. Looks like you'll have to use the standard window of the visual studio - Yaktens Teed
  • @YaktensTeed: Maybe the resharper redirects the debug output somewhere else? - VladD
  • I do not know. In my opinion, there is a bug at all. I added a throw new Exception (); test to a unit, so the resharper does not output infu at all, only writes that the test did not pass - Yaktens Tied
  • @YaktensTeed: Yes, it looks like a bug. If you have paid for the resharper money, contact support, let them repair. - VladD