How to write output from unit test?

Any call in my unit tests on Debug.Write(line) or Console.Write(Line) simply skipped during debugging and the output is never printed. Calling these functions from within classes, I use the job perfectly.

I understand that unit testing is for automation, but I still want to receive messages from unit test.

+93
debugging c # unit-testing visual-studio mstest
Jan 24 '11 at 20:35
source share
14 answers

Try using TestContext.WriteLine() , which displays the text in the test results.

Example:

  [TestClass] public class UnitTest1 { private TestContext testContextInstance; /// <summary> /// Gets or sets the test context which provides /// information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } [TestMethod] public void TestMethod1() { TestContext.WriteLine("Message..."); } } 

"Magic" is described in MSDN as "The test environment automatically sets a property that can then be used in unit tests."

+105
Jan 24 '11 at 20:52
source share

A little late to talk.

I also tried to get Debug, Trace, Console or TestContext to work in unit testing.

None of these methods will work or show output in the output window.

  Trace.WriteLine("test trace"); Debug.WriteLine("test debug"); TestContext.WriteLine("test context"); Console.WriteLine("test console"); 

VS2012 and Big
(from comments) There is no icon in Visual Studio 2012. Instead, there is an Output link in the test results. If you click on the link, you will see all WriteLine .

Before VS2012
Then I noticed that in my Test Results window after starting the test next to the green circle with little success, there is another icon that I double-clicked on it. These were the results of my test, and it included all types of letters above.

+138
Nov 23 '12 at 16:35
source share

In Visual Studio 2017, you can see the output from the test explorer.

1) In your test method Console.WriteLine ("something");

2) Run the test.

3) In the "Test Explorer" window, click "Passed Test Method".

4) And click on the "Output" link.

enter image description here

And click "Output", you can see the result of Console.Writeline (). enter image description here

+52
Apr 11 '18 at 5:59
source share

It depends on your test runner ... for example, I use XUnit, so in case you use, follow these instructions:

https://xunit.imtqy.com/docs/capturing-output.html

This method groups your output with each specific unit test.

 using Xunit; using Xunit.Abstractions; public class MyTestClass { private readonly ITestOutputHelper output; public MyTestClass(ITestOutputHelper output) { this.output = output; } [Fact] public void MyTest() { var temp = "my class!"; output.WriteLine("This is output from {0}", temp); } } 

The link I provided to write to your output window shows a different method, but I prefer the previous one.

+13
Jul 18 '17 at 19:05
source share

I think this is still relevant.

You can use this NuGet package: https://www.nuget.org/packages/Bitoxygen.Testing.Pane/

Call your own WriteLine method from this library.
It creates a test panel inside the output window and always places messages (during each test run, regardless of the DEBUG and TRACE flags).

To make tracing easier, I can recommend creating a base class:

 [TestClass] public abstract class BaseTest { #region Properties public TestContext TestContext { get; set; } public string Class { get { return this.TestContext.FullyQualifiedTestClassName; } } public string Method { get { return this.TestContext.TestName; } } #endregion #region Methods protected virtual void Trace(string message) { System.Diagnostics.Trace.WriteLine(message); Output.Testing.Trace.WriteLine(message); } #endregion } [TestClass] public class SomeTest : BaseTest { [TestMethod] public void SomeTest1() { this.Trace(string.Format("Yeah: {0} and {1}", this.Class, this.Method)); } } 
+4
Jan 24 '16 at 20:06
source share

Solved using the following example:

 public void CheckConsoleOutput() { Console.WriteLine("Hi Hi World"); Trace.WriteLine("Trace Trace the World"); Debug.WriteLine("Debug Debug WOrld"); Assert.IsTrue(true); } 

After completing this test, in the "Test Passed" section, you can view the output that will open the output window.

+1
Jan 31 '18 at 19:05
source share

Try using:

Console.WriteLine()

A call to Debug.WriteLine will only be executed during the definition of DEBUG.

Other suggestions should use: Trace.WriteLine , but I have not tried this.

There is also an option (not sure if VS2008 has it), but you can still use Debug.WriteLine when running a test with the Test With Debugger parameter in the IDE

0
Jan 24 2018-11-11T00:
source share

Are you sure you are running your unit tests in Debug? Debug.WriteLine will not be configured in release versions.

Two possible options:

  • Trace.WriteLine (), which is built into the inot release builds as well as debug

  • Cancel the DEBUG definition in the build settings for unit test

0
Jan 24 '11 at 20:41
source share

I get no output when my Test / Test Settings / Default Processor Architecture settings and the assemblies that reference my test projects do not match. Otherwise, Trace.Writeline () is working fine.

0
Jan 08 '13 at 18:35
source share

I use xunit, so this is what I use: Debugger.Log(0, "1", input);

PS: you can use Debugger.Break(); so you can log in out

0
May 30 '14 at 15:48
source share

Trace.WriteLine should work if you select the correct output (a drop-down list with the inscription "Show output from" found in the "Output" window)

0
Mar 25 '15 at 9:24
source share

Console.WriteLine will not work. In debug mode, only Debug.WriteLine () or Trace.WriteLine () will work.

I do the following: I include using System.Diagnostics in the test module. Then use Debug.WriteLine for my output, right-click on the test , select Debug selected tests . The output result will now appear in the Output window below. I am using Visual Studio 2017 vs 15.8.1, with the standard unit testing framework that VS provides.

0
Sep 12 '18 at 13:07 on
source share

Just ran into this. The reason / solution is another option of the above, so I will post.
My problem was that I did not get the output because I was writing the result set from the Linq asynchronous call to the console in a loop in an asynchronous context:

 var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345"); p.ForEachAsync(payment => Console.WriteLine(payment.Amount)); 

and therefore, the test did not write data to the console until the console object was cleared by the runtime (when running only one test). The solution was to first convert the result set to a list so that I could use the non-asynchronous version of forEach ():

 var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345").ToList(); p.ForEachAsync(payment =>Console.WriteLine(payment.Amount)); 
0
Nov 24 '18 at 16:58
source share

It really depends on the test participant, as @jonzim mentioned. For NUnit 3, I had to use NUnit.Framework.TestContext.Progress.WriteLine() to run the output in the Visual Studio 2017 output window.

NUnit describes how: here

As far as I understand, this revolves around additional parallelization of the test execution, which the test participants received.

0
03 Oct '19 at 15:34
source share



All Articles