Show output using dotnet core 1.1 and xunit tests?

I got the opportunity to create unit tests that have been very easy to test code in Visual Studio over the years. I was able to get the same unit tests to work with .NET core 1.1 in VS 2017 by creating a project Visual C#/.NET Core/Unit Test Project (.NET Core)

and then adding packages xunit

and xunit.runner.visualstudio

nuget.

The only problem is that I no longer see the Exit link when I run the test to see what I wrote in the console. Instead, I can use Trace.WriteLine

from a package System.Diagnostics

and see the result in the Output

Visual Studio window , but viewing the results associated with each test run is easier and more useful.

Is there a way to get this to display with my setting or is there another setting I could use?

Edit

@Ilya's solution works, but I need to overwrite all my tests by deleting [TestClass]

and replacing [TestMethod]

with [Fact]

, and I can no longer right click and run one test method, doing this seems to run all test methods in the class ...

+3


source to share


1 answer


The recommended xUnit way to capture the output is to use ITestOutputHelper

:

public class BaseTestClass
{
    protected ITestOutputHelper Output { get; }

    public BaseTestClass(ITestOutputHelper output)
    {
        Output = output;
    }

    public void WriteLine(string str)
    {
        Output.WriteLine(str ?? Environment.NewLine);
    }
}

public class MyTestClass : BaseTestClass
{
    public MyTestClass(ITestOutputHelper output) : base(output) {}

    [Fact]
    public void MyTest()
    {
        WriteLine("foo");
    }
}

      



Check the docs: Capturing Output .

If you used xUnit.net 1.x

, you may have previously recorded the output before Console

, Debug

or Trace

. When xUnit.net v2

shipped with parallelization enabled by default, this output capture mechanism is no longer suitable ; it is impossible to know which of the many tests that can run in parallel were responsible for these shared resources. Users migrating code from v1.x to v2.x must use one of two new methods instead.

+4


source







All Articles