Mary June
Mary June

Reputation: 159

How to display message on the unit test result window for successful unit tests? c#

I gave written a data-driven unit test in C# and the unit test uses MS Visual Studio unit test framework. For unsuccessful unit tests, exception would be thrown and a message would be shown in the unit test result windows. I hope to display some message for successful unit tests and the messages should be shown in the unit test output window. How to do it? I have tried Console.WriteLine("Message") but it doesn't work as I wish. Any suggestions?

Upvotes: 3

Views: 5032

Answers (2)

scrat.squirrel
scrat.squirrel

Reputation: 3826

In 2020, VS2019, one can output text to the test output window like this.

For this code, you'll need a dependency on Microsoft.VisualStudio.TestPlatform and works best in conjunction with Microsoft.VisualStudio.TestTools.

The test output is accessible from the Test Explorer side-bar, as a link, only after the test was executed. Clicking on that link will open the full test output window.

using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace YourNamespace.Tests
{
    [TestClass]
    public class SomeTestClassName
    {

        [TestMethod]
        public void Test1()
        {
            ConsoleOutput.Instance.WriteLine("+++ This test line ends up in the test output +++", OutputLevel.Information);
        }
    }
}

Upvotes: 2

Charly
Charly

Reputation: 851

Use Debug.WriteLine() to write to the output window.

Place this at the end of your test:

System.Diagnostics.Debug.WriteLine("Test Finished!");

Upvotes: 1

Related Questions