如何在每个场景之前输出Cucumber后台步骤?

2024-01-29

通常,Cucumber 将输出后台步骤,使其看起来与您在功能文件中定义的相同(位于顶部)。

$ bundle exec cucumber --color --format pretty

Feature: Something

  Background:
    Given step 1
    And step 2

  Scenario: a scenario
    When I do step 3
    Then it works

  Scenario: another scenario
    When I do a different step 3
    Then it works

如果您始终可以在场景开始时显示步骤,那么就可以更容易地看到后台步骤何时成功执行。我怎样才能启用这种行为?

Feature: Something

  Scenario: a scenario
    Given step 1
    And step 2
    When I do step 3
    Then it works

  Scenario: another scenario
    Given step 1
    And step 2
    When I do a different step 3
    Then it works

您必须创建一个自定义格式化程序。

假设您想要类似漂亮格式化程序的东西,您可以创建继承自漂亮格式化程序的类并替换所需的方法。基本上,您需要更改以下逻辑:

  • 背景不显示
  • 场景显示其背景步骤

该格式化程序似乎可以工作:

require 'cucumber/formatter/pretty'

module Cucumber
  module Formatter
    class MyFormatter < Pretty

      def background_name(keyword, name, file_colon_line, source_indent)        
        # Do nothing
      end

      def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
        @hide_this_step = false
        if exception
          if @exceptions.include?(exception)
            return
          end
          @exceptions << exception
        end
        if @in_background
          @hide_this_step = true
          return
        end
        @status = status
      end

    end # MyFormatter
  end # Formatter
end # Cucumber

假设它在您的 support 文件夹中,您可以在启动 cucumber 时使用它:

cucumber -f Cucumber::Formatter::MyFormatter
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在每个场景之前输出Cucumber后台步骤? 的相关文章

随机推荐