-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Summary formatter #999
Merged
Merged
Summary formatter #999
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public methods of the Console module are part of the public API (see #893), so this PR introduces breaking changes (which is OK since we are aiming at 3.0.0 - but the PR is miss labeled).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point Bjorn. I'd like to get rid of that module altogether really, or at least slim down its interface a lot. Using composition (e.g.
ConsoleIssues
) is the way forward.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I've updated the labelling on the PR.
I regret that I didn't tease apart the refactoring from the development of the new formatter, but it was late. I guess I still could if you think that would make it easier to review / merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have been nice for this change to be documented in the changelog 🍻