【问题标题】:How to output Cucumber background steps before each scenario?如何在每个场景之前输出 Cucumber 后台步骤?
【发布时间】:2016-05-11 04:21:36
【问题描述】:

通常,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

【问题讨论】:

    标签: ruby cucumber


    【解决方案1】:

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

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

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

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

    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
    

    假设这是在您的支持文件夹中,您可以在启动黄瓜时使用它:

    cucumber -f Cucumber::Formatter::MyFormatter
    

    【讨论】:

      【解决方案2】:

      在撰写本文时,可接受的解决方案在 Cucumber 的当前版本 2.3.3 中不起作用。后台步骤永远不会到达before_step_result。这是我为这个版本的 Cucumber 找到的最佳解决方案:

      将方法Cucumber::Formatter::LegacyApi::Adapter::FeaturePrinter.same_background_as_previous_test_case?(在黄瓜宝石的lib/cucumber/formatter/legacy_api/adapter.rb中定义)从

      def same_background_as_previous_test_case?(source)
        source.background == @previous_test_case_background
      end
      

      def same_background_as_previous_test_case?(source)
        false
      end
      

      因为FeaturePrinter 是动态定义的,所以您不能通过在features/support 的文件中定义新方法来对其进行猴子修补。您需要 fork gem,或者,如果您只需要偶尔进行此更改以进行调试,请在已安装的 gem 副本中编辑该文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 2018-07-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-07
        • 2021-12-06
        相关资源
        最近更新 更多