运行 xunit 测试时无法将输出打印到控制台窗口

Joe*_*uly 4 c# xunit

public class test2InAnotherProject
{
    private readonly ITestOutputHelper output;

    public test2InAnotherProject(ITestOutputHelper output)
    {
        this.output = output;
    }
    int Diff(int a, int b)
    {
        return (a - b);
    }
    int Div(int a, int b)
    {
        return (b / a);
    }

    [Fact]
    public void Test2()
    {
        int a = 2, b = 4;

        output.WriteLine("Test1: Project 2 in old library");
        int c = Diff(a, b);
        Assert.Equal(c, (a - b));
        output.WriteLine("Test1: Asssert done Project 2 in old library");
    }

    [Fact]
    public void Test3()
    {
        int a = 2, b = 4;

        output.WriteLine("Test2: Project 2 in old library");
        int c = Div(a, b);
        Assert.Equal(c, (float)((b / a)));
        output.WriteLine("Test2: Assert done Project 2 in old library");
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试在使用命令通过命令提示符运行测试时打印这些行

dotnet 测试 --no-build

尝试过Console.Writeline,之后我尝试使用Output.WriteLine. 即使我从 Visual Studio 运行,也无法在输出窗口中打印这些行。

dri*_*zzd 9

确实没有输出Console.WriteLine。并且ITestOutputHelper输出未显示在“输出”窗口中。相反,当您在Test Explorer 中单击测试时,会出现一个输出链接。单击该链接以查看输出。

要在命令行上显示测试输出,请使用dotnet test --logger "console;verbosity=detailed".

在此处输入图片说明


Man*_*ari -1

请注意,您可以使用 ITestOutputHelper 并且它应该写入输出。

参阅此文档了解更多详细信息。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)