C# xUnit 测试监听器

问题描述 投票:0回答:1

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

我一直在挖掘 xunit git 存储库,发现了一些使用了

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

}

现在,我知道您可以在拆卸钩子内执行此操作,但请记住,这只是一个简单的示例,我想要的是更复杂的东西。所以准确地说,我将在哪里/如何注册测试来使用这个侦听器?在 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);
    }
}

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

c# .net selenium xunit xunit.net
1个回答
6
投票

我没有使用过 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;
    }
}

然后通过

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

要使用我的示例代码,只需将类复制到您的测试项目中(您还需要添加对

xunit.runner.utility
NuGet 包的引用)。 xUnit 框架会自动发现
IRunnerReporter
——无需显式注册任何内容。

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

编辑1

我更新了

MyMessageSink
(上文)的实现,以演示如何侦听
ITestPassed
消息。我还更新了该代码片段中嵌入的链接——之前的链接是实现,但我们确实应该使用这些抽象。

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

© www.soinside.com 2019 - 2024. All rights reserved.