c# - 使用 ReSharper,如何在长时间运行的单元测试期间显示调试输出?

标签 c# visual-studio unit-testing resharper xunit

我将 xUnit 与 ReSharper 测试运行器和 xUnitContrib 一起使用resharper 插件。

当我有一个长时间运行的测试时,我希望能够将一些进度指示器输出到“单元测试输出”窗口。

我已经尝试过 Debug.WriteLinesTrace.WriteLineConsole.WriteLine。所有这些都具有相同的行为 - 在测试完成之前,输出窗口中不会显示任何内容。

例如:

[Fact]
public void Test()
{
    Debug.WriteLine("A");
    Trace.WriteLine("B");
    Console.WriteLine("C");

    Thread.Sleep(10000);
}

在 10 秒过去并且测试完成之前,测试不会显示任何输出。我如何一路获得输出?

更新 1

我也尝试过使用 MSTest 和 NUnit。 NUnit 是唯一一个一路显示输出的。

在测试完成之前,MSTest 和 XUnit 不会返回任何输出。奇怪的是,虽然 XUnit 和 NUnit 测试输出看起来像这样:

A
B
C

MSTest 输出如下所示:

C


Debug Trace:

A
B

考虑到所有这些变化,我认为答案是由测试运行器实现来决定如何以及何时输出。有谁知道是否可以配置 XUnit 测试运行器?

更新 2

我想这一定是xUnitContrib的不足之处。发布到他们的 CodePlex issue tracker .

最佳答案

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.

在这里查看如何使用 xUnit.net v2 执行日志记录的示例:

http://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);
    }
}

关于c# - 使用 ReSharper,如何在长时间运行的单元测试期间显示调试输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15092438/

相关文章:

c# - 测试 Microsoft ASP.NET Web API ---- 内存中

c# - X509商店位置与商店?

c++ - pygraphviz 的 Python 3 pip 安装失败, "Microsoft Visual C++ is required",已安装 Visual Studio 2017

visual-studio - 断开 TFS 中各个文件的分支链接

c++ - 错误 2296 : '^' : illegal , 左操作数的类型为 'double'

javascript - 方法在单元测试中不可用

c# - 使用 foreach 语句删除多条记录

c# - 我应该在 T-SQL 中进行错误处理吗?

c# - 将字符串 decimal 转换为 int

PHPUnit 模拟对象和静态方法