Skip to content

Commit

Permalink
Add event to broadcast all test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Feb 15, 2017
1 parent 3f57480 commit 082dc25
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 2 deletions.
40 changes: 40 additions & 0 deletions features/docs/events/test_run_starting_event.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Feature: Test Run Starting Event

This event is fired once all test cases have been filtered, just before
the first one is executed.

See [the API documentation](http://www.rubydoc.info/github/cucumber/cucumber-ruby/Cucumber/Events/TestRunStarting) for more information about the data available on this event.

Background:
Given the standard step definitions
And a file named "features/foo.feature" with:
"""
Feature: Foo
Scenario:
Given a passing step
"""
And a file named "features/bar.feature" with:
"""
Feature: Foo
Scenario:
Given a passing step
"""
And a file named "features/support/events.rb" with:
"""
AfterConfiguration do |config|
config.on_event :test_run_starting do |event|
config.out_stream.puts "test run starting"
config.out_stream.puts event.test_cases.map(&:location)
end
end
"""

Scenario: Run the test case
When I run `cucumber -q`
Then it should pass with:
"""
test run starting
features/bar.feature:2
features/foo.feature:2
"""

1 change: 1 addition & 0 deletions lib/cucumber/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def self.registry
StepDefinitionRegistered,
StepActivated,
TestRunFinished,
TestRunStarting,
)
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/cucumber/events/test_run_starting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true
require 'cucumber/core/events'

module Cucumber
module Events

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

# @return [Array<Cucumber::Core::Test::Case>] the test cases to be executed
attr_reader :test_cases
end

end
end
1 change: 1 addition & 0 deletions lib/cucumber/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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_starting_event'
require 'cucumber/filters/prepare_world'
require 'cucumber/filters/quit'
require 'cucumber/filters/randomizer'
Expand Down
27 changes: 27 additions & 0 deletions lib/cucumber/filters/broadcast_test_run_starting_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true
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 BroadcastTestRunStartingEvent < Core::Filter.new(:config)
def initialize(config, receiver=nil)
super
@test_cases = []
end

def test_case(test_case)
@test_cases << test_case
self
end

def done
config.notify :test_run_starting, @test_cases
@test_cases.map do |test_case|
test_case.describe_to(@receiver)
end
super
self
end
end
end
end
28 changes: 28 additions & 0 deletions lib/cucumber/formatter/event_stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'securerandom'

module Cucumber
module Formatter
class EventStream

def initialize(config)
@config, @io = config, config.out_stream
@series = SecureRandom.uuid
write_event type: "start"
config.on_event :test_case_finished, -> (event) {
write_event type: "test-case-finished", location: event.test_case.location, result: event.result.to_sym.to_s
}
end

private

def write_event(attributes)
data = attributes.merge({
series: @series,
timestamp: Time.now.to_i
})
@io.puts data.to_json
end
end
end
end

5 changes: 3 additions & 2 deletions lib/cucumber/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ def filters
filters << Cucumber::Core::Test::NameFilter.new(name_regexps)
filters << Cucumber::Core::Test::LocationsFilter.new(filespecs.locations)
filters << Filters::Randomizer.new(@configuration.seed) if @configuration.randomize?
filters << Filters::Quit.new
filters << Filters::Retry.new(@configuration)
# TODO: can we just use RbLanguages's step definitions directly?
step_match_search = StepMatchSearch.new(@support_code.ruby.method(:step_matches), @configuration)
filters << Filters::ActivateSteps.new(step_match_search, @configuration)
Expand All @@ -253,6 +251,9 @@ def filters
filters << Filters::ApplyBeforeHooks.new(@support_code)
filters << Filters::ApplyAfterHooks.new(@support_code)
filters << Filters::ApplyAroundHooks.new(@support_code)
filters << Filters::BroadcastTestRunStartingEvent.new(@configuration)
filters << Filters::Quit.new
filters << Filters::Retry.new(@configuration)
# need to do this last so it becomes the first test step
filters << Filters::PrepareWorld.new(self)
end
Expand Down

0 comments on commit 082dc25

Please sign in to comment.