3

Normally, Cucumber will output background steps to look the same as you defined them in your feature file (once at the top).

$ 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

It would be much easier to see when a background step is executed successfully if you could always display the steps at the beginning of a scenario. How can I enable this behavior?

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
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Andrew
  • 227,796
  • 193
  • 515
  • 708

2 Answers2

0

You will have to create a custom formatter.

Assuming you want something like the pretty formatter, you can create class that inherits from the pretty formatter and replaces the required methods. Basically, you would want to change the logic that:

  • The background is not displayed
  • Scenarios show their background steps

This formatter appears to work:

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

Assuming this is in your support folder, you can use it when start cucumber:

cucumber -f Cucumber::Formatter::MyFormatter
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
0

The accepted solution doesn't work in the current version of Cucumber at this writing, 2.3.3. Background steps never make it to before_step_result. Here's the best solution I found for this version of Cucumber:

Change the method Cucumber::Formatter::LegacyApi::Adapter::FeaturePrinter. same_background_as_previous_test_case? (defined in the cucumber gem's lib/cucumber/formatter/legacy_api/adapter.rb) from

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

to

def same_background_as_previous_test_case?(source)
  false
end

Because FeaturePrinter is defined dynamically, you can't monkey-patch it by defining the new method in a file in features/support. You need to fork the gem or, if you only need to make this change occasionally for debugging, edit the file in your installed copy of the gem.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121