Skip to content

Commit

Permalink
Change the JUnit formatter to use events instead of callbacks.
Browse files Browse the repository at this point in the history
Also use new style formatter initializer in the JUnit formatter.
Let the runtime handle that not all formatters write to a stream or
file.
  • Loading branch information
brasmusson committed Mar 30, 2016
1 parent 84a498f commit 0845763
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
5 changes: 4 additions & 1 deletion features/docs/formatters/junit_formatter.feature
@@ -1,4 +1,3 @@
@spawn
Feature: JUnit output formatter
In order for developers to create test reports with ant
Cucumber should be able to output JUnit xml files
Expand Down Expand Up @@ -60,6 +59,7 @@ Feature: JUnit output formatter
| is undefined |
"""

@spawn
Scenario: one feature, one passing scenario, one failing scenario
When I run `cucumber --format junit --out tmp/ features/one_passing_one_failing.feature`
Then it should fail with:
Expand Down Expand Up @@ -101,6 +101,7 @@ Feature: JUnit output formatter
"""

@spawn
Scenario: one feature in a subdirectory, one passing scenario, one failing scenario
When I run `cucumber --format junit --out tmp/ features/some_subdirectory/one_passing_one_failing.feature --require features`
Then it should fail with:
Expand Down Expand Up @@ -245,6 +246,7 @@ can't convert .* into String \(TypeError\)
You *must* specify --out DIR for the junit formatter
"""

@spawn
Scenario: strict mode, one feature, one scenario outline, four examples: one passing, one failing, one pending, one undefined
When I run `cucumber --strict --format junit --out tmp/ features/scenario_outline.feature`
Then it should fail with:
Expand Down Expand Up @@ -326,6 +328,7 @@ You *must* specify --out DIR for the junit formatter
"""

@spawn
Scenario: strict mode with --expand option, one feature, one scenario outline, four examples: one passing, one failing, one pending, one undefined
When I run `cucumber --strict --expand --format junit --out tmp/ features/scenario_outline.feature`
Then it should fail with exactly:
Expand Down
28 changes: 18 additions & 10 deletions lib/cucumber/formatter/junit.rb
@@ -1,4 +1,5 @@
require 'builder'
require 'cucumber/formatter/backtrace_filter'
require 'cucumber/formatter/io'
require 'cucumber/formatter/interceptor'
require 'fileutils'
Expand All @@ -16,12 +17,17 @@ def initialize(feature_file)
end
end

def initialize(_runtime, io, options)
@reportdir = ensure_dir(io, "junit")
@options = options
def initialize(config)
config.on_event :before_test_case, &method(:on_before_test_case)
config.on_event :after_test_case, &method(:on_after_test_case)
config.on_event :after_test_step, &method(:on_after_test_step)
config.on_event :finished_testing, &method(:on_finished_testing)
@reportdir = ensure_dir(config.out_stream, "junit")
@config = config
end

def before_test_case(test_case)
def on_before_test_case(event)
test_case = event.test_case
unless same_feature_as_previous_test_case?(test_case.feature)
end_feature if @current_feature
start_feature(test_case.feature)
Expand All @@ -33,13 +39,15 @@ def before_test_case(test_case)
@interceptederr = Interceptor::Pipe.wrap(:stderr)
end

def after_test_step(test_step, result)
def on_after_test_step(event)
return if @failing_step_source

@failing_step_source = test_step.source.last unless result.ok?(@options[:strict])
@failing_step_source = event.test_step.source.last unless event.result.ok?(@config.strict?)
end

def after_test_case(test_case, result)
def on_after_test_case(event)
test_case = event.test_case
result = event.result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
test_case_name = NameBuilder.new(test_case)
scenario = test_case_name.scenario_name
scenario_designation = "#{scenario}#{test_case_name.name_suffix}"
Expand All @@ -50,7 +58,7 @@ def after_test_case(test_case, result)
Interceptor::Pipe.unwrap! :stderr
end

def done
def on_finished_testing(event)
end_feature if @current_feature
end

Expand Down Expand Up @@ -86,7 +94,7 @@ def end_feature

def create_output_string(test_case, scenario, result, row_name)
output = "#{test_case.keyword}: #{scenario}\n\n"
return output if result.ok?(@options[:strict])
return output if result.ok?(@config.strict?)
if test_case.keyword == "Scenario"
output += "#{@failing_step_source.keyword}" unless hook?(@failing_step_source)
output += "#{@failing_step_source.name}\n"
Expand All @@ -107,7 +115,7 @@ def build_testcase(result, scenario_designation, output)
name = scenario_designation

@builder.testcase(:classname => classname, :name => name, :time => "%.6f" % duration) do
if !result.passed? && result.ok?(@options[:strict])
if !result.passed? && result.ok?(@config.strict?)
@builder.skipped
@skipped += 1
elsif !result.passed?
Expand Down
4 changes: 2 additions & 2 deletions lib/cucumber/runtime.rb
Expand Up @@ -195,8 +195,8 @@ def formatters

def create_formatter(factory, path_or_io, options)
if !legacy_formatter?(factory)
out_stream = Cucumber::Formatter::Io.ensure_io(path_or_io)
return factory.new(@configuration.with_options(out_stream: out_stream))
return factory.new(@configuration) if path_or_io.nil?
return factory.new(@configuration.with_options(out_stream: path_or_io))
end
results = Formatter::LegacyApi::Results.new
runtime_facade = Formatter::LegacyApi::RuntimeFacade.new(results, @support_code, @configuration)
Expand Down
4 changes: 2 additions & 2 deletions spec/cucumber/formatter/junit_spec.rb
Expand Up @@ -22,7 +22,7 @@ def write_file(feature_filename, data)
context "With no options" do
before(:each) do
allow(File).to receive(:directory?) { true }
@formatter = TestDoubleJunitFormatter.new(runtime, '', {})
@formatter = TestDoubleJunitFormatter.new(actual_runtime.configuration.with_options(out_stream: ''))
end

after(:each) do
Expand Down Expand Up @@ -200,7 +200,7 @@ def after_step(step)
let(:runtime) { Runtime.new({:expand => true}) }
before(:each) do
allow(File).to receive(:directory?) { true }
@formatter = TestDoubleJunitFormatter.new(runtime, '', {:expand => true})
@formatter = TestDoubleJunitFormatter.new(actual_runtime.configuration.with_options(out_stream: '', :expand => true))
end

after(:each) do
Expand Down

0 comments on commit 0845763

Please sign in to comment.