# Iterator behaviour

**URL:** <https://rubytalk.org/t/iterator-behaviour/33905>\
**Category:** ruby-talk\
**Created:** [20 December 2006 16:54 UTC](https://rubytalk.org/t/iterator-behaviour/33905 "2006-12-20T16:54:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Russell](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max\_Russell](https://rubytalk.org/u/Max_Russell)\
**Post date:** [20 December 2006 16:54 UTC](https://rubytalk.org/t/iterator-behaviour/33905/1 "2006-12-20T16:54:15Z")

</div>

I have a harness I'm using for testing with Watir. It has become  
apparent that the iterator is loading the tests defined based on the  
order they appear in the directory as opposed to the order in the  
testlist.

File.open('testlist.txt').each\_line do |entry|  
&nbsp;&nbsp;self.send(:define\_method, entry.strip){ load "#{entry.strip}.rb"}

so if the testlist read something like

test\_aac  
test\_aab  
test\_aaa

instead of executing them in the order above it would do

test\_aaa  
test\_aab  
test\_aac

I'm probably being thick, but I'm not spotting where my iterator is  
going wrong?

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

---

<div class="post-metadata">

**Author:** ![Brad\_Phelan1](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Brad\_Phelan1](https://rubytalk.org/u/Brad_Phelan1)\
**Post date:** [21 December 2006 08:25 UTC](https://rubytalk.org/t/iterator-behaviour/33905/2 "2006-12-21T08:25:09Z")

</div>

Max Russell wrote:

> I have a harness I'm using for testing with Watir. It has become  
> apparent that the iterator is loading the tests defined based on the  
> order they appear in the directory as opposed to the order in the  
> testlist.
> 
> File.open('testlist.txt').each\_line do |entry|  
> &nbsp;&nbsp;self.send(:define\_method, entry.strip){ load "#{entry.strip}.rb"}
> 
> so if the testlist read something like
> 
> test\_aac  
> test\_aab  
> test\_aaa
> 
> instead of executing them in the order above it would do
> 
> test\_aaa  
> test\_aab  
> test\_aac
> 
> I'm probably being thick, but I'm not spotting where my iterator is  
> going wrong?

There is nothing wrong with your iterator here. However what  
you are doing with your iterator is calling define\_method  
which will generate methods ( presumably test methods )  
inside your test class.

I am guessing that the test framework is agnostic as to  
the order you define methods. It would probably pull them  
off the class with

&nbsp;&nbsp;Class:instance\_methods

which I will probably generate a list in alphabetical order  
which is what you see.

> **···**
>
> --  
> Brad Phelan  
> [http://xtargets.com](http://xtargets.com)

---

<div class="post-metadata">

**Author:** ![Max\_Russell](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max\_Russell](https://rubytalk.org/u/Max_Russell)\
**Post date:** [22 December 2006 11:13 UTC](https://rubytalk.org/t/iterator-behaviour/33905/3 "2006-12-22T11:13:51Z")

</div>

I'm doing the ugly thing just now and using something like this

test\_001\_aaa  
test\_002\_aac  
test\_003\_aab

obviously this isn't nice as it necessitates retaining a mapping of  
tests to order, which is what I wanted to avoid. I had wanted to be able  
to select lists of \*any\* tests in any order, thus creating suites out of  
arbitrary test components.

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

---

<div class="post-metadata">

**Author:** ![Shiwei\_Zhang](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Shiwei\_Zhang](https://rubytalk.org/u/Shiwei_Zhang)\
**Post date:** [21 December 2006 11:46 UTC](https://rubytalk.org/t/iterator-behaviour/33905/4 "2006-12-21T11:46:57Z")

</div>

Max,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Can you tell us how you define your define\_method? You put "yield" within it, right? I think the iterator in your scenario has no issues. And I think the "iterator" has nothing to do with Watir. I guess you defined your Watir cases respectively in the file "#{entry.strip}.rb". So I propose a possibility as follows: The files are loaded in the sequence of test\_aac, test\_aab, test\_aaa, but their results print in a random sequence depending on how much time needs to be taken before printing the results.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You may disclose more details to this mailing list.

Shiwei  
(The views expressed are my own and not necessarily those of Oracle and its affiliates.)

Brad Phelan wrote:

> **···**
>
> > Max Russell wrote:
> > 
> > > I have a harness I'm using for testing with Watir. It has become  
> > > apparent that the iterator is loading the tests defined based on the  
> > > order they appear in the directory as opposed to the order in the  
> > > testlist.
> > > 
> > > File.open('testlist.txt').each\_line do |entry|  
> > > &nbsp;&nbsp;self.send(:define\_method, entry.strip){ load "#{entry.strip}.rb"}
> > > 
> > > so if the testlist read something like
> > > 
> > > test\_aac  
> > > test\_aab  
> > > test\_aaa
> > > 
> > > instead of executing them in the order above it would do
> > > 
> > > test\_aaa  
> > > test\_aab  
> > > test\_aac
> > > 
> > > I'm probably being thick, but I'm not spotting where my iterator is  
> > > going wrong?
> > 
> > There is nothing wrong with your iterator here. However what  
> > you are doing with your iterator is calling define\_method  
> > which will generate methods ( presumably test methods )  
> > inside your test class.
> > 
> > I am guessing that the test framework is agnostic as to  
> > the order you define methods. It would probably pull them  
> > off the class with
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Class:instance\_methods
> > 
> > which I will probably generate a list in alphabetical order  
> > which is what you see.
> > 
> > --  
> > Brad Phelan  
> > [http://xtargets.com](http://xtargets.com)
