【问题标题】:xUnit doesn't write message to the output panexUnit 不会将消息写入输出窗格
【发布时间】:2015-10-23 03:01:13
【问题描述】:

在 Visual Studio 2015 社区中,我有一个示例 ASP.NET 5 (vNext) 项目和一个带有单元测试的项目 (xUnit.net)。 DNX 的版本是 1.0.0-beta5。我的目标是在测试运行期间将消息添加到输出窗格。 Here我采取了这样的方式,所以我的单元测试代码是这样的:

using Xunit;
using Xunit.Abstractions;

namespace UnitTests
{

    public class UnitTest1
    {
        ITestOutputHelper output;

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

        [Fact]
        public void TestTestTest()
        {
            output.WriteLine("Test Message");
            Assert.Equal(2, 2);
        }

    }
}

Visual Studio 测试资源管理器发现了这个测试(没关系),但我在 输出 窗格(来自测试)中只有:

------ Run test started ------
------ Test started: Project: UnitTests ------
Starting  Microsoft.Framework.TestHost [C:\Users\*******\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta5\bin\dnx.exe --appbase "C:\Users\*******\Documents\Visual Studio 2015\Projects\MvcMovie\UnitTests" Microsoft.Framework.ApplicationHost --port 55837 Microsoft.Framework.TestHost --port 55893]
Connected to Microsoft.Framework.TestHost
Running tests in 'C:\Users\*******\Documents\Visual Studio 2015\Projects\MvcMovie\UnitTests\project.json'
========== Run test finished: 1 run (0:00:03,2267169) ==========

另外,在选择的测试运行信息下没有任何链接“输出”,如下所示: (仅“测试通过...经过的时间...”)

我应该怎么做才能使ITestOutputHelper 工作?

【问题讨论】:

  • 我也遇到了同样的问题,一直没能找到解决办法。
  • @TimothyShields 但是,当测试失败时,它会出现。但如果它通过则不会(如图所示......)
  • 有趣...你的意思是你确实得到了失败测试的输出?
  • @TimothyShields 是的,在同一个测试中,如果我写 Assert.Equal(2, 3),我会得到一个链接“输出”。单击它会出现带有“标准输出”文本块的新选项卡。
  • 它不为成功的测试保存输出实际上可能是设计使然。

标签: c# visual-studio-2015 xunit.net dnx


【解决方案1】:

此解决方案适用于我(Visual Studio 2017 和 xUnit 2.2.0.3545)。

尝试在App.Config 文件中添加以下配置。 (我不知道为什么。它只是需要。如果它不存在,只需在您的测试项目中添加一个新的。)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="xunit.diagnosticMessages" value="true"/>
  </appSettings>
</configuration>

输出信息将按预期写入Test 输出,如下所示。

[xUnit.net 00:00:00.0853907]   Starting:    xxxAssemblyName (parallel test collections = on, max threads = 8)
[xUnit.net 00:00:00.1689373]     Example.xxxTestClassName [PASS]
[xUnit.net 00:00:00.1697265]       Output:
[xUnit.net 00:00:00.1700698]         xxxx your Test Message
[xUnit.net 00:00:00.1797303]   Finished:    xxxAssemblyName

【讨论】:

  • 注意: 基于 XML 的配置有 deprecated,如果可能,现在使用 JSON-based configuration
  • xunit.runner.json: { "$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json", "appDomain": "ifAvailable", "diagnosticMessages": true, "internalDiagnosticMessages": false, "longRunningTestSeconds": 4, "maxParallelThreads": 8, "methodDisplay": "classAndMethod", "methodDisplayOptions": "none", "parallelizeAssembly": true, "parallelizeTestCollections": true, "preEnumerateTheories": true, "shadowCopy": true }
  • 您的回答有效,谢谢!。 n.b.: 我正在使用上面的xunit.runner.json 文件,设置"diagnosticMessages": true
  • 记得将文件 xunit.runner.json 设置为 CopyAlways,这让我转了一会儿
  • @TGP1994 - 感谢您提供的信息,是的,现在两个链接都已断开。但我在这里找到了新链接:deprecated XML-based configurationJSON-based configuration。希望对您有所帮助。
【解决方案2】:

正如xUnit GitHub page 中解释的那样,我使用了

私有只读 ITestOutputHelper 输出;

这对我有用。

【讨论】:

  • 别忘了将它添加到测试类构造函数中——它会被注入
【解决方案3】:

我在使用 NUnit 很长时间后第一次使用 xUnit 时也遇到了这个问题。

令人惊讶的是,xUnit 不直接支持控制台输出,但确实提供了一些其他方法来实现相同的目标。

https://xunit.github.io/docs/capturing-output.html

如果您没有真正投资于 xUnit,我想说最简单的做法就是使用 NUnit 或 MSTest,因为两者都支持简单的 console.writeline

【讨论】:

    【解决方案4】:

    仅对于 ITestOutputHelper 实现,以下工作:

    作为reported by Joe.wang,必须将{add key="xunit.diagnosticMessages" value="true"}放到单元测试项目的App.config中。

    作为reported by stefano,您必须将 ITestOutputHelper 类型的本地成员添加到测试类。并且必须将其添加到构造函数的参数中,并将注入的对象分配给本地成员。

    您应该在使用输出方法检测后重建。

    你必须有一个失败的测试。

    您对输出方法的调用应该失败的测试之前。

    每个测试都有效!不适用于测试套件。

    【讨论】:

      【解决方案5】:

      只需使用Console.Write()Console.WriteLine()

      ReSharper 与此挂钩,您将在测试结果中看到输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        • 1970-01-01
        • 2019-10-29
        相关资源
        最近更新 更多