handle exception in setup/teardown

+ new test of this and exception in test_* methods
This commit is contained in:
Johan Holmberg 2016-06-26 21:12:53 +02:00
parent 966a98f4b1
commit deb6af6086
2 changed files with 63 additions and 3 deletions

View File

@ -169,9 +169,12 @@ module Cmdtest
clog.notify("testmethod", @method) do
obj = @adm_class.runtime_class.new(self, clog, runner)
Dir.chdir(obj._work_dir.path)
obj.setup
begin
obj.setup
obj.send(@method)
Dir.chdir(obj._work_dir.path)
obj.teardown
clog.assert_success
runner.method_filter.success(method_id)
rescue Cmdtest::AssertFailed => e
@ -184,8 +187,6 @@ module Cmdtest
io.puts e.backtrace.map {|line| " " + line }
clog.assert_error(io.string)
end
Dir.chdir(obj._work_dir.path)
obj.teardown
end
ensure
Dir.chdir(ORIG_CWD)

59
t/CMDTEST_raise.rb Normal file
View File

@ -0,0 +1,59 @@
require "selftest_utils"
class CMDTEST_raise < Cmdtest::Testcase
include SelftestUtils
def test_raise_TEST
create_file "CMDTEST_foo.rb", [
"class CMDTEST_foo < Cmdtest::Testcase",
" def setup",
" raise 'error in setup' if ENV['CMDTEST_RAISE'] == 'setup'",
" end",
"",
" def teardown",
" raise 'error in teardown' if ENV['CMDTEST_RAISE'] == 'teardown'",
" end",
"",
" def test_foo",
" raise 'error in test' if ENV['CMDTEST_RAISE'] == 'test'",
" puts '123'",
" end",
"end",
]
cmd_cmdtest do
stdout_equal [
"123",
]
end
ENV['CMDTEST_RAISE'] = 'setup'
cmd_cmdtest do
exit_nonzero
stdout_equal /--- CAUGHT EXCEPTION:/
stdout_equal /--- error in setup/
end
ENV['CMDTEST_RAISE'] = 'test'
cmd_cmdtest do
exit_nonzero
stdout_equal /--- CAUGHT EXCEPTION:/
stdout_equal /--- error in test/
end
ENV['CMDTEST_RAISE'] = 'teardown'
cmd_cmdtest do
exit_nonzero
stdout_equal /--- CAUGHT EXCEPTION:/
stdout_equal /--- error in teardown/
end
ENV['CMDTEST_RAISE'] = nil
cmd_cmdtest do
stdout_equal "123\n"
end
end
end