diff --git a/spec/report_spec.cr b/spec/report_spec.cr index f9e9b64..97491c2 100644 --- a/spec/report_spec.cr +++ b/spec/report_spec.cr @@ -15,7 +15,7 @@ def new_failure_result(result_type : Spectator::Result.class = Spectator::Failed result_type.new(example, elapsed, expectations, error) end -def new_report(successful_count = 5, failed_count = 5, error_count = 5, pending_count = 5, overhead_time = 1_000_000i64) +def new_report(successful_count = 5, failed_count = 5, error_count = 5, pending_count = 5, overhead_time = 1_000_000i64, fail_blank = false) results = [] of Spectator::Result successful_count.times { results << new_passing_result } failed_count.times { results << new_failure_result } @@ -24,7 +24,7 @@ def new_report(successful_count = 5, failed_count = 5, error_count = 5, pending_ example_runtime = results.compact_map(&.as?(Spectator::FinishedResult)).sum(&.elapsed) total_runtime = example_runtime + Time::Span.new(nanoseconds: overhead_time) - Spectator::Report.new(results, total_runtime) + Spectator::Report.new(results, total_runtime, fail_blank: fail_blank) end describe Spectator::Report do @@ -43,6 +43,13 @@ describe Spectator::Report do end end + describe "#examples_ran" do + it "is the number of non-skipped examples" do + report = new_report(5, 4, 3, 2) + report.examples_ran.should eq(12) + end + end + describe "#successful_count" do it "is the expected value" do report = new_report(5, 4, 3, 2) @@ -94,6 +101,31 @@ describe Spectator::Report do report.failed?.should be_false end end + + context "with fail-blank enabled" do + context "when no tests run" do + it "is true" do + report = new_report(0, 0, 0, 5, fail_blank: true) + report.failed?.should be_true + end + end + + context "when tests run" do + context "and there are failures" do + it "is true" do + report = new_report(5, 4, 3, 2, fail_blank: true) + report.failed?.should be_true + end + end + + context "and there are no failures" do + it "is false" do + report = new_report(5, 0, 0, 2, fail_blank: true) + report.failed?.should be_false + end + end + end + end end describe "#remaining?" do diff --git a/src/spectator/report.cr b/src/spectator/report.cr index a284dff..11c4797 100644 --- a/src/spectator/report.cr +++ b/src/spectator/report.cr @@ -25,7 +25,8 @@ module Spectator # The *results* are from running the examples in the test suite. # The *runtime* is the total time it took to execute the suite. # The *remaining_count* is the number of tests skipped due to fail-fast. - def initialize(@results : Array(Result), @runtime, @remaining_count = 0) + # The *fail_blank* flag indicates whether it is a failure if there were no tests run. + def initialize(@results : Array(Result), @runtime, @remaining_count = 0, @fail_blank = false) @results.each do |result| case result when SuccessfulResult @@ -46,9 +47,14 @@ module Spectator @results.size end + # Number of examples run (not skipped or pending). + def examples_ran + @successful_count + @failed_count + end + # Indicates whether the test suite failed. def failed? - failed_count > 0 + failed_count > 0 || (@fail_blank && examples_ran == 0) end # Indicates whether there were skipped tests diff --git a/src/spectator/runner.cr b/src/spectator/runner.cr index 41e78ee..27558e9 100644 --- a/src/spectator/runner.cr +++ b/src/spectator/runner.cr @@ -23,7 +23,7 @@ module Spectator # Generate a report and pass it along to the formatter. remaining = @suite.size - results.size - report = Report.new(results, elapsed, remaining) + report = Report.new(results, elapsed, remaining, @config.fail_blank?) @config.formatter.end_suite(report) !report.failed?