C# xUnit 测试侦听器

Nic*_*ick 6 .net c# selenium xunit xunit.net

我正在构建一个基于 .Net Core 的 selenium 测试框架,团队决定使用 xUnit。一切顺利,一切顺利,但一段时间以来,我们一直在尝试复制 Java TestNG 侦听器的功能,但运气不佳。

我一直在挖掘 xunit git repo 并发现了一些ITestListener使用此类接口的实例。在深入挖掘之后,我发现这些侦听器来自一个名为的包TestDriven.Framework,我想确切地知道我将如何使用使用这些接口创建的测试侦听器?

到目前为止,这是我的简单测试侦听器,它应该在测试失败时编写一些内容:

public class Listener
{
    readonly int totalTests;

    public Listener(ITestListener listener, int totalTests)
    {
        this.totalTests = totalTests;
        TestListener = listener;
        TestRunState = TestRunState.NoTests;
    }

    public ITestListener TestListener { get; private set; }
    public TestRunState TestRunState { get; set; }

    public void onTestFail(ITestFailed args)
    {
        Console.WriteLine(args.Messages);
    }

}
Run Code Online (Sandbox Code Playgroud)

现在,我知道您可以在拆卸钩子中执行此操作,但请记住,这只是一个简单的示例,我想到的是更复杂的内容。因此,准确地说,我将在哪里/如何注册测试以使用此侦听器?在 Java TestNg 中我会,@Listeners但在 C# 中我不太确定。

编辑 1以便示例工作并设法将其添加到我自己的项目结构中,但是当我尝试使用它时

    class TestPassed : TestResultMessage, ITestPassed
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TestPassed"/> class.
    /// </summary>
    public TestPassed(ITest test, decimal executionTime, string output)
        : base(test, executionTime, output) {
        Console.WriteLine("Execution time was an awesome " + executionTime);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在注册这个时遇到问题,或者我是否正确注册了它。就示例而言,我找到了实际的消息接收器,但也找到了我不确定如何使用的实际测试状态数据。

xan*_*der 5

我没有使用过 TestNG,但我做了一些快速阅读,我我明白你在追求什么。

为了演示,我实现了一个非常基本的概念验证 xUnit IMessageSink

public class MyMessageSink : IMessageSink
{
    public bool OnMessage(IMessageSinkMessage message)
    {
        // Do what you want to in response to events here.
        // 
        // Each event has a corresponding implementation of IMessageSinkMessage.
        // See examples here: https://github.com/xunit/abstractions.xunit/tree/master/src/xunit.abstractions/Messages
        if (message is ITestPassed)
        {
            // Beware that this message won't actually appear in the Visual Studio Test Output console.
            // It's just here as an example. You can set a breakpoint to see that the line is hit.
            Console.WriteLine("Execution time was an awesome " + ((ITestPassed)message).ExecutionTime);
        }

        // Return `false` if you want to interrupt test execution.
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式注册接收器IRunnerReporter

public class MyRunnerReporter : IRunnerReporter
{
    public string Description => "My custom runner reporter";

    // Hard-coding `true` means this reporter will always be enabled.
    //
    // You can also implement logic to conditional enable/disable the reporter.
    // Most reporters based this decision on an environment variable.
    // Eg: https://github.com/xunit/xunit/blob/cbf28f6d911747fc2bcd64b6f57663aecac91a4c/src/xunit.runner.reporters/TeamCityReporter.cs#L11
    public bool IsEnvironmentallyEnabled => true;

    public string RunnerSwitch => "mycustomrunnerreporter";

    public IMessageSink CreateMessageHandler(IRunnerLogger logger)
    {
        return new MyMessageSink();
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用我的示例代码,只需将类复制到您的测试项目中(您还需要添加对xunit.runner.utilityNuGet 包的引用)。xUnit 框架会自动发现IRunnerReporter--no 需要显式注册任何东西。

如果这看起来朝着正确的方向发展,您可以在 xUnit 源代码中找到更多信息。所有涉及的接口都有详细的文档记录。xunit.runner.reporters 命名空间中有一些现有的实现。AssemblyRunner.cs还演示了一种将不同事件类型分派给各个处理程序的可能方法。

编辑 1

我已经更新了MyMessageSink(上面)的实现来演示你可以如何监听ITestPassed消息。我还更新了嵌入在该代码片段中的链接——前一个链接是实现,但我们真的应该使用这些抽象。

if (message is IMessageType)模式非常粗糙,如果您想收听许多不同的消息类型,则无法很好地扩展。由于我不知道您的需求,因此我只采用了可能可行的最简单的方法——希望您可以改进/扩展它以满足您的需求。