What’s the first thing every developer does when things go wrong?
He debugs the code, of course.
Well, sometimes you want to debug the tests a little bit, put Console.WriteLine or whatever works and see what happens in tests.
This post will show you how to write messages to the console window during unit testing in Visual Studio 2022. This can be helpful when you want to report the results of your tests or get feedback on test execution.
Let’s get started!
How to write to the console in a unit test when you use MSTest?
When you are using MSTest to write unit tests, there may be times when you want to write something to the console. For example, you may want to write out the contents of an object for debugging purposes.
The easiest way to write output from the test is to use the Console.WriteLine method.
[TestMethod]
public void test_that_writes_to_console2()
{
Console.WriteLine("Some serious debugging happening here...");
}
It will write the string as the Standard output in the Visual Studio Text Explorer window.
Another option you can use is the Debug.WriteLine() method. To use this method, you need add System.Diagnostics to your using statements at the top.
[TestMethod]
public void test_that_writes_to_console()
{
Debug.WriteLine("Some serious debugging happening here...");
}
If you simply run your test, this method will take whatever string you provide and write it as Debug Trace to the Summary pane of the Text Explorer window.
If you right-click on your test and click on Debug (or Ctrl + R, Ctrl + T),
then the output will also appear in the Output window of Visual Studio.
This is probably more useful since you don’t have to click on every test separately. Instead, you can track what’s happening to all of them in a single place.
How to write to the console in a unit test when you use xUnit?
If you use xUnit as your preferred unit testing framework, then the Console.WriteLine won’t work in tests. For example, this code:
[Fact]
public void xunit_write_to_console()
{
Console.WriteLine("xunit output");
}
doesn’t do anything.
The Debug.WriteLine approach will work, but you need to remember to run the test in Debug mode.
What you need to do instead is to use the ITestOutputHelper provided by the xUnit framework. To use the ITestOutputHelper, inject it into your test class via constructor.
public class WriteToConsoleClass
{
private readonly ITestOutputHelper output;
public WriteToConsoleClass(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void xunit_write_to_console()
{
output.WriteLine("xunit output");
}
}
After you execute the test, the test output will appear in the Text Explorer under Test Detail Summary.
How to write to the console in a unit test when you use NUnit?
Writing to the console when using NUnit is a simple process.
Starting with NUnit 3, you have access to the TestContext class. It is responsible for containing information about the execution environment and the test method itself.
An example of using the TextContext class:
public class Tests
{
[Test, Order(3)]
public void First_test()
{
TestContext.Out.WriteLine("First_test");
}
[Test, Order(2)]
public void Second_test()
{
TestContext.Out.WriteLine("Second_test");
}
[Test, Order(1)]
public void Third_test()
{
TestContext.Out.WriteLine("Third_test");
}
}
You can even simplify the code a little bit by leaving out the Out property:
public class Tests
{
[Test, Order(3)]
public void First_test()
{
TestContext.WriteLine("First_test");
}
[Test, Order(2)]
public void Second_test()
{
TestContext.WriteLine("Second_test");
}
[Test, Order(1)]
public void Third_test()
{
TestContext.WriteLine("Third_test");
}
}
The output will be visible in the Output window.
In case you are wondering what the Order means, it’s for controlling test order execution. If you would like to learn more about that, check out this article.
Conclusion
If you want to write output to the console when using xUnit, MSTest, or NUnit in Visual Studio 2022, this article has shown you how.
At the end of the day, it’s up to you what testing framework will work best for your project. Depending on personal preferences or company standards, any of these frameworks could be a great option.
Hopefully, this article has given you a better understanding of how each one works when it comes to writing text output to the console.