# Scripts run using load in "for" loop run out of order

**URL:** <https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988>\
**Category:** ruby-talk\
**Created:** [23 January 2008 18:31 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988 "2008-01-23T18:31:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fa\_Sidd](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fa\_Sidd](https://rubytalk.org/u/Fa_Sidd)\
**Post date:** [23 January 2008 18:31 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/1 "2008-01-23T18:31:33Z")

</div>

Hi  
I am new to ruby and like it so far.

I have an existing test harness to plug into and need to run a number of  
scripts in a specific order.  
I am using load in a "for loop" to run them. Unfortunately the scripts  
get run out of order.

Below is my main script and my other scripts follow.

Files are:  
test.rb  
Tests/A-test.rb  
Tests/B-test.rb  
Tests/C-test.rb  
Tests/D-test.rb

To simplify the question, I have created an array of the scripts I need  
to run in the required order. But the output shows that the scripts run  
in the following order D-test.rb, A-test.rb, C-test.rb and then  
B-test.rb.

Somehow A gets run before C.

Is there any way to run the scripts in the correct order? And how come  
it loops through the puts first and then the load?

Thanks in advance

#\*\*\*\*\*\* test.rb \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TOPDIR = File.join(File.dirname(\_\_FILE\_\_), '.')  
$LOAD\_PATH.unshift TOPDIR

require 'test/unit'  
require 'test/unit/ui/console/testrunner'

Dir.chdir TOPDIR

testList = []

testList[0]="Tests/D-test.rb"  
testList[1]="Tests/C-test.rb"  
testList[2]="Tests/A-test.rb"  
testList[3]="Tests/B-test.rb"

begin  
&nbsp;&nbsp;for i in 0 .. testList.length-1  
&nbsp;&nbsp;&nbsp;&nbsp;puts "i: #{i} testname: #{testList[i]}"  
&nbsp;&nbsp;&nbsp;&nbsp;load(testList[i])  
&nbsp;&nbsp;end  
end

#\*\*\*\*\*\*\*\*\*\*\*\*\* A-test.rb \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
class TC\_One \< Test::Unit::TestCase

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In A-test setup"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_one  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In A-test.rb testcase one"  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 1  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In A-test teardown"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;end  
end

#\*\*\*\*\*\*\*\*\*\*\*\*\* B-test.rb \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
class TC\_Two \< Test::Unit::TestCase

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In B-test setup"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_one  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In B-test.rb testcase one"  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 1  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In B-test teardown"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;end  
end

#\*\*\*\*\*\*\*\*\*\*\*\*\* C-test.rb \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
class TC\_Three \< Test::Unit::TestCase

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In C-test setup"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_one  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In C-test.rb testcase one"  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 1  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In C-test teardown"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;end  
end  
#\*\*\*\*\*\*\*\*\*\*\*\*\* D-test.rb \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
class TC\_Four \< Test::Unit::TestCase

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In D-test setup"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_one  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In D-test.rb testcase one"  
&nbsp;&nbsp;&nbsp;&nbsp;t = Time.now  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 2  
&nbsp;&nbsp;&nbsp;&nbsp;puts t  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;puts "In D-test teardown"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\*\*\*\*\*\*\*\*\*\*\*"  
&nbsp;&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;end  
end  
#\*\*\*\*\*\*\*\*\*\*\*\*\* Output \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Y:\ruby\>test.rb  
i: 0 testname: Tests/D-test.rb  
i: 1 testname: Tests/C-test.rb  
i: 2 testname: Tests/A-test.rb  
i: 3 testname: Tests/B-test.rb  
Loaded suite Y:/ruby/test  
Started

> **···**
>
> \*\*\*\*\*\*\*\*\*\*\*  
> In D-test setup  
> In D-test.rb testcase one  
> Wed Jan 23 10:14:33 -0800 2008  
> In D-test teardown  
> \*\*\*\*\*\*\*\*\*\*\*
> 
> .  
> \*\*\*\*\*\*\*\*\*\*\*  
> In A-test setup  
> In A-test.rb testcase one  
> In A-test teardown  
> \*\*\*\*\*\*\*\*\*\*\*
> 
> .  
> \*\*\*\*\*\*\*\*\*\*\*  
> In C-test setup  
> In C-test.rb testcase one  
> In C-test teardown  
> \*\*\*\*\*\*\*\*\*\*\*
> 
> .  
> \*\*\*\*\*\*\*\*\*\*\*  
> In B-test setup  
> In B-test.rb testcase one  
> In B-test teardown  
> \*\*\*\*\*\*\*\*\*\*\*
> 
> .  
> Finished in 5.002 seconds.
> 
> 4 tests, 0 assertions, 0 failures, 0 errors  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Thomas\_P](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Thomas\_P](https://rubytalk.org/u/Thomas_P)\
**Post date:** [23 January 2008 20:15 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/2 "2008-01-23T20:15:38Z")

</div>

> Hi  
> I am new to ruby and like it so far.
> 
> I have an existing test harness to plug into and need to run a number of  
> scripts in a specific order.  
> I am using load in a "for loop" to run them. Unfortunately the scripts  
> get run out of order.
> 
> To simplify the question, I have created an array of the scripts I need  
> to run in the required order. But the output shows that the scripts run  
> in the following order D-test.rb, A-test.rb, C-test.rb and then  
> B-test.rb.
> 
> Somehow A gets run before C.

in your code, your are not 'running' the test cases with your order  
you're just loading Testcase declarations. All the collected test classes  
are 'run' when your loop is finished.  
The test-classes are running in alphabetical order ("TC\_Four", "TC\_One",  
"TC\_Three", "TC\_Two")

And how come

> it loops through the puts first and then the load?

it doesn't - it does perfectly the puts and then the load-statement but  
since the load is only loading declarations you will see no output (like in  
every other class declaration which is included in a ruby program).

-Thomas

> **···**
>
> On 23/01/2008, Fa Sidd \<siddiqifh@yahoo.com\> wrote:
> 
> --  
> Thomas Preymesser  
> thopre@gmail.com  
> thomas@thopre.com  
> Büro: 030 - 830 353 88  
> mobil: 0176 - 75 03 03 04  
> Privat: 030 - 49 78 37 06
> 
> > **[thopre](https://thopre.wordpress.com/)**
> >
> > Entwicklungen für die Bühne des Lebens
> 
> > **[ThoPre.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=thopre.com)**
> >
> > The leading source brandable domain names. Competitive prices. Excellent service. Get your domain name today.

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [24 January 2008 22:55 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/3 "2008-01-24T22:55:09Z")

</div>

> Hi  
> I am new to ruby and like it so far.  
> ...

First, since you are new to Ruby, some alternate ways to code your sample:

> testList =
> 
> testList[0]="Tests/D-test.rb"  
> testList[1]="Tests/C-test.rb"  
> testList[2]="Tests/A-test.rb"  
> testList[3]="Tests/B-test.rb"

a) Append to the array instead of directly indexing:  
&nbsp;&nbsp;&nbsp;&nbsp;testList =   
&nbsp;&nbsp;&nbsp;&nbsp;testList \<\< "Tests/D-test.rb"  
&nbsp;&nbsp;&nbsp;&nbsp;testList \<\< "Tests/C-test.rb"  
&nbsp;&nbsp;&nbsp;&nbsp;testList \<\< "Tests/A-test.rb"  
&nbsp;&nbsp;&nbsp;&nbsp;testList \<\< "Tests/B-test.rb"

b) Directly initialize the literal array:  
&nbsp;&nbsp;&nbsp;&nbsp;testList = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Tests/D-test.rb",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Tests/C-test.rb",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Tests/A-test.rb",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Tests/B-test.rb"  
&nbsp;&nbsp;&nbsp;&nbsp;]

c) Initialize an array of words:  
&nbsp;&nbsp;&nbsp;&nbsp;testList = %w{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests/D-test.rb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests/C-test.rb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests/A-test.rb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests/B-test.rb  
&nbsp;&nbsp;&nbsp;&nbsp;}

> begin  
> for i in 0 .. testList.length-1  
> &nbsp;&nbsp;&nbsp;puts "i: #{i} testname: #{testList[i]}"  
> &nbsp;&nbsp;&nbsp;load(testList[i])  
> end  
> end

a) Instead of explicit loop and indexing, iterate  
&nbsp;&nbsp;&nbsp;&nbsp;testList.each do |test|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "testname: #{test}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;load test  
&nbsp;&nbsp;&nbsp;&nbsp;end

b) If you still want the index too  
&nbsp;&nbsp;&nbsp;&nbsp;testList.each\_with\_index do |test, i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "#{i} \t #{test}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;load test  
&nbsp;&nbsp;&nbsp;&nbsp;end

And, as to your question, perhaps you could try explicitly invoke the  
TestRunner:

# load all of the test cases  
Dir.glob("./Tests/\*.rb").each do |tfile|  
&nbsp;&nbsp;require tfile  
end

# run the test cases explicitly in order  
[TC\_Four, TC\_Three, TC\_One, TC\_Two].each do |tclass|  
&nbsp;&nbsp;Test::Unit::UI::Console::TestRunner.run(tclass)  
end

See "Test Runners" and "Test Suite":  
[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html](http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html)

> **···**
>
> On Jan 23, 2008 1:31 PM, Fa Sidd \<siddiqifh@yahoo.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Fa\_Sidd](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fa\_Sidd](https://rubytalk.org/u/Fa_Sidd)\
**Post date:** [23 January 2008 21:32 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/4 "2008-01-23T21:32:08Z")

</div>

Thomas Preymesser wrote:

> in your code, your are not 'running' the test cases with your order  
> you're just loading Testcase declarations. All the collected test  
> classes  
> are 'run' when your loop is finished.  
> The test-classes are running in alphabetical order ("TC\_Four", "TC\_One",  
> "TC\_Three", "TC\_Two")

Thanks for your response.

Is there a way I can run the scripts in the correct order in the loop?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [24 January 2008 23:06 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/5 "2008-01-24T23:06:51Z")

</div>

> And, as to your question, perhaps you could try explicitly invoke the  
> TestRunner:
> 
> ...
> 
> See "Test Runners" and "Test Suite":  
> [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html](http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html)

Oops, lost the last bit:  
But, sorry that this doesn't yet work around not knowing the class  
names just the file names.

---

<div class="post-metadata">

**Author:** ![zetetic](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@zetetic](https://rubytalk.org/u/zetetic)\
**Post date:** [24 January 2008 07:40 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/6 "2008-01-24T07:40:00Z")

</div>

To execute a test, you need to run it as follows:

&nbsp;&nbsp;Test::Unit::UI::Console::TestRunner.run(test)

Try replacing everything after Dir.chdir TOPDIR with the following:

&nbsp;&nbsp;Dir["Tests/\*.rb"].each {|testfile| load "#{testfile}"}  
&nbsp;&nbsp;[TC\_Four,TC\_Three,TC\_One,TC\_Two,].each {|test|  
Test::Unit::UI::Console::TestRunner.run(test) }

or if you want to name the files explicitly, you could do:

&nbsp;&nbsp;testList = %w(Tests/A-test.rb Tests/B-test.rb Tests/C-test.rb Tests/  
D-test.rb)  
&nbsp;&nbsp;testList.each {|testfile| load "#{testfile}"}  
&nbsp;&nbsp;[TC\_Four,TC\_Three,TC\_One,TC\_Two,].each {|test|  
Test::Unit::UI::Console::TestRunner.run(test) }

This loads all the test classes first, then executes them in the order  
you specify. Change the order of the classes in the second line if you  
want to change the order of the tests.

> **···**
>
> On Jan 23, 1:32 pm, Fa Sidd \<siddiq...@yahoo.com\> wrote:
> 
> > Thomas Preymesser wrote:  
> > \> in your code, your are not 'running' the test cases with your order  
> > \> you're just loading Testcase declarations. All the collected test  
> > \> classes  
> > \> are 'run' when your loop is finished.  
> > \> The test-classes are running in alphabetical order ("TC\_Four", "TC\_One",  
> > \> "TC\_Three", "TC\_Two")
> > 
> > Thanks for your response.
> > 
> > Is there a way I can run the scripts in the correct order in the loop?  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [24 January 2008 23:24 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/7 "2008-01-24T23:24:04Z")

</div>

Maybe:

require 'test/unit/ui/console/testrunner'

# global array to hold the test classes as they are loaded  
$tests =

# monkey patch to record the new subclasses as they are loaded  
class Test::Unit::TestCase  
&nbsp;&nbsp;def self.inherited(subclass)  
&nbsp;&nbsp;&nbsp;&nbsp;$tests \<\< subclass  
&nbsp;&nbsp;end  
end

# load all the files  
%w{  
&nbsp;&nbsp;Tests/D-test.rb  
&nbsp;&nbsp;Tests/C-test.rb  
&nbsp;&nbsp;Tests/A-test.rb  
&nbsp;&nbsp;Tests/B-test.rb  
}.each do |tfile|  
&nbsp;&nbsp;require tfile  
end

# run the test classes in the order they were loaded  
$tests.each do |tclass|  
&nbsp;&nbsp;Test::Unit::UI::Console::TestRunner.run(tclass)  
end

> **···**
>
> On Jan 24, 2008 6:06 PM, \<brabuhr@gmail.com\> wrote:
> 
> > But, sorry that this doesn't yet work around not knowing the class  
> > names just the file names.

---

<div class="post-metadata">

**Author:** ![Fa\_Sidd](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fa\_Sidd](https://rubytalk.org/u/Fa_Sidd)\
**Post date:** [24 January 2008 17:00 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/8 "2008-01-24T17:00:39Z")

</div>

zetetic wrote:

> &nbsp;&nbsp;testList = %w(Tests/A-test.rb Tests/B-test.rb Tests/C-test.rb Tests/  
> D-test.rb)  
> &nbsp;&nbsp;testList.each {|testfile| load "#{testfile}"}  
> &nbsp;&nbsp;[TC\_Four,TC\_Three,TC\_One,TC\_Two,].each {|test|  
> Test::Unit::UI::Console::TestRunner.run(test) }
> 
> This loads all the test classes first, then executes them in the order  
> you specify. Change the order of the classes in the second line if you  
> want to change the order of the tests.

I get the list of tests scripts from the test harness and do not know  
the class names and execution order in advance.

Is there a better way to my tests, if I had just a list of test scripts  
in order of execution

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Fa\_Sidd](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fa\_Sidd](https://rubytalk.org/u/Fa_Sidd)\
**Post date:** [25 January 2008 01:04 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/9 "2008-01-25T01:04:22Z")

</div>

unknown wrote:

> **···**
>
> > On Jan 24, 2008 6:06 PM, \<brabuhr@gmail.com\> wrote:
> > 
> > # monkey patch to record the new subclasses as they are loaded  
> > class Test::Unit::TestCase  
> > &nbsp;&nbsp;def self.inherited(subclass)  
> > &nbsp;&nbsp;&nbsp;&nbsp;$tests \<\< subclass  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # load all the files  
> > %w{  
> > &nbsp;&nbsp;Tests/D-test.rb  
> > &nbsp;&nbsp;Tests/C-test.rb  
> > &nbsp;&nbsp;Tests/A-test.rb  
> > &nbsp;&nbsp;Tests/B-test.rb  
> > }.each do |tfile|  
> > &nbsp;&nbsp;require tfile  
> > end
> > 
> > # run the test classes in the order they were loaded  
> > $tests.each do |tclass|  
> > &nbsp;&nbsp;Test::Unit::UI::Console::TestRunner.run(tclass)  
> > end
> 
> This worked!
> 
> Not sure if replacing "load(testList[i])" with "`ruby #{testList[i]}`"  
> would have worked. I have global variables that I need to worry about
> 
> Thanks for the help everyone
> 
> Thanks.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [25 January 2008 02:09 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/10 "2008-01-25T02:09:57Z")

</div>

Or, avoid the global with:

&nbsp;&nbsp;class Test::Unit::TestCase  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :subclasses  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def inherited( subclass )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@subclasses||=) \<\< subclass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

and then

&nbsp;&nbsp;Test::Unit::TestCase.subclasses.each do |klass|  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end

> **···**
>
> On Jan 24, 4:24 pm, brab...@gmail.com wrote:
> 
> > On Jan 24, 2008 6:06 PM, \<brab...@gmail.com\> wrote:
> > 
> > \> But, sorry that this doesn't yet work around not knowing the class  
> > \> names just the file names.
> > 
> > Maybe:
> > 
> > require 'test/unit/ui/console/testrunner'
> > 
> > # global array to hold the test classes as they are loaded  
> > $tests =
> > 
> > # monkey patch to record the new subclasses as they are loaded  
> > class Test::Unit::TestCase  
> > def self.inherited(subclass)  
> > $tests \<\< subclass  
> > end  
> > end

---

<div class="post-metadata">

**Author:** ![Thomas\_P](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Thomas\_P](https://rubytalk.org/u/Thomas_P)\
**Post date:** [24 January 2008 17:42 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/11 "2008-01-24T17:42:03Z")

</div>

replace your "load(testList[i])"-line with "`ruby #{testList[i]}`"

-Thomas

> **···**
>
> On 24/01/2008, Fa Sidd \<siddiqifh@yahoo.com\> wrote:
> 
> > zetetic wrote:  
> > \>  
> > \> testList = %w(Tests/A-test.rb Tests/B-test.rb Tests/C-test.rb Tests/  
> > \> D-test.rb)  
> > \> testList.each {|testfile| load "#{testfile}"}  
> > \> [TC\_Four,TC\_Three,TC\_One,TC\_Two,].each {|test|  
> > \> Test::Unit::UI::Console::TestRunner.run(test) }  
> > \>  
> > \> This loads all the test classes first, then executes them in the order  
> > \> you specify. Change the order of the classes in the second line if you  
> > \> want to change the order of the tests.
> > 
> > I get the list of tests scripts from the test harness and do not know  
> > the class names and execution order in advance.
> > 
> > Is there a better way to my tests, if I had just a list of test scripts  
> > in order of execution
> 
> --  
> Thomas Preymesser  
> thopre@gmail.com  
> thomas@thopre.com  
> Büro: 030 - 830 353 88  
> mobil: 0176 - 75 03 03 04  
> Privat: 030 - 49 78 37 06
> 
> > **[thopre](https://thopre.wordpress.com/)**
> >
> > Entwicklungen für die Bühne des Lebens
> 
> > **[ThoPre.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=thopre.com)**
> >
> > The leading source brandable domain names. Competitive prices. Excellent service. Get your domain name today.

---

<div class="post-metadata">

**Author:** ![zetetic](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@zetetic](https://rubytalk.org/u/zetetic)\
**Post date:** [25 January 2008 22:24 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/12 "2008-01-25T22:24:57Z")

</div>

Sweet! (or perhaps I should say: suite!)

> **···**
>
> On Jan 24, 6:06 pm, Phrogz \<phr...@mac.com\> wrote:
> 
> > On Jan 24, 4:24 pm, brab...@gmail.com wrote:
> > 
> > \> On Jan 24, 2008 6:06 PM, \<brab...@gmail.com\> wrote:
> > 
> > \> \> But, sorry that this doesn't yet work around not knowing the class  
> > \> \> names just the file names.
> > 
> > \> Maybe:
> > 
> > \> require 'test/unit/ui/console/testrunner'
> > 
> > \> # global array to hold the test classes as they are loaded  
> > \> $tests =
> > 
> > \> # monkey patch to record the new subclasses as they are loaded  
> > \> class Test::Unit::TestCase  
> > \> def self.inherited(subclass)  
> > \> $tests \<\< subclass  
> > \> end  
> > \> end
> > 
> > Or, avoid the global with:
> > 
> > &nbsp;&nbsp;class Test::Unit::TestCase  
> > &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :subclasses  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def inherited( subclass )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@subclasses||=) \<\< subclass  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > and then
> > 
> > &nbsp;&nbsp;Test::Unit::TestCase.subclasses.each do |klass|  
> > &nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [26 January 2008 00:06 UTC](https://rubytalk.org/t/scripts-run-using-load-in-for-loop-run-out-of-order/43988/13 "2008-01-26T00:06:30Z")

</div>

check out my dynaload gem, it abstracts this very pattern for objects, classses, and modules.

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Jan 25, 2008, at 3:24 PM, zetetic wrote:
> 
> > Sweet! (or perhaps I should say: suite!)
> 
> --  
> share your knowledge. it's a way to achieve immortality.  
> h.h. the 14th dalai lama
