-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a summary formatter
- Loading branch information
Showing
10 changed files
with
205 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
Feature: Spec formatter | ||
|
||
This formatter mimics the output from tools like RSpec or Mocha, giving an | ||
overview of each feature and scenario but omitting the steps. | ||
|
||
Background: | ||
Given the standard step definitions | ||
|
||
Scenario: A couple of scenarios | ||
Given a file named "features/test.feature" with: | ||
""" | ||
Feature: Test | ||
Scenario: Passing | ||
Given this step passes | ||
Scenario: Failing | ||
Given this step fails | ||
""" | ||
When I run `cucumber --format summary` | ||
Then it should fail with exactly: | ||
""" | ||
Test | ||
Passing ✓ | ||
Failing ✗ | ||
Failing Scenarios: | ||
cucumber features/test.feature:5 # Scenario: Failing | ||
2 scenarios (1 failed, 1 passed) | ||
2 steps (1 failed, 1 passed) | ||
0m0.012s | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'cucumber/formatter/console' | ||
|
||
module Cucumber | ||
module Formatter | ||
class ConsoleCounts | ||
include Console | ||
|
||
def initialize(config) | ||
@test_case_summary = Core::Test::Result::Summary.new | ||
@test_step_summary = Core::Test::Result::Summary.new | ||
|
||
config.on_event :test_case_finished do |event| | ||
event.result.describe_to @test_case_summary | ||
end | ||
|
||
config.on_event :test_step_finished do |event| | ||
event.result.describe_to @test_step_summary if from_gherkin?(event.test_step) | ||
end | ||
end | ||
|
||
def to_s | ||
[ | ||
[scenario_count, status_counts(@test_case_summary)].compact.join(" "), | ||
[step_count, status_counts(@test_step_summary)].compact.join(" ") | ||
].join("\n") | ||
end | ||
|
||
private | ||
|
||
def from_gherkin?(test_step) | ||
test_step.source.last.location.file.match(/\.feature$/) | ||
end | ||
|
||
def scenario_count | ||
count = @test_case_summary.total | ||
"#{count} scenario" + (count == 1 ? "" : "s") | ||
end | ||
|
||
def step_count | ||
count = @test_step_summary.total | ||
"#{count} step" + (count == 1 ? "" : "s") | ||
end | ||
|
||
def status_counts(summary) | ||
counts = [:failed, :skipped, :undefined, :pending, :passed].map { |status| | ||
count = summary.total(status) | ||
[status, count] | ||
}.select { |status, count| | ||
count > 0 | ||
}.map { |status, count| | ||
format_string("#{count} #{status}", status) | ||
} | ||
"(#{counts.join(", ")})" if counts.any? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'cucumber/formatter/console' | ||
|
||
module Cucumber | ||
module Formatter | ||
class ConsoleIssues | ||
include Console | ||
|
||
def initialize(config) | ||
@failures = [] | ||
@config = config | ||
@config.on_event(:test_case_finished) do |event| | ||
@failures << event.test_case if event.result.failed? | ||
end | ||
end | ||
|
||
def to_s | ||
return if @failures.empty? | ||
result = [ format_string("Failing Scenarios:", :failed) ] + @failures.map { |failure| | ||
source = @config.source? ? format_string(" # #{failure.keyword}: #{failure.name}", :comment) : '' | ||
format_string("cucumber #{profiles_string}" + failure.location, :failed) + source | ||
} | ||
result.join("\n") | ||
end | ||
|
||
def any? | ||
@failures.any? | ||
end | ||
|
||
private | ||
|
||
def profiles_string | ||
return if @config.custom_profiles.empty? | ||
@config.custom_profiles.map { |profile| "-p #{profile}" }.join(' ') + ' ' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,57 @@ | ||
require 'cucumber/formatter/io' | ||
require 'cucumber/formatter/console' | ||
require 'cucumber/formatter/console_counts' | ||
require 'cucumber/formatter/console_issues' | ||
require 'cucumber/core/test/result' | ||
|
||
module Cucumber | ||
module Formatter | ||
module Summary | ||
|
||
def scenario_summary(runtime, &block) | ||
scenarios_proc = lambda{|status| elements = runtime.scenarios(status)} | ||
scenario_count_proc = element_count_proc(scenarios_proc) | ||
dump_summary_counts(runtime.scenarios.length, scenario_count_proc, "scenario", &block) | ||
end | ||
# Summary formatter, outputting only feature / scenario titles | ||
class Summary | ||
include Io | ||
include Console | ||
|
||
def step_summary(runtime, &block) | ||
steps_proc = lambda{|status| runtime.steps(status)} | ||
step_count_proc = element_count_proc(steps_proc) | ||
dump_summary_counts(runtime.steps.length, step_count_proc, "step", &block) | ||
end | ||
def initialize(config) | ||
@config, @io = config, ensure_io(config.out_stream) | ||
@counts = ConsoleCounts.new(@config) | ||
@issues = ConsoleIssues.new(@config) | ||
@start_time = Time.now | ||
|
||
def dump_summary_counts(total_count, status_proc, what, &block) | ||
dump_count(total_count, what) + dump_status_counts(status_proc, &block) | ||
@config.on_event :test_case_starting do |event| | ||
print_feature event.test_case | ||
print_test_case event.test_case | ||
end | ||
|
||
@config.on_event :test_case_finished do |event| | ||
print_result event.result | ||
end | ||
|
||
@config.on_event :test_run_finished do |event| | ||
duration = Time.now - @start_time | ||
@io.puts | ||
print_statistics(duration, @config, @counts, @issues) | ||
end | ||
end | ||
|
||
private | ||
|
||
def element_count_proc(find_elements_proc) | ||
lambda { |status| | ||
elements = find_elements_proc.call(status) | ||
elements.any? ? elements.length : 0 | ||
} | ||
def print_feature(test_case) | ||
feature = test_case.feature | ||
return if @current_feature == feature | ||
@io.puts unless @current_feature.nil? | ||
@io.puts feature | ||
@current_feature = feature | ||
end | ||
|
||
def dump_status_counts(element_count_proc) | ||
counts = [:failed, :skipped, :undefined, :pending, :passed].map do |status| | ||
count = element_count_proc.call(status) | ||
count != 0 ? yield("#{count} #{status.to_s}", status) : nil | ||
end.compact | ||
if counts.any? | ||
" (#{counts.join(', ')})" | ||
else | ||
"" | ||
end | ||
def print_test_case(test_case) | ||
@io.print " #{test_case.name} " | ||
end | ||
|
||
def dump_count(count, what, state=nil) | ||
[count, state, "#{what}#{count == 1 ? '' : 's'}"].compact.join(" ") | ||
def print_result(result) | ||
@io.puts format_string(result, result.to_sym) | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
Oops, something went wrong.