简体   繁体   English

由xUnit调用时如何输出结果?

[英]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 并且由xUnit测试方法调用

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

However, the line Debug.WriteLine("....for debugging...."); 但是,该行Debug.WriteLine("....for debugging...."); didn't write anything in Visual Studio debug output? 在Visual Studio调试输出中没有写任何东西?

The line Debug.WriteLine("....for debugging...."); 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. 仅当测试以“调试”模式运行时,才会将输出写入Visual Studio的“调试输出”窗口。 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 . 但是,如果尝试在运行XUnit测试时输出结果,则最好在名称空间Xunit.Abstractions使用ITestOutputHelper

Code sample is available in : https://xunit.github.io/docs/capturing-output.html 代码示例位于: 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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