From 12f905a31b133b8cdb612e8ddf64fe3e0cd03f12 Mon Sep 17 00:00:00 2001 From: Johan Holmberg Date: Mon, 31 Oct 2016 00:32:33 +0100 Subject: [PATCH] add 'skip_test' --- bin/cmdtest.rb | 15 ++++++++++++++- lib/cmdtest/baselogger.rb | 9 +++++++++ lib/cmdtest/consolelogger.rb | 4 ++++ lib/cmdtest/junitfile.rb | 29 ++++++++++++++++++++++++++--- lib/cmdtest/junitlogger.rb | 12 +++++++++++- lib/cmdtest/testcase.rb | 9 +++++++++ t/CMDTEST_options.rb | 2 +- 7 files changed, 74 insertions(+), 6 deletions(-) diff --git a/bin/cmdtest.rb b/bin/cmdtest.rb index b73ef9e..988debd 100755 --- a/bin/cmdtest.rb +++ b/bin/cmdtest.rb @@ -62,6 +62,10 @@ module Cmdtest process_item [:assert_success] end + def test_skipped(str) + process_item [:test_skipped, str] + end + def assert_failure(str) process_item [:assert_failure, str] end @@ -103,6 +107,8 @@ module Cmdtest case cmd when :assert_success # nothing + when :test_skipped + _distribute("test_skipped", rest) when :assert_failure _distribute("assert_failure", rest) when :assert_error @@ -185,6 +191,10 @@ module Cmdtest clog.assert_success runner.method_filter.success(method_id) ok = true + rescue Cmdtest::TestSkipped => e + obj.teardown + clog.test_skipped(e.message) + runner.method_filter.failure(method_id) rescue Cmdtest::AssertFailed => e obj.teardown clog.assert_failure(e.message) @@ -418,6 +428,7 @@ module Cmdtest "classes" => error_logger.n_classes, "methods" => error_logger.n_methods, "commands" => error_logger.n_commands, + "skipped" => error_logger.n_skipped, "failures" => error_logger.n_failures, "errors" => error_logger.n_errors, } @@ -462,6 +473,7 @@ module Cmdtest "classes" => error_logger.n_classes, "methods" => error_logger.n_methods, "commands" => error_logger.n_commands, + "skipped" => error_logger.n_skipped, "failures" => error_logger.n_failures, "errors" => error_logger.n_errors, } @@ -487,11 +499,12 @@ module Cmdtest puts "###" puts "### Finished: %s, Elapsed: %02d:%02d:%02d" % [Time.now.strftime("%F %T"), h,m,s] puts - puts "%s %d test classes, %d test methods, %d commands, %d errors, %d fatals." % [ + puts "%s %d test classes, %d test methods, %d commands, %d skipped, %d errors, %d fatals." % [ summary["failures"] == 0 && summary["errors"] == 0 ? "###" : "---", summary["classes"], summary["methods"], summary["commands"], + summary["skipped"], summary["failures"], summary["errors"], ] diff --git a/lib/cmdtest/baselogger.rb b/lib/cmdtest/baselogger.rb index 82c5367..46c5cb3 100644 --- a/lib/cmdtest/baselogger.rb +++ b/lib/cmdtest/baselogger.rb @@ -56,6 +56,9 @@ module Cmdtest def cmdline(method, comment) end + def test_skipped(str) + end + def assert_failure(str) end @@ -69,6 +72,7 @@ module Cmdtest attr_reader :n_suites, :n_files, :n_classes attr_reader :n_methods, :n_commands, :n_failures, :n_errors + attr_reader :n_skipped def initialize(opts) super @@ -78,6 +82,7 @@ module Cmdtest @n_classes = 0 @n_methods = 0 @n_commands = 0 + @n_skipped = 0 @n_failures = 0 @n_errors = 0 end @@ -102,6 +107,10 @@ module Cmdtest @n_commands += 1 end + def test_skipped(msg) + @n_skipped += 1 + end + def assert_failure(msg) @n_failures += 1 end diff --git a/lib/cmdtest/consolelogger.rb b/lib/cmdtest/consolelogger.rb index f4e25aa..db75d74 100644 --- a/lib/cmdtest/consolelogger.rb +++ b/lib/cmdtest/consolelogger.rb @@ -51,6 +51,10 @@ module Cmdtest end end + def test_skipped(str) + puts str.gsub(/^/, "--- ") + end + def assert_failure(str) puts str.gsub(/^/, "--- ") end diff --git a/lib/cmdtest/junitfile.rb b/lib/cmdtest/junitfile.rb index edf9d12..0a2a0c3 100644 --- a/lib/cmdtest/junitfile.rb +++ b/lib/cmdtest/junitfile.rb @@ -70,7 +70,7 @@ module Cmdtest #---------- - class ErrTestcase < Testcase + class ProblemTestcase < Testcase def initialize(classname, name, message, type, text) @classname = classname @@ -85,10 +85,12 @@ module Cmdtest @classname, @name, ] - f.put ' %s', [ + f.put ' <%s message="%s" type="%s">%s', [ + xml_tag, @message, @type, @text, + xml_tag, ] f.put ' ' end @@ -96,6 +98,20 @@ module Cmdtest #---------- + class ErrTestcase < ProblemTestcase + def xml_tag + "failure" + end + end + + class SkipTestcase < ProblemTestcase + def xml_tag + "skipped" + end + end + + #---------- + class Testsuite def initialize(package, name) @@ -116,10 +132,17 @@ module Cmdtest testcase end + def skip_testcase(classname, name, message, type, text) + testcase = SkipTestcase.new(classname, name, message, type, text) + @testcases << testcase + testcase + end + def write(f) - f.put ' ', [ + f.put ' ', [ 0, @testcases.grep(ErrTestcase).size, + @testcases.grep(SkipTestcase).size, @name, @testcases.size, @package, diff --git a/lib/cmdtest/junitlogger.rb b/lib/cmdtest/junitlogger.rb index c398bf4..bd9b6e3 100644 --- a/lib/cmdtest/junitlogger.rb +++ b/lib/cmdtest/junitlogger.rb @@ -41,10 +41,16 @@ module Cmdtest def testmethod_begin(method) @err_assertions = [] + @err_skip = nil end def testmethod_end(method) - if @err_assertions.size > 0 + if @err_skip != nil + message = @err_skip.split(/\n/)[0] + type = "skip" + text = @err_skip + @ts.skip_testcase(_xml_class, method, message, type, text) + elsif @err_assertions.size > 0 message = @err_assertions[0].split(/\n/)[0] type = "assert" text = @err_assertions.join @@ -58,6 +64,10 @@ module Cmdtest "CMDTEST." + @testcase_class_name end + def test_skipped(str) + @err_skip = str + end + def assert_failure(str) @err_assertions << str end diff --git a/lib/cmdtest/testcase.rb b/lib/cmdtest/testcase.rb index b568197..be04c50 100644 --- a/lib/cmdtest/testcase.rb +++ b/lib/cmdtest/testcase.rb @@ -28,6 +28,7 @@ require "cmdtest/lcs" module Cmdtest class AssertFailed < RuntimeError ; end + class TestSkipped < RuntimeError ; end class UsageError < RuntimeError ; end # Base class for testcases. @@ -357,6 +358,14 @@ module Cmdtest #------------------------------ + def skip_test(reason) + _process_after do + raise TestSkipped, "SKIP: " + reason + end + end + + #------------------------------ + def exit_zero _process_after do @_checked_status = true diff --git a/t/CMDTEST_options.rb b/t/CMDTEST_options.rb index 1c6f8f7..c975aaf 100644 --- a/t/CMDTEST_options.rb +++ b/t/CMDTEST_options.rb @@ -39,7 +39,7 @@ class CMDTEST_options < Cmdtest::Testcase stdout_equal /.===== CMDTEST_foo.rb/ stdout_equal /.----- CMDTEST_foo1$/ stdout_equal /.\.\.\.\.\. test_foo1$/ - stdout_equal /test methods, \d+ commands, \d+ errors,/ + stdout_equal /test methods, \d+ commands, \d+ skipped, \d+ errors,/ end end