简体   繁体   中英

Output from xUnit tests run with Visual Studio test runner not shown in Output window

I created a fresh .NET Core Class Library project named FooBarBaz . I've then used the package manager console to run:

  • Install-Package xunit xunit
  • Install-Package xunit xunit.runners.visualstudio

This is the only code I have added:

using Xunit;
using Xunit.Abstractions;

namespace FooBarBaz
{
    public class Class1
    {
        private readonly ITestOutputHelper output;

        public Class1(ITestOutputHelper output)
        {
            this.output = output;
            output.WriteLine("OUTPUT FROM MY CONSTRUCTOR");
        }

        [Fact]
        public void SmokeTest()
        {
            output.WriteLine("OUTPUT FROM MY TEST");
            Assert.True(true);
        }
    }
}

This is based straight on the xUnit.net documentation example . I know that the documentation goes on to talk about "Message Sinks" and whatnot, but I could've sworn I saw the message in the Output window of visual studio. In my real project this seems to work only erratically.

I know I can click the "Output" hyperlink after selecting a test and see it, but that's just one step extra, and that output doesn't have a monospace font either (which I'd like to have).

See this:

没有输出的输出窗口

How do I configure xUnit to provide output in the Output window ?

After typing the question and fiddling around some more, the completely obscure solution popped up: only tests that fail show ITestOutputHelper output in the Output window.

Try changing the assertion to Assert.True(false); and you'll get this:

带输出的输出窗口

Not sure why that's the default, or how you'd change it.

What Jeroen wrote regarding ITestOutputHelper is correct, so you can't use it for "live" messages. But I have a workaround for you:

If you're debugging anyway and activated "Debug" mode, nothing stops you from doing something like

loopCount++;
System.Diagnostics.Debug.WriteLine($"Processing Entries ({loopCount} of {loopMax}) ...");

This will show up in the "Debug" window (ie in the Output window there is a dropdown where you need to select "Show output from Debug"). It appears immediately, ideal for long running loops etc.

Don't worry, those messages show up only in "Debug" configuration. To disable them, select "Release", and they won't appear any more - so it is easy to turn them on or off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM