# Random testing with Test::Unit

**URL:** <https://rubytalk.org/t/random-testing-with-test-unit/21107>\
**Category:** ruby-talk\
**Created:** [26 September 2005 17:10 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107 "2005-09-26T17:10:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [26 September 2005 17:10 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/1 "2005-09-26T17:10:10Z")

</div>

Does anybody else do random testing on their ruby code besides  
me? I picked it up because of my background (IC design) where  
random testing is one of the verification techniques of  
hardware.

So far, I've used 3 random testing strategies for my ruby code.  
Here are the different ways that the expected response is  
determined using those strategies:

- compare multiple implementations of the same functionality  
(i.e. StringIO vs. IO, ordered hash vs. Hash).

- AOP. Make a wrapper class or module that does pre and post  
checks of the methods. Use that wrapper class/module when  
testing. When the pre-check fails, the current test would be  
aborted/skipped (random generation of inputs yielded something  
invalid). When the post-check fails, the test would fail.

- determine the expected response (from some random input)  
within the test suite.

For all of these strategies, I've had to hack up Test::Unit in  
the same way to add various features. Here are the things I've  
added:

+ instead of running the test\_\* methods in a fixed order once,  
run them in a random order for N passes.

+ abort testing when the first failure is reached.

+ easy way to skip the rest of the current test (when the  
random input is invalid). Don't include this in the number of  
tests or mark it as skipped.

+ way to pass number of passes of the test\_\* methods on the  
command line.

+ display random seed and way of passing it in on the command  
line.

+ test suite should have access to the --verbose level or  
another switch to control the debugging verbosity.

+ way to pass command-line options down to the test suite to  
control various things - what methods to test, what  
classes to test, various other flags.

I would like to see most of these (optional) features added to  
test/unit. Anybody else have opinions on this subject?

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! for Good  
> Donate to the Hurricane Katrina relief effort.  
> [http://store.yahoo.com/redcross-donate3/](http://store.yahoo.com/redcross-donate3/)

---

<div class="post-metadata">

**Author:** ![Kev\_Jackson2](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Kev\_Jackson2](https://rubytalk.org/u/Kev_Jackson2)\
**Post date:** [27 September 2005 02:44 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/2 "2005-09-27T02:44:59Z")

</div>

> - AOP. Make a wrapper class or module that does pre and post  
> checks of the methods. Use that wrapper class/module when  
> testing. When the pre-check fails, the current test would be  
> aborted/skipped (random generation of inputs yielded something  
> invalid). When the post-check fails, the test would fail.

Hi I'd love to see how you implemented this as I wanted to write my own AOP-like code for ruby but I got stuck with my lack of ruby knowledge!

Kev

---

<div class="post-metadata">

**Author:** ![Eivind\_Eklund](https://avatars.discourse-cdn.com/v4/letter/e/f17d59/32.png) [@Eivind\_Eklund](https://rubytalk.org/u/Eivind_Eklund)\
**Post date:** [27 September 2005 16:24 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/3 "2005-09-27T16:24:28Z")

</div>

> Does anybody else do random testing on their ruby code besides  
> me? I picked it up because of my background (IC design) where  
> random testing is one of the verification techniques of  
> hardware.

I cannot remember having used random testing for Ruby code, though I've used  
it for code in other languages. Having a nice framework for it would be,  
well, nice.

For all of these strategies, I've had to hack up Test::Unit in

> the same way to add various features. Here are the things I've  
> added:
> 
> + instead of running the test\_\* methods in a fixed order once,  
> run them in a random order for N passes.

Running N times sounds like a subclass of TestCase would be appropriate.  
Otherwise, how do you distinguish between what tests should be run once and  
which should be run several time (for statistical coverage)?

Random order sounds reasonable for everything, anyway.

+ abort testing when the first failure is reached.

Also subclass material.

+ easy way to skip the rest of the current test (when the

> random input is invalid). Don't include this in the number of  
> tests or mark it as skipped.

Also subclass material, I think.

+ way to pass number of passes of the test\_\* methods on the

> command line.
> 
> + display random seed and way of passing it in on the command  
> line.
> 
> + test suite should have access to the --verbose level or  
> another switch to control the debugging verbosity.

My gut feeling is that this is inappropriate. I see a major part of the  
Test::Unit style testing as the fact that the programmer only has to check  
for pass/fail. Passing through verbiosity information seems to turn this on  
its head, encouraging setting up the output for inspection. (On the other  
hand, I have sometimes needed information for debugging - calling this  
"debug level" would do much to ease my concerns...)

+ way to pass command-line options down to the test suite to

> control various things - what methods to test, what  
> classes to test, various other flags.

I also get an initial bad feeling about this, as it makes it totally  
necessary to use the console-based testrunner.

Eivind.

> **···**
>
> On 9/26/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:  
> --  
> Hazzle free packages for Ruby?  
> RPA is available from [http://www.rubyarchive.org/](http://www.rubyarchive.org/)

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [27 September 2005 15:58 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/4 "2005-09-27T15:58:57Z")

</div>

I've attached an example (executable) of one way to do AOP and  
random testing for testing Array#push and Array#pop. I'm no  
expert in AOP and exactly what its definition is (did it before  
knowing what it was), but here are the ways I can think of for  
doing AOP:

- make a derived class for the class under test (what this  
example does). Put the pre/post tests around a call to "super"  
for each method. If you need to use other orginal (super)  
methods in your pre/post tests (that you've also put pre/post  
tests in), you have to jump through hoops to do it.

- make a wrapper class for testing the methods of an object.  
new/initialize should take this object and store it in an  
instance variable. For each method, put pre/post tests around  
a call to the object's method.

- directly modify the class under test. Alias each original  
method and redefine each method to do pre/post test around  
calls to the original (aliased) method.

I was also thinking you might be able to do this stuff in a  
module and include it, but I couldn't figure it out.

Again, AOP is only one way of getting expected results for  
random testing. I mentioned 2 others.

You can see from this example how several features (mentioned  
in my previous message) would be useful for random testing.

[test\_array.rb](https://rubytalk.org/uploads/short-url/uu9LHKUa4SO46JvY8RNWjQ5OPl8.rb) (1.24 KB)

> **···**
>
> --- Kev Jackson \<kevin.jackson@it.fts-vn.com\> wrote:
> 
> > \>- AOP. Make a wrapper class or module that does pre and  
> > post  
> > \>checks of the methods. Use that wrapper class/module when  
> > \>testing. When the pre-check fails, the current test would  
> > be  
> > \>aborted/skipped (random generation of inputs yielded  
> > something  
> > \>invalid). When the post-check fails, the test would fail.  
> > \>  
> > \>  
> > Hi I'd love to see how you implemented this as I wanted to  
> > write my own  
> > AOP-like code for ruby but I got stuck with my lack of ruby  
> > knowledge!
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [27 September 2005 17:14 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/5 "2005-09-27T17:14:14Z")

</div>

I agree that a derived class of Test::Unit::TestCase (maybe  
Test::Unit::RandomTestCase) would be appropriate. I tried this  
first. Unfortunately, I found several places where doing this  
became problematic - test runners/collectors, rubygems?? (don't  
remember now). There is the assumption that all derived  
classes of Test::Unit::TestCase are the actual test case  
classes for testing (not the case for this  
Test::Unit::RandomTestCase class). At the time, it seemed  
easier to hack into Test::Unit::TestCase and  
Test::Unit::TestSuite. Now I'm hacking into TestSuite#tests to  
redefine tests.each. It still doesn't seem like the right way.  
Some derived classes that handle random testing  
(RandomTestCase and RandomTestSuite) seems like the right way.

> \>  
> \> Does anybody else do random testing on their ruby code  
> besides  
> \> me? I picked it up because of my background (IC design)  
> where  
> \> random testing is one of the verification techniques of  
> \> hardware.
> 
> I cannot remember having used random testing for Ruby code,  
> though I've used  
> it for code in other languages. Having a nice framework for  
> it would be,  
> well, nice.
> 
> For all of these strategies, I've had to hack up Test::Unit  
> in  
> \> the same way to add various features. Here are the things  
> I've  
> \> added:  
> \>  
> \> + instead of running the test\_\* methods in a fixed order  
> once,  
> \> run them in a random order for N passes.
> 
> Running N times sounds like a subclass of TestCase would be  
> appropriate.  
> Otherwise, how do you distinguish between what tests should  
> be run once and  
> which should be run several time (for statistical coverage)?
> 
> Random order sounds reasonable for everything, anyway.
> 
> + abort testing when the first failure is reached.
> 
> Also subclass material.
> 
> + easy way to skip the rest of the current test (when the  
> \> random input is invalid). Don't include this in the number  
> of  
> \> tests or mark it as skipped.
> 
> Also subclass material, I think.
> 
> + way to pass number of passes of the test\_\* methods on the  
> \> command line.  
> \>  
> \> + display random seed and way of passing it in on the  
> command  
> \> line.  
> \>  
> \> + test suite should have access to the --verbose level or  
> \> another switch to control the debugging verbosity.
> 
> My gut feeling is that this is inappropriate. I see a major  
> part of the  
> Test::Unit style testing as the fact that the programmer only  
> has to check  
> for pass/fail. Passing through verbiosity information seems  
> to turn this on  
> its head, encouraging setting up the output for inspection.  
> (On the other  
> hand, I have sometimes needed information for debugging -  
> calling this  
> "debug level" would do much to ease my concerns...)
> 
> + way to pass command-line options down to the test suite to  
> \> control various things - what methods to test, what  
> \> classes to test, various other flags.
> 
> I also get an initial bad feeling about this, as it makes it  
> totally  
> necessary to use the console-based testrunner.

As you guessed, I use random testing for both testing and  
debugging. With hand-coded tests, there is not as big a need  
for spitting out debug info, because the failure points you to  
the line # and you can gather what the inputs were to easily  
recreate the problem. This is not so with random testing -  
especially when there is some state carried over from one  
random test to the next (not done in hand-coded tests). When  
you have failures with random testing, you need a way to track  
down exactly what happened. You need extra debug info.

Also since random testing can run many more tests and still be  
useful (thousands - even millions), having the ability to  
narrow the testing down to a specific area to test/debug would  
be useful.

I've never used anything but a console test runner. Where does  
stdout/stderr go for the other runners. If you put it in a log  
file, it seems like it could still be useful for other runners.

> **···**
>
> --- Eivind Eklund \<eeklund@gmail.com\> wrote:
> 
> > On 9/26/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:
> 
> > Eivind.  
> > --  
> > Hazzle free packages for Ruby?  
> > RPA is available from [http://www.rubyarchive.org/](http://www.rubyarchive.org/)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! for Good  
> Donate to the Hurricane Katrina relief effort.  
> [http://store.yahoo.com/redcross-donate3/](http://store.yahoo.com/redcross-donate3/)

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [27 September 2005 19:48 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/6 "2005-09-27T19:48:09Z")

</div>

I gave another shot at making a derived TestCase class. I only  
needed one hack to effectively make it ignore this class when  
running tests: undef\_method(:default\_test). Here is what I  
came up with that has many of the features I wanted:

require 'test/unit'

module Test  
&nbsp;&nbsp;module Unit  
&nbsp;&nbsp;&nbsp;&nbsp;class RandomTestCase \< TestCase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.suite  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$debug\_level = (ENV['DEBUG\_LEVEL']||0).to\_i if  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$debug\_level.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if $random\_seed.nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$random\_seed = ENV['RAND\_SEED'].to\_i.nonzero? ||  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(srand;srand)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srand($random\_seed)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts("random\_seed: #{$random\_seed}") if  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$debug\_level\>=1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;random\_iterations = (ENV['RAND\_ITER']||8).to\_f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;methods = Regexp.new(ENV['TEST\_METHODS']||".\*")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;suite = super  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tests = suite.tests  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tests.reject! { |t|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!methods.match(t.name.gsub(/\Atest\_/,''))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(class \<\< tests;self;end).class\_eval {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def each  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(:stop\_suite) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@iterations\*size).to\_i.times {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(:invalid\_test) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(slice(rand(size)))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tests.instance\_eval { @iterations = random\_iterations }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;suite  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;undef\_method(:default\_test)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not passed?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts("\nrandom\_seed: #{$random\_seed}")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw(:stop\_suite)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Here is that example I gave earlier using the above for random  
testing Array#push and Array#pop using AOP:

class ArrayAOP \< Array  
&nbsp;&nbsp;include Test::Unit::Assertions  
&nbsp;&nbsp;def push(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;print("#{self.inspect}.push(\*#{args.inspect}) -\> ") if  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$debug\_level\>=1  
&nbsp;&nbsp;&nbsp;&nbsp;n = size  
&nbsp;&nbsp;&nbsp;&nbsp;na = args.size  
&nbsp;&nbsp;&nbsp;&nbsp;ret = super  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(n+na,size)  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(slice(-na,na),args)  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_same(self,ret)  
&nbsp;&nbsp;&nbsp;&nbsp;p(ret) if $debug\_level\>=1  
&nbsp;&nbsp;&nbsp;&nbsp;ret  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def pop  
&nbsp;&nbsp;&nbsp;&nbsp;print("#{self.inspect}.pop -\> ") if $debug\_level\>=1  
&nbsp;&nbsp;&nbsp;&nbsp;n = size  
&nbsp;&nbsp;&nbsp;&nbsp;ret0 = n.nonzero? ? slice(-1) : nil  
&nbsp;&nbsp;&nbsp;&nbsp;ret = super  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(ret0,ret)  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(n.nonzero? ? n-1 : 0,size)  
&nbsp;&nbsp;&nbsp;&nbsp;p(ret) if $debug\_level\>=1  
&nbsp;&nbsp;&nbsp;&nbsp;ret  
&nbsp;&nbsp;end  
end

class ArrayTest \< Test::Unit::RandomTestCase  
&nbsp;&nbsp;def self.suite  
&nbsp;&nbsp;&nbsp;&nbsp;@@object = ArrayAOP  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def test\_push  
&nbsp;&nbsp;&nbsp;&nbsp;args =   
&nbsp;&nbsp;&nbsp;&nbsp;rand(4).times {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args \<\< [0,"",,nil,false][rand(5)]  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;@@object.push(\*args)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def test\_pop  
&nbsp;&nbsp;&nbsp;&nbsp;rand(4).times {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@object.pop  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;end  
end

As you can see from above, with this RandomTestCase class, you  
can do random testing very easily. But, it still would be nice  
to get options from the command line rather than the  
environment as I'm doing in RandomTestCase.

> **···**
>
> --- Eivind Eklund \<eeklund@gmail.com\> wrote:
> 
> > On 9/26/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:  
> > \>  
> > \> Does anybody else do random testing on their ruby code  
> > besides  
> > \> me? I picked it up because of my background (IC design)  
> > where  
> > \> random testing is one of the verification techniques of  
> > \> hardware.
> > 
> > I cannot remember having used random testing for Ruby code,  
> > though I've used  
> > it for code in other languages. Having a nice framework for  
> > it would be,  
> > well, nice.
> > 
> > For all of these strategies, I've had to hack up Test::Unit  
> > in  
> > \> the same way to add various features. Here are the things  
> > I've  
> > \> added:  
> > \>  
> > \> + instead of running the test\_\* methods in a fixed order  
> > once,  
> > \> run them in a random order for N passes.
> > 
> > Running N times sounds like a subclass of TestCase would be  
> > appropriate.  
> > Otherwise, how do you distinguish between what tests should  
> > be run once and  
> > which should be run several time (for statistical coverage)?
> > 
> > Random order sounds reasonable for everything, anyway.
> > 
> > + abort testing when the first failure is reached.
> > 
> > Also subclass material.
> > 
> > + easy way to skip the rest of the current test (when the  
> > \> random input is invalid). Don't include this in the number  
> > of  
> > \> tests or mark it as skipped.
> > 
> > Also subclass material, I think.
> > 
> > + way to pass number of passes of the test\_\* methods on the  
> > \> command line.  
> > \>  
> > \> + display random seed and way of passing it in on the  
> > command  
> > \> line.  
> > \>  
> > \> + test suite should have access to the --verbose level or  
> > \> another switch to control the debugging verbosity.
> > 
> > My gut feeling is that this is inappropriate. I see a major  
> > part of the  
> > Test::Unit style testing as the fact that the programmer only  
> > has to check  
> > for pass/fail. Passing through verbiosity information seems  
> > to turn this on  
> > its head, encouraging setting up the output for inspection.  
> > (On the other  
> > hand, I have sometimes needed information for debugging -  
> > calling this  
> > "debug level" would do much to ease my concerns...)
> > 
> > + way to pass command-line options down to the test suite to  
> > \> control various things - what methods to test, what  
> > \> classes to test, various other flags.
> > 
> > I also get an initial bad feeling about this, as it makes it  
> > totally  
> > necessary to use the console-based testrunner.
> > 
> > Eivind.  
> > --  
> > Hazzle free packages for Ruby?  
> > RPA is available from [http://www.rubyarchive.org/](http://www.rubyarchive.org/)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Jeff\_Wood](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Jeff\_Wood](https://rubytalk.org/u/Jeff_Wood)\
**Post date:** [27 September 2005 21:02 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/7 "2005-09-27T21:02:57Z")

</div>

Why do you need DEBUG LEVEL or whatever when the Ruby  
environment/interpreter already has $VERBOSE ... ???

I mean, it's already there ... just turn up the verbosity ...

ruby -v mytestcases.rb

... and your done ... ( am i missing something? )

j.

> **···**
>
> On 9/27/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:
> 
> > I gave another shot at making a derived TestCase class. I only  
> > needed one hack to effectively make it ignore this class when  
> > running tests: undef\_method(:default\_test). Here is what I  
> > came up with that has many of the features I wanted:
> > 
> > require 'test/unit'
> > 
> > module Test  
> > module Unit  
> > class RandomTestCase \< TestCase  
> > def self.suite  
> > $debug\_level = (ENV['DEBUG\_LEVEL']||0).to\_i if  
> > $debug\_level.nil?  
> > if $random\_seed.nil?  
> > $random\_seed = ENV['RAND\_SEED'].to\_i.nonzero? ||  
> > (srand;srand)  
> > srand($random\_seed)  
> > puts("random\_seed: #{$random\_seed}") if  
> > $debug\_level\>=1  
> > end  
> > random\_iterations = (ENV['RAND\_ITER']||8).to\_f  
> > methods = Regexp.new(ENV['TEST\_METHODS']||".\*")  
> > suite = super  
> > tests = suite.tests  
> > tests.reject! { |t|  
> > !methods.match(t.name.gsub(/\Atest\_/,''))  
> > }  
> > (class \<\< tests;self;end).class\_eval {  
> > def each  
> > catch(:stop\_suite) {  
> > (@iterations\*size).to\_i.times {  
> > catch(:invalid\_test) {  
> > yield(slice(rand(size)))  
> > }  
> > }  
> > }  
> > end  
> > }  
> > tests.instance\_eval { @iterations = random\_iterations }  
> > suite  
> > end  
> > undef\_method(:default\_test)  
> > def teardown  
> > if not passed?  
> > puts("\nrandom\_seed: #{$random\_seed}")  
> > throw(:stop\_suite)  
> > end  
> > end  
> > end  
> > end  
> > end
> > 
> > Here is that example I gave earlier using the above for random  
> > testing Array#push and Array#pop using AOP:
> > 
> > class ArrayAOP \< Array  
> > include Test::Unit::Assertions  
> > def push(\*args)  
> > print("#{self.inspect}.push(\*#{args.inspect}) -\> ") if  
> > $debug\_level\>=1  
> > n = size  
> > na = args.size  
> > ret = super  
> > assert\_equal(n+na,size)  
> > assert\_equal(slice(-na,na),args)  
> > assert\_same(self,ret)  
> > p(ret) if $debug\_level\>=1  
> > ret  
> > end  
> > def pop  
> > print("#{self.inspect}.pop -\> ") if $debug\_level\>=1  
> > n = size  
> > ret0 = n.nonzero? ? slice(-1) : nil  
> > ret = super  
> > assert\_equal(ret0,ret)  
> > assert\_equal(n.nonzero? ? n-1 : 0,size)  
> > p(ret) if $debug\_level\>=1  
> > ret  
> > end  
> > end
> > 
> > class ArrayTest \< Test::Unit::RandomTestCase  
> > def self.suite  
> > @@object = ArrayAOP  
> > super  
> > end  
> > def test\_push  
> > args =   
> > rand(4).times {  
> > args \<\< [0,"",,nil,false][rand(5)]  
> > }  
> > @@object.push(\*args)  
> > end  
> > def test\_pop  
> > rand(4).times {  
> > @@object.pop  
> > }  
> > end  
> > end
> > 
> > As you can see from above, with this RandomTestCase class, you  
> > can do random testing very easily. But, it still would be nice  
> > to get options from the command line rather than the  
> > environment as I'm doing in RandomTestCase.
> > 
> > --- Eivind Eklund \<eeklund@gmail.com\> wrote:
> > 
> > \> On 9/26/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:  
> > \> \>  
> > \> \> Does anybody else do random testing on their ruby code  
> > \> besides  
> > \> \> me? I picked it up because of my background (IC design)  
> > \> where  
> > \> \> random testing is one of the verification techniques of  
> > \> \> hardware.  
> > \>  
> > \>  
> > \> I cannot remember having used random testing for Ruby code,  
> > \> though I've used  
> > \> it for code in other languages. Having a nice framework for  
> > \> it would be,  
> > \> well, nice.  
> > \>  
> > \> For all of these strategies, I've had to hack up Test::Unit  
> > \> in  
> > \> \> the same way to add various features. Here are the things  
> > \> I've  
> > \> \> added:  
> > \> \>  
> > \> \> + instead of running the test\_\* methods in a fixed order  
> > \> once,  
> > \> \> run them in a random order for N passes.  
> > \>  
> > \>  
> > \> Running N times sounds like a subclass of TestCase would be  
> > \> appropriate.  
> > \> Otherwise, how do you distinguish between what tests should  
> > \> be run once and  
> > \> which should be run several time (for statistical coverage)?  
> > \>  
> > \> Random order sounds reasonable for everything, anyway.  
> > \>  
> > \>  
> > \> + abort testing when the first failure is reached.  
> > \>  
> > \>  
> > \> Also subclass material.  
> > \>  
> > \> + easy way to skip the rest of the current test (when the  
> > \> \> random input is invalid). Don't include this in the number  
> > \> of  
> > \> \> tests or mark it as skipped.  
> > \>  
> > \>  
> > \> Also subclass material, I think.  
> > \>  
> > \> + way to pass number of passes of the test\_\* methods on the  
> > \> \> command line.  
> > \> \>  
> > \> \> + display random seed and way of passing it in on the  
> > \> command  
> > \> \> line.  
> > \> \>  
> > \> \> + test suite should have access to the --verbose level or  
> > \> \> another switch to control the debugging verbosity.  
> > \>  
> > \>  
> > \> My gut feeling is that this is inappropriate. I see a major  
> > \> part of the  
> > \> Test::Unit style testing as the fact that the programmer only  
> > \> has to check  
> > \> for pass/fail. Passing through verbiosity information seems  
> > \> to turn this on  
> > \> its head, encouraging setting up the output for inspection.  
> > \> (On the other  
> > \> hand, I have sometimes needed information for debugging -  
> > \> calling this  
> > \> "debug level" would do much to ease my concerns...)  
> > \>  
> > \> + way to pass command-line options down to the test suite to  
> > \> \> control various things - what methods to test, what  
> > \> \> classes to test, various other flags.  
> > \>  
> > \>  
> > \> I also get an initial bad feeling about this, as it makes it  
> > \> totally  
> > \> necessary to use the console-based testrunner.  
> > \>  
> > \> Eivind.  
> > \> --  
> > \> Hazzle free packages for Ruby?  
> > \> RPA is available from [http://www.rubyarchive.org/](http://www.rubyarchive.org/)  
> > \>
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Yahoo! Mail - PC Magazine Editors' Choice 2005  
> > [http://mail.yahoo.com](http://mail.yahoo.com)
> 
> --  
> "[http://ruby-lang.org](http://ruby-lang.org) -- do you ruby?"
> 
> Jeff Wood

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [27 September 2005 22:17 UTC](https://rubytalk.org/t/random-testing-with-test-unit/21107/8 "2005-09-27T22:17:22Z")

</div>

Yes, there are several ruby switches to control  
warnings/debugging/verbosity:

&nbsp;&nbsp;-d set debugging flags (set $DEBUG to true)  
&nbsp;&nbsp;-v print version number, then turn on verbose  
mode  
&nbsp;&nbsp;-w turn warnings on for your script  
&nbsp;&nbsp;-W[level] set warning level; 0=silence, 1=medium,  
2=verbose (default)

In addition to setting some global variables ($VERBOSE, $DEBUG,  
$-v, $-w, $-d), these turn on warnings that some ruby code may  
not like (produce excess warnings). -d probably also turns on  
some internal ruby debugging, but I don't know where.

In addition to this, the autorunner in test/unit has  
--verbose[=level]. I would like to reuse this, but it is not  
very easy to access (can get it through  
ObjectSpace.each\_object).

The TEST\_METHODS below isn't needed as test/unit/autorunner  
already has a --name option which works fine for this.

> **···**
>
> --- Jeff Wood \<jeff.darklight@gmail.com\> wrote:
> 
> > Why do you need DEBUG LEVEL or whatever when the Ruby  
> > environment/interpreter already has $VERBOSE ... ???
> > 
> > I mean, it's already there ... just turn up the verbosity ...
> > 
> > ruby -v mytestcases.rb
> > 
> > ... and your done ... ( am i missing something? )
> > 
> > j.
> > 
> > On 9/27/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:  
> > \>  
> > \> I gave another shot at making a derived TestCase class. I  
> > only  
> > \> needed one hack to effectively make it ignore this class  
> > when  
> > \> running tests: undef\_method(:default\_test). Here is what I  
> > \> came up with that has many of the features I wanted:  
> > \>  
> > \> require 'test/unit'  
> > \>  
> > \> module Test  
> > \> module Unit  
> > \> class RandomTestCase \< TestCase  
> > \> def self.suite  
> > \> $debug\_level = (ENV['DEBUG\_LEVEL']||0).to\_i if  
> > \> $debug\_level.nil?  
> > \> if $random\_seed.nil?  
> > \> $random\_seed = ENV['RAND\_SEED'].to\_i.nonzero? ||  
> > \> (srand;srand)  
> > \> srand($random\_seed)  
> > \> puts("random\_seed: #{$random\_seed}") if  
> > \> $debug\_level\>=1  
> > \> end  
> > \> random\_iterations = (ENV['RAND\_ITER']||8).to\_f  
> > \> methods = Regexp.new(ENV['TEST\_METHODS']||".\*")  
> > \> suite = super  
> > \> tests = suite.tests  
> > \> tests.reject! { |t|  
> > \> !methods.match(t.name.gsub(/\Atest\_/,''))  
> > \> }  
> > \> (class \<\< tests;self;end).class\_eval {  
> > \> def each  
> > \> catch(:stop\_suite) {  
> > \> (@iterations\*size).to\_i.times {  
> > \> catch(:invalid\_test) {  
> > \> yield(slice(rand(size)))  
> > \> }  
> > \> }  
> > \> }  
> > \> end  
> > \> }  
> > \> tests.instance\_eval { @iterations = random\_iterations }  
> > \> suite  
> > \> end  
> > \> undef\_method(:default\_test)  
> > \> def teardown  
> > \> if not passed?  
> > \> puts("\nrandom\_seed: #{$random\_seed}")  
> > \> throw(:stop\_suite)  
> > \> end  
> > \> end  
> > \> end  
> > \> end  
> > \> end  
> > \>  
> > \>  
> > \> Here is that example I gave earlier using the above for  
> > random  
> > \> testing Array#push and Array#pop using AOP:  
> > \>  
> > \>  
> > \> class ArrayAOP \< Array  
> > \> include Test::Unit::Assertions  
> > \> def push(\*args)  
> > \> print("#{self.inspect}.push(\*#{args.inspect}) -\> ") if  
> > \> $debug\_level\>=1  
> > \> n = size  
> > \> na = args.size  
> > \> ret = super  
> > \> assert\_equal(n+na,size)  
> > \> assert\_equal(slice(-na,na),args)  
> > \> assert\_same(self,ret)  
> > \> p(ret) if $debug\_level\>=1  
> > \> ret  
> > \> end  
> > \> def pop  
> > \> print("#{self.inspect}.pop -\> ") if $debug\_level\>=1  
> > \> n = size  
> > \> ret0 = n.nonzero? ? slice(-1) : nil  
> > \> ret = super  
> > \> assert\_equal(ret0,ret)  
> > \> assert\_equal(n.nonzero? ? n-1 : 0,size)  
> > \> p(ret) if $debug\_level\>=1  
> > \> ret  
> > \> end  
> > \> end  
> > \>  
> > \> class ArrayTest \< Test::Unit::RandomTestCase  
> > \> def self.suite  
> > \> @@object = ArrayAOP  
> > \> super  
> > \> end  
> > \> def test\_push  
> > \> args =   
> > \> rand(4).times {  
> > \> args \<\< [0,"",,nil,false][rand(5)]  
> > \> }  
> > \> @@object.push(\*args)  
> > \> end  
> > \> def test\_pop  
> > \> rand(4).times {  
> > \> @@object.pop  
> > \> }  
> > \> end  
> > \> end  
> > \>  
> > \>  
> > \> As you can see from above, with this RandomTestCase class,  
> > you  
> > \> can do random testing very easily. But, it still would be  
> > nice  
> > \> to get options from the command line rather than the  
> > \> environment as I'm doing in RandomTestCase.  
> > \>  
> > \> --- Eivind Eklund \<eeklund@gmail.com\> wrote:  
> > \>  
> > \> \> On 9/26/05, Eric Mahurin \<eric\_mahurin@yahoo.com\> wrote:  
> > \> \> \>  
> > \> \> \> Does anybody else do random testing on their ruby code  
> > \> \> besides  
> > \> \> \> me? I picked it up because of my background (IC design)  
> > \> \> where  
> > \> \> \> random testing is one of the verification techniques of  
> > \> \> \> hardware.  
> > \> \>  
> > \> \>  
> > \> \> I cannot remember having used random testing for Ruby  
> > code,  
> > \> \> though I've used  
> > \> \> it for code in other languages. Having a nice framework  
> > for  
> > \> \> it would be,  
> > \> \> well, nice.  
> > \> \>  
> > \> \> For all of these strategies, I've had to hack up  
> > Test::Unit  
> > \> \> in  
> > \> \> \> the same way to add various features. Here are the  
> > things  
> > \> \> I've  
> > \> \> \> added:  
> > \> \> \>  
> > \> \> \> + instead of running the test\_\* methods in a fixed  
> > order  
> > \> \> once,  
> > \> \> \> run them in a random order for N passes.  
> > \> \>  
> > \> \>  
> > \> \> Running N times sounds like a subclass of TestCase would  
> > be  
> > \> \> appropriate.  
> > \> \> Otherwise, how do you distinguish between what tests  
> > should  
> > \> \> be run once and  
> > \> \> which should be run several time (for statistical  
> > coverage)?  
> > \> \>  
> > \> \> Random order sounds reasonable for everything, anyway.  
> > \> \>  
> > \> \>  
> > \> \> + abort testing when the first failure is reached.  
> > \> \>  
> > \> \>  
> > \> \> Also subclass material.  
> > \> \>  
> > \> \> + easy way to skip the rest of the current test (when the  
> > \> \> \> random input is invalid). Don't include this in the  
> > number  
> > \> \> of  
> > \> \> \> tests or mark it as skipped.  
> > \> \>  
> > \> \>  
> > \> \> Also subclass material, I think.  
> > \> \>  
> > \> \> + way to pass number of passes of the test\_\* methods on  
> > the  
> > \> \> \> command line.  
> > \> \> \>  
> > \> \> \> + display random seed and way of passing it in on the  
> > \> \> command  
> > \> \> \> line.  
> > \> \> \>  
> > \> \> \> + test suite should have access to the --verbose level  
> > or  
> > \> \> \> another switch to control the debugging verbosity.  
> > \> \>  
> > \> \>  
> > \> \> My gut feeling is that this is inappropriate. I see a  
> > major  
> > \> \> part of the  
> > \> \> Test::Unit style testing as the fact that the programmer  
> > only  
> > \> \> has to check  
> > \> \> for pass/fail. Passing through verbiosity information  
> > seems  
> > \> \> to turn this on  
> > \> \> its head, encouraging setting up the output for  
> > inspection.  
> > \> \> (On the other  
> > \> \> hand, I have sometimes needed information for debugging -  
> > \> \> calling this  
> > \> \> "debug level" would do much to ease my concerns...)  
> > \> \>  
> > \> \> + way to pass command-line options down to the test suite  
> > to  
> > \> \> \> control various things - what methods to test, what  
> > \> \> \> classes to test, various other flags.  
> > \> \>  
> > \> \>  
> > \> \> I also get an initial bad feeling about this, as it makes  
> > it  
> > \> \> totally  
> > \> \> necessary to use the console-based testrunner.  
> > \> \>  
> > \> \> Eivind.  
> > \> \> --  
> > \> \> Hazzle free packages for Ruby?  
> > \> \> RPA is available from [http://www.rubyarchive.org/](http://www.rubyarchive.org/)  
> > \> \>  
> > \>  
> > \>  
> > \>  
> > \>  
> > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \> Yahoo! Mail - PC Magazine Editors' Choice 2005  
> > \> [http://mail.yahoo.com](http://mail.yahoo.com)  
> > \>  
> > \>
> > 
> > --  
> > "[http://ruby-lang.org](http://ruby-lang.org) -- do you ruby?"
> > 
> > Jeff Wood
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...
