【问题标题】:not able to print output to console window while running xunit tests运行 xunit 测试时无法将输出打印到控制台窗口
【发布时间】:2020-05-30 20:44:07
【问题描述】:
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");
    }
}

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

dotnet 测试 --no-build

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

【问题讨论】:

    标签: c# xunit


    【解决方案1】:

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

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

    【讨论】:

    • 我正在使用 xunit 运行器类并制作另一个应用程序来运行通过的 dll 测试。 test2InAnotherProject 的 dll 被传递给 xunit 运行器实现,它在控制台窗口中运行 dll。所以我会在应用程序运行时输出到控制台窗口。
    【解决方案2】:

    package Manager 控制台 或 powershell 控制台 ISE 等 powershell 控制台中运行 dotnete test,您可以获得 Xunit 项目的所有 Console.WriteLine 输出。

    powershel ISE 中,运行此脚本:

    cls
    cd 'path/to/project/test/folder'
    dotnet test
    

    源代码中的任何Console.WriteLine(..) 也会显示在 PS 控制台中。

    【讨论】:

      【解决方案3】:

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

      refer this documentation了解更多详情。

      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);
          }
      }
      

      【讨论】:

      • 根据我的代码。我已经在使用 ITestOutputHelper 仍然无法正常工作。
      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 2014-03-19
      相关资源
      最近更新 更多