Skip to content

Commit

Permalink
Do not by default delay the execution of test cases
Browse files Browse the repository at this point in the history
* Do not by default delay the execution of test cases until all have been
  processed through the filter chain.
* Change the name of the TestRunStarted event to TestCount event.
* Add the option --count-first which ensures that the TestCount event is
  issued before any test case is executed.
  • Loading branch information
brasmusson committed Aug 7, 2017
1 parent 10f0951 commit 808a5f1
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,55 @@ Feature: Test Run Started Event
"""
Feature: Foo
Scenario:
Given a passing step
Given a turtle
"""
And a file named "features/bar.feature" with:
"""
Feature: Foo
Scenario:
Given a passing step
Given a turtle
"""
And a step definition that looks like this:
"""ruby
Given /a turtle/ do
puts "turtle!"
end
"""
And a file named "features/support/events.rb" with:
"""
AfterConfiguration do |config|
config.on_event :test_run_started do |event|
config.out_stream.puts "test run started"
config.on_event :test_count do |event|
config.out_stream.puts "test count"
config.out_stream.puts event.test_cases.map(&:location)
end
end
"""

@todo-windows
Scenario: Run the test case
When I run `cucumber -q`
When I run `cucumber -q -f progress`
Then it should pass with:
"""
turtle!
.
turtle!
.test count
features/bar.feature:2
features/foo.feature:2
"""

@todo-windows
Scenario: Run the test case with the --count-first option
When I run `cucumber --count-first -q -f progress`
Then it should pass with:
"""
test run started
test count
features/bar.feature:2
features/foo.feature:2
turtle!
.
turtle!
.
"""

4 changes: 4 additions & 0 deletions lib/cucumber/cli/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def paths
@options[:paths]
end

def count_first?
@options[:count_first]
end

def to_hash
Hash(@options).merge(out_stream: @out_stream, error_stream: @error_stream)
end
Expand Down
1 change: 1 addition & 0 deletions lib/cucumber/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def parse!(args) # rubocop:disable Metrics/AbcSize
opts.on('-g', '--guess', 'Guess best match for Ambiguous steps.') { set_option :guess }
opts.on('-l', '--lines LINES', *lines_msg) {|lines| set_option :lines, lines }
opts.on('-x', '--expand', 'Expand Scenario Outline Tables in output.') { set_option :expand }
opts.on('--count-first', 'Do not start the execution of scenarios before the test count event has been issued') { set_option :count_first }

opts.on('--order TYPE[:SEED]', 'Run examples in the specified order. Available types:',
*<<-TEXT.split("\n")) do |order|
Expand Down
4 changes: 4 additions & 0 deletions lib/cucumber/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def filters
@options[:filters]
end

def count_first?
@options[:count_first]
end

def feature_files
potential_feature_files = with_default_features_path(paths).map do |path|
path = path.tr('\\', '/') # In case we're on windows. Globs don't work with backslashes.
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.registry
StepActivated,
TestRunFinished,
GherkinSourceRead,
TestRunStarted
TestCount
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
module Cucumber
module Events

# Event fired once all test cases have been filtered before
# the first one is executed.
class TestRunStarted < Core::Event.new(:test_cases)
# Event fired once all test cases have been filtered.
class TestCount < Core::Event.new(:test_cases)

# @return [Array<Cucumber::Core::Test::Case>] the test cases to be executed
attr_reader :test_cases
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'cucumber/filters/apply_before_hooks'
require 'cucumber/filters/apply_after_hooks'
require 'cucumber/filters/apply_around_hooks'
require 'cucumber/filters/broadcast_test_run_started_event'
require 'cucumber/filters/broadcast_test_count_event'
require 'cucumber/filters/prepare_world'
require 'cucumber/filters/quit'
require 'cucumber/filters/randomizer'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ module Cucumber
module Filters
# Added at the end of the filter chain to broadcast a list of
# all of the test cases that have made it through the filters.
class BroadcastTestRunStartedEvent < Core::Filter.new(:config)
class BroadcastTestCountEvent < Core::Filter.new(:config)
def initialize(config, receiver=nil)
super
@count_first = config.count_first?
@test_cases = []
end

def test_case(test_case)
@test_cases << test_case
test_case.describe_to(@receiver) unless @count_first
self
end

def done
config.notify :test_run_started, @test_cases
@test_cases.map do |test_case|
test_case.describe_to(@receiver)
config.notify :test_count, @test_cases
if @count_first
@test_cases.map do |test_case|
test_case.describe_to(@receiver)
end
end
super
self
Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ def filters
@configuration.filters.each do |filter|
filters << filter
end
filters << Filters::BroadcastTestCountEvent.new(@configuration)
unless configuration.dry_run?
filters << Filters::ApplyAfterStepHooks.new(@support_code)
filters << Filters::ApplyBeforeHooks.new(@support_code)
filters << Filters::ApplyAfterHooks.new(@support_code)
filters << Filters::ApplyAroundHooks.new(@support_code)
filters << Filters::BroadcastTestRunStartedEvent.new(@configuration)
filters << Filters::Quit.new
filters << Filters::Retry.new(@configuration)
# need to do this last so it becomes the first test step
Expand Down

0 comments on commit 808a5f1

Please sign in to comment.