【问题标题】:Java+Cucumber: get the current tag/abverb being executedJava+Cucumber:获取当前正在执行的标签/动词
【发布时间】:2019-10-30 21:49:06
【问题描述】:

我正在尝试打印 Cucumber 中正在执行的当前步骤。我正在使用自定义格式化程序来打印步骤定义。但是,我还想打印正在执行的当前副词(Given、When、Then、And...)。我可能也遗漏了一些东西,在 Cucumber 中可能吗?这是我的代码:

格式化程序:

public class MyCucumberFormatter implements ConcurrentEventListener {

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestStepStarted.class, runStartedHandler);
    }

    private EventHandler<TestStepStarted> runStartedHandler = new EventHandler<TestStepStarted>() {
        @Override
        public void receive(TestStepStarted event) {
            startReport(event);
        }
    };

    private void startReport(TestStepStarted event) {
        if (!(event.testStep instanceof PickleStepTestStep)) {
            return;
        }
        PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
        log("Step: " + testStep.getStepText());
    }
}

示例场景:

Scenario: Test user life cycle: create user, activate and delete
  Given A valid admin logs in
  When Admin creates new user
  And User is activated
  Then User should successfully login

现在,它打印为:

    A valid admin logs in
    Admin creates new user
    User is activated
    User should successfully login

我希望它打印为:

鉴于有效的管理员登录
管理员创建新用户
并且用户已激活
那么用户应该成功登录

【问题讨论】:

  • 为什么不使用内置格式化程序之一?
  • @Marit 我无法使用内置格式化程序,因为我想打印当前步骤的所有日志语句。是否可以内置格式化程序?

标签: cucumber cucumber-jvm gherkin cucumber-java


【解决方案1】:

您还不能在 v4.x 中执行此操作,但您可以通过 pickleStepTestStep.getStep().getKeyWord() 在 v5.0.0-RC1 中执行此操作

【讨论】:

  • 我还想弄清楚是否有办法将我当前的信息日志添加到当前正在执行的步骤中。这在较新的版本中也可以吗?
  • 大多数日志框架都允许您附加自定义附加程序。您可能可以在插件中附加一个。不过要小心并行执行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2012-01-10
相关资源
最近更新 更多