简体   繁体   中英

How to output result when invoked by xUnit?

For example, there is a class to be tested.

public class ToBeTested
{
    public static void Method1()
    {
        Debug.WriteLine("....for debugging....");
    }
}

And It's invoked by xUnit test method

[Fact]
public void Test1()
{
    ToBeTested.Method1();
}

However, the line Debug.WriteLine("....for debugging...."); didn't write anything in Visual Studio debug output?

The line Debug.WriteLine("....for debugging...."); will write the output to the Debug Output window of visual studio only when the tests are ran in Debug mode. Instead of "Run Tests" you can use "Debug Tests" and can see the output in window.

But if you are trying to output results while running XUnit tests, then it is better to use ITestOutputHelper in namespace Xunit.Abstractions .

Code sample is available in : https://xunit.github.io/docs/capturing-output.html

using Xunit;
using Xunit.Abstractions;

public class MyTestClass
{
    private readonly ITestOutputHelper output;

    public MyTestClass(ITestOutputHelper output)
    {
        this.output = output;
    }

    [Fact]
    public void MyTest()
    {
        var temp = "my class!";
        output.WriteLine("This is output from {0}", temp);
    }
}

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