Implement fail-fast in runner

Report tracks number of omitted tests due to abort.
This commit is contained in:
Michael Miller 2019-03-22 00:05:53 -06:00
parent 9e63c20df2
commit a31d5c8b5b
4 changed files with 82 additions and 7 deletions

View file

@ -71,6 +71,15 @@ describe Spectator::Report do
end
end
describe "#remaining_count" do
it "is the expected value" do
results = [] of Spectator::Result
remaining = 5
report = Spectator::Report.new(results, Time::Span.zero, remaining)
report.remaining_count.should eq(remaining)
end
end
describe "#failed?" do
context "with a failed test suite" do
it "is true" do
@ -87,6 +96,24 @@ describe Spectator::Report do
end
end
describe "#remaining?" do
context "with remaining tests" do
it "is true" do
results = [] of Spectator::Result
report = Spectator::Report.new(results, Time::Span.zero, 5)
report.remaining?.should be_true
end
end
context "without remaining tests" do
it "is false" do
results = [] of Spectator::Result
report = Spectator::Report.new(results, Time::Span.zero, 0)
report.remaining?.should be_false
end
end
end
describe "#failures" do
it "returns the expected results" do
results = Array.new(5) { new_failure_result.as(Spectator::Result) }

View file

@ -1,9 +1,10 @@
require "./spec_helper"
# Creates a `Config` for Spectator that is suited for testing it.
def spectator_test_config(formatter : Spectator::Formatting::Formatter? = nil)
def spectator_test_config(formatter : Spectator::Formatting::Formatter? = nil, fail_fast = false)
builder = Spectator::ConfigBuilder.new
builder.formatter = formatter || Spectator::Formatting::SilentFormatter.new
builder.fail_fast = fail_fast
builder.build
end
@ -26,6 +27,34 @@ describe Spectator::Runner do
called.should eq([0, 1, 2, 3, 4])
end
context "with fail-fast enabled" do
it "stops on the first failure" do
called = [] of Int32
group = SpyExample.create_group(10) do |index|
called << index
raise "Failure" if index > 5
end
suite = Spectator::TestSuite.new(group)
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
runner.run
called.should eq([0, 1, 2, 3, 4, 5, 6])
end
context "the report" do
it "has the remaining tests" do
spy = SpyFormatter.new
group = SpyExample.create_group(10) do |index|
raise "Failure" if index > 5
end
suite = Spectator::TestSuite.new(group)
runner = Spectator::Runner.new(suite, spectator_test_config(spy, true))
runner.run
report = spy.end_suite_calls.first
report.remaining_count.should eq(3)
end
end
end
context "the formatter" do
it "#start_suite is called once" do
spy = SpyFormatter.new

View file

@ -17,10 +17,15 @@ module Spectator
# Number of pending examples.
getter pending_count = 0
# Number of remaining tests.
# This will be greater than zero only in fail-fast mode.
getter remaining_count
# Creates the report.
# The *results* are from running the examples in the test suite.
# The *runtime* is the total time it took to execute the suite.
def initialize(@results : Array(Result), @runtime)
# The *remaining_count* is the number of tests skipped due to fail-fast.
def initialize(@results : Array(Result), @runtime, @remaining_count = 0)
@results.each do |result|
case result
when SuccessfulResult
@ -46,6 +51,12 @@ module Spectator
failed_count > 0
end
# Indicates whether there were skipped tests
# because of a failure causing the test to abort.
def remaining?
remaining_count > 0
end
# Returns a set of results for all failed examples.
def failures
@results.each.compact_map(&.as?(FailedResult))

View file

@ -16,20 +16,28 @@ module Spectator
@config.formatter.start_suite(@suite)
# Run all examples and capture the results.
results = [] of Result
results = Array(Result).new(@suite.size)
elapsed = Time.measure do
results = @suite.map do |example|
run_example(example).as(Result)
end
collect_results(results)
end
# Generate a report and pass it along to the formatter.
report = Report.new(results, elapsed)
remaining = @suite.size - results.size
report = Report.new(results, elapsed, remaining)
@config.formatter.end_suite(report)
!report.failed?
end
# Runs all examples and adds results to a list.
private def collect_results(results)
@suite.each do |example|
result = run_example(example).as(Result)
results << result
break if @config.fail_fast? && result.is_a?(FailedResult)
end
end
# Runs a single example and returns the result.
# The formatter is given the example and result information.
private def run_example(example)