繁体   English   中英

如何在黄瓜中实现自定义侦听器?

[英]How to implement custom listeners in cucumber?

如何在黄瓜中实现客户听众?

哪个可以登录到控制台/报告失败方法的发生?

使用黄瓜 4.0

注意:钩子在方法级别没有帮助

黄瓜中没有像 TestNG 这样的自定义侦听器选项。 我们应该只使用 Hooks。

您可以实现 ConcurrentEventListener 以获取事件[example] 的某些挂钩。

活动可以在这里找到

您可以像下面这样在黄瓜中实现侦听器

    import io.cucumber.plugin.EventListener;
    import io.cucumber.plugin.event.*;
    
    import java.net.URI;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.UUID;
    
    public class    ReportPlugin implements EventListener {
    
     private final Map<String, UUID> startedSteps = new TreeMap<String, UUID>();
     private final Map<String, Status> finishedCases = new TreeMap<String, Status>();
    
       @Override
        public void setEventPublisher(EventPublisher publisher) {
            
            publisher.registerHandlerFor(TestStepStarted.class, this::handleTestStepStarted);
        publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);

        }
    
    private void handleTestStepStarted(TestStepStarted event) {
            startedSteps.put(event.getTestStep().toString(), event.getTestStep().getId());
            for (Map.Entry<String, UUID> entry : startedSteps.entrySet()) {
                    String location = entry.getKey();
                    UUID uuid = entry.getValue();
                    System.out.println(location + " ###fromTestStepStarted### " + uuid);
            
             //above prints
            //io.cucumber.core.runner.PickleStepTestStep@5a5c128 ###fromTestStepStarted### 7f964f1c-9442-43fc-97e9-9ec6717eb47f
           // io.cucumber.core.runner.PickleStepTestStep@77b919a3 ###fromTestStepStarted### a5d57753-aecb-40a0-a0cf-76bef7526dd8

                }
        }
}

要运行上述类 - 将类与您的步骤定义或您的支持类放在junit-platform.properties ,然后在junit-platform.properties (对于 Junit5)中提及这样的插件

cucumber.plugin = com.test.support.ReportPlugin

对于 Junit4,您可能需要将插件添加到您的跑步者类

当您运行测试时,您应该会看到控制台上打印的所有内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM