3

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

2 回答 2

0

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

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

  • 背景不显示
  • 场景显示他们的背景步骤

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

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
于 2013-09-27T16:17:21.210 回答
0

在撰写本文时,可接受的解决方案在 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. 您需要派生 gem,或者,如果您只需要偶尔进行此更改以进行调试,请在已安装的 gem 副本中编辑该文件。

于 2016-05-09T14:10:41.907 回答