From: "weathercoach@..." Date: 2007-11-20T10:05:02+09:00 Subject: exit call in script causes unit test to not run (newby) Hello. I've been grappling with unit testing for a couple weeks now. I'm a bourne shell scripter by day who is trying to expand my skill set. I currently have a ruby script which I'm trying to create some unit tests for. The unit tests have not been running and I've finally traced the problem to this method. Specifically the "exit" call. If I comment out the exit then my tests actually run. # Cleanup any transient files and directories and then exit def script_clean FileUtils.rm_rf(WORKDIR) if File.exists?(WORKDIR) # Everything is tidy we should leave exit end The above method is called in 2 different scenarios. The first case is during script execution if any Exceptions occur. Then the error message is printed and the script_clean method is called to ensure the process exits. Here is an example of where the method is called begin File.open(ADMIN_FILE, "w+") {|f| f << ADMIN_CONTENTS } rescue Exception => e puts e ; script_clean end Additionally if the script runs through to the end without encountering any errors then the last line is a call to the script_clean method script_clean My tests are not triggering any Exceptions so it appears to be the last line which calls "script_clean" to hamper any test execution. Explicitly exiting when an uncorrectable error has happened is something I commonly do in bourne shell scripts and it made sense to carry over the practice in ruby. Should I be terminating premature and normal script execution in some other fashion that is more compatible with test/unit. Here is an basic example demonstrating what I'm seeing. ------------example script to reproduce 'exit' call hindering unit tests require 'optparse' # Argument handling options = {} OptionParser.new do |o| o.on("-n") { |options[:noop]| } o.on_tail("-h", "--help", "Print usage.") o.parse!(ARGV) end # Set PROTO constant based on command line arg if options[:noop] PROTO = true else PROTO = false end # Example method that exits def clean exit end # Call clean method if PROTO is true if PROTO clean end ----------example unit test # Load up required supporting files require 'someapp' require 'test/unit' # Class to hold the config file tests class CliTest < Test::Unit::TestCase def test_dryrun assert PROTO, true end end -----------example execution where "exit" is not called ./test_someapp.rb Loaded suite ./test_someapp Started F Finished in 0.013832 seconds. 1) Failure: test_dryrun(CliTest) [./test_someapp.rb:23]: true. is not true. 1 tests, 1 assertions, 1 failures, 0 errors ----------example execution which calls "exit" ./test_someapp.rb -n # echo $? 0 Any tips for a bourne shell convert are very much appreciated! TIA. G