C#xUnit测试监听器
我正在构建一个基于.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); } } 现在,我知道你可以在一个撕裂的钩子里做到这一点,但请记住,这只是一个简单的例子,我想到的是更复杂的东西.那么准确地说,我将在何处/如何注册测试以使用此侦听器?在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); } } 我无法注册这个,或者我甚至正在注册它.就示例而言,我已经找到了实际的消息接收器,但也找到了我不确定如何使用的实际测试状态数据. 解决方法
我没有和TestNG合作,但是我做了一些快速阅读,我想我看到了你所追求的.
为了演示,我实现了一个非常基本的概念验证xUnit 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; } } 然后通过 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 namespace中有一些现有的实现.AssemblyRunner.cs还演示了一种可能的方法,用于将不同的事件类型分派给各个处理程序. 编辑1 我已经更新了MyMessageSink(上面)的实现,以演示如何监听ITestPassed消息.我还更新了代码片段中嵌入的链接 – 前一个链接是实现,但我们应该真正使用这些抽象. if(消息是IMessageType)模式非常粗糙,如果您想要侦听许多不同的消息类型,则无法很好地扩展.由于我不了解您的需求,我只是选择了最简单的方法 – 希望您可以改进/扩展它以满足您的需求. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |