Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show output using dotnet core 1.1 and xunit unit tests?

I've fount creating unit tests a super easy way to try out code in Visual Studio over the years. I was able to get the same type of unit tests to work with .NET core 1.1 in VS 2017 by creating a Visual C#/.NET Core/Unit Test Project (.NET Core) project, then adding xunit and xunit.runner.visualstudio nuget packages.

The only problem is that I no longer see the 'Output' link when the test is run to see what I wrote to the console. I can use Trace.WriteLine from the System.Diagnostics package instead and see the output in the Output window of Visual Studio, but seeing the output associated with each test run is easier and more useful.

Is there a way to get that to display with my setup or is there a different setup I could use?

Edit

@ilya's solution works, but I have to re-write all my tests, removing [TestClass] and replacing [TestMethod] with [Fact], and I can no longer right-click and run a single test method, doing that seems to run all test methods in the class...

like image 885
Jason Goemaat Avatar asked May 17 '17 01:05

Jason Goemaat


People also ask

Does xUnit work with .NET core?

The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and . NET Framework 4.5.

How do you write xUnit test cases in .NET core?

To write a test you simply create a public method that returns nothing and then decorate it with the Fact attribute. Inside that method you can put whatever code you want but, typically, you'll create some object, do something with it and then check to see if you got the right result using a method on the Assert class.

How do I create a test report in xUnit?

One way to do it: add " --logger=trx" to the "dotnet test" command and then use the build step "Process xUnit test result report" from the "xUnit plugin". Use the option "MSTest-Version N/A (default) Pattern" and set pattern to "*/. trx". This is the answer!

What dotnet command executes xUnit tests?

The dotnet test command builds the solution and runs a test host application for each test project in the solution. The test host executes tests in the given project using a test framework, for example: MSTest, NUnit, or xUnit, and reports the success or failure of each test.


1 Answers

Recommended xUnit way to capture 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 been writing output to Console, Debug, or Trace. When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing to those shared resources. Users who are porting code from v1.x to v2.x should use one of the two new methods instead.

like image 188
Ilya Chumakov Avatar answered Oct 25 '22 22:10

Ilya Chumakov