Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not by default delay the execution of test cases #1170

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions features/docs/events/test_case_count_event.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Feature: Test Case Count Event

This event is fired only if a handler if registered for it. In that case it
is fired once all test cases have been filtered, just before the first one
is executed. Register a handler for this event delays the execution of any
tese case until all test cases have been filtered.

See [the API documentation](http://www.rubydoc.info/github/cucumber/cucumber-ruby/Cucumber/Events/TestCaseCount) 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 turtle
"""
And a file named "features/bar.feature" with:
"""
Feature: Foo
Scenario:
Given a turtle
"""
And a step definition that looks like this:
"""ruby
Given /a turtle/ do
puts "turtle!"
end
"""
And a step definition that looks like this:
"""ruby
Then /no test case count event has been sent/ do
puts "turtle!"
end
"""

@todo-windows
Scenario: Run the test case with a handler registered for the test case count event
Given a file named "features/support/events.rb" with:
"""
AfterConfiguration do |config|
config.on_event :test_case_count do |event|
config.out_stream.puts "test case count"
config.out_stream.puts event.test_cases.map(&:location)
end
end
"""
When I run `cucumber -q -f progress`
Then it should pass with:
"""
test case count
features/bar.feature:2
features/foo.feature:2

turtle!
.
turtle!
.
"""

41 changes: 0 additions & 41 deletions features/docs/events/test_run_started_event.feature

This file was deleted.

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
TestCaseCount
)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# coding: utf-8
# 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 TestRunStarted < Core::Event.new(:test_cases)
# Event fired once all test cases have been filtered.
class TestCaseCount < 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_case_count_event'
require 'cucumber/filters/prepare_world'
require 'cucumber/filters/quit'
require 'cucumber/filters/randomizer'
Expand Down
44 changes: 44 additions & 0 deletions lib/cucumber/filters/broadcast_test_case_count_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'cucumber/core/filter'

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 BroadcastTestCaseCountEvent < Core::Filter.new(:config)
def initialize(config, receiver=nil)
super
end

def test_case(test_case)
if handlers_exist?
test_cases << test_case
else
test_case.describe_to(@receiver)
end
self
end

def done
if handlers_exist?
config.notify :test_case_count, test_cases
test_cases.map do |test_case|
test_case.describe_to(@receiver)
end
end
super
self
end

private

def handlers_exist?
@handlers_exist ||= config.event_bus.handlers_exist_for?(:test_case_count)
end

def test_cases
@test_cases ||= []
end
end
end
end
27 changes: 0 additions & 27 deletions lib/cucumber/filters/broadcast_test_run_started_event.rb

This file was deleted.

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::BroadcastTestCaseCountEvent.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
49 changes: 49 additions & 0 deletions spec/cucumber/filters/broadcast_test_case_count_event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true
require 'cucumber/filters/broadcast_test_case_count_event'

describe Cucumber::Filters::BroadcastTestCaseCountEvent do
subject(:filter) { Cucumber::Filters::BroadcastTestCaseCountEvent.new(config, receiver) }

let(:event_bus) { double(:event_bus) }
let(:config) { double(:config) }
let(:receiver) { double(:receiver) }

let(:test_case) { double(:test_case) }

before do
allow(config).to receive(:event_bus).and_return(event_bus)
end

context 'without handler registered for the test case count event' do
before(:each) do
allow(event_bus).to receive(:handlers_exist_for?).with(:test_case_count).and_return(false)
end

it 'test cases are passed on immediately' do
expect(test_case).to receive(:describe_to).with(receiver)

filter.test_case(test_case)
end
end

context 'with handler registered for the test case count event' do
before(:each) do
allow(event_bus).to receive(:handlers_exist_for?).with(:test_case_count).and_return(true)
end

it 'test cases are not passed on immediately' do
expect(test_case).not_to receive(:describe_to).with(receiver)

filter.test_case(test_case)
end

it 'test case count event is issued before the the test cases are passed on' do
expect(config).to receive(:notify).with(:test_case_count, [test_case]).ordered
expect(test_case).to receive(:describe_to).with(receiver).ordered
expect(receiver).to receive(:done).ordered

filter.test_case(test_case)
filter.done
end
end
end