Weird test/unit behavior

Consider a simple test -

my_test.rb:

···

----------------------------------------
require 'test/unit'

class MyTest < Test::Unit::TestCase

  def test1
    assert(true)
  end

end
----------------------------------------

Now consider a simple script to run this.

run2.rb:
----------------------------------------
if ARGV.length == 0
  puts "usage run testfile"
else
  type = ARGV.shift
  if type == "-t"
    f = ARGV.shift
    load f
  end
end
----------------------------------------

So if you run it then you get the expected result.

run2 -t my_test.rb
Loaded suite ~sparkle:/nasir/misc/test/run2
Started
.
Finished in 0.0 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

But now consider another script to run the same test.

run1.rb:
--------------------------------------------
D:\nasir\misc\test>cat run1.rb
if ARGV.length == 0
  puts "usage: run testfile"
else
  ARGV.each_with_index do |arg, i|
    if arg == "-t"
      load ARGV[i+1]
    end
  end
end
--------------------------------------------

When you run it the testcase is loaded alright but it doesnt run!

run1.rb -t my_test.rb
Loaded suite ~sparkle:/nasir/misc/test/run1
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

------

Apparently the testsuite is not run/loaded in this case.

Any help in finding out what is going on is appreciated.

- nasir

Ah Gotcha!

It is a side effect of having commandline arguments. ARGV is also used by
autorunner.rb.
using shift in run2.rb clears the ARGV array.

···

On 7/6/07, Nasir Khan <rubylearner@gmail.com> wrote:

Consider a simple test -

my_test.rb:
----------------------------------------
require 'test/unit'

class MyTest < Test::Unit::TestCase

  def test1
    assert(true)
  end

end
----------------------------------------

Now consider a simple script to run this.

run2.rb:
----------------------------------------
if ARGV.length == 0
  puts "usage run testfile"
else
  type = ARGV.shift
  if type == "-t"
    f = ARGV.shift
    load f
  end
end
----------------------------------------

So if you run it then you get the expected result.

run2 -t my_test.rb
Loaded suite ~sparkle:/nasir/misc/test/run2
Started
.
Finished in 0.0 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

But now consider another script to run the same test.

run1.rb:
--------------------------------------------
D:\nasir\misc\test>cat run1.rb
if ARGV.length == 0
  puts "usage: run testfile"
else
  ARGV.each_with_index do |arg, i|
    if arg == "-t"
      load ARGV[i+1]
    end
  end
end
--------------------------------------------

When you run it the testcase is loaded alright but it doesnt run!

run1.rb -t my_test.rb
Loaded suite ~sparkle:/nasir/misc/test/run1
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

------

Apparently the testsuite is not run/loaded in this case.

Any help in finding out what is going on is appreciated.

- nasir