# Re-run unit test-suite

**URL:** <https://www.ruby-forum.com/t/re-run-unit-test-suite/109643>\
**Category:** Ruby\
**Created:** [July 17, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643 "2007-07-17T15:32:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/1 "2007-07-17T15:32:22Z")

</div>

Hi,

I have parametrised the data of my unit test methods. However I would  
like to re-run the test class, but I am unable to do this with the  
require method.

require ‘test/unit’  
require ‘Test\_Suite\_1’  
require ‘Test\_Suite\_1’

Could anyone suggest an alternative method please?

Regards

Aidy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/2 "2007-07-17T15:38:23Z")

</div>

On 7/17/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could anyone suggest an alternative method please?  
> Hi,

I can see two choices:

1. create a custom runner (see the sources/rdoc how to do it) and run  
your cases twice.

or

1. move your dependent code out of the testcase so that your testcases  
are not dependent on the data changes but instead they contain/manage  
the data changes and call the appropriate tests.

Maybe if you could post some code how your data and tests looks like  
somebody would tell you more…

Jano

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/3 "2007-07-17T17:40:39Z")

</div>

On Jul 17, 2007, at 06:30 , removed\_email\_address@domain.invalid wrote:

> Could anyone suggest an alternative method please?  
> Subclass the test suite, overriding setup with your parametrised  
> data. You’ll reuse the test methods with different data for each  
> subclass.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2007, 1:32am UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/4 "2007-07-19T01:32:55Z")

</div>

On 7/18/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Aidy  
> Hi,

I have looked at the code, but I haven’t found a nice solution ☹  
That said, you can run your class by

require ‘test/unit/testsuite’  
require ‘test/unit/ui/console/testrunner’

class TS\_MyTests  
def self.suite  
suite = Test::Unit::TestSuite.new  
suite \<\< Test\_Suite\_1.suite  
suite \<\< Test\_Suite\_1.suite  
return suite  
end  
end

Test::Unit::UI::Console::TestRunner.run(TS\_MyTests)

It won’t help you as @@test is set only once - when the class is  
defined.

I wonder why do you use Test::Unit, when you barely use its features  
(by that I mean especially no asserts in the code). If it is indeed  
the case (no hidden asserts somewhere in the unpublished code), it  
might be easier to run your cases by your code.

Another way how to solve it would be to create custom TestSuite, that  
would set the data and call the test case.

class RepeatingTestSuite \< Test::Unit::TestSuite  
def initialize(count=1, name=“Unnamed TestSuite”)  
super(name)  
@count = count  
end

```
    def run(result, &progress_block)
            # set your data here
            @count.times do
                    super
            end
    end

    def size
            return @count * super
    end

```

end

class TS\_MyTests  
def self.suite  
suite = RepeatingTestSuite.new(2)  
suite \<\< Test\_Suite\_1.suite  
return suite  
end  
end

Another thing that came to my mind is that you can solve your  
xml-saving problem by creating a custom runner, that will store the  
results into xml instead of printing to console.

Then, you might consider storing your data directly in an array of  
hashes, or in the separate YAML file, though I don’t know why you did  
this way - where the data comes from etc.  
Now, you have a little bug, when you advance @@j by1 when you should by  
3

Finally, consider declaring creating object for one data set, and a  
special container for  
all the sets. That should clear your design at least bit.

If you add “each” method to your container you can write your run method  
as:

```
    def run(result, &progress_block)
            container.each do |data|
                    $current_data = data
                    super
            end
    end

```

Global variable is a dirty hack and should be avoided, but I cannot  
think of a better solution now.

BTW. using a lot of @@ class variables is a mark of a bad design  
IMHOI… not always, but usually. Maybe splitting into more classes  
will help.

And have a look at test/unit sources if you haven’t done  
already…there’s much more info that in the docs. Most of the code is  
easy, except that throw/catch part in the TestCase 😉

HTH,

Jano

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/5 "2007-07-19T19:57:32Z")

</div>

Hi Jano

> I have looked at the code,

Thanks I feel isolated working on this code on my own. Your effort is  
greatly appreciated.

> I wonder why do you use Test::Unit, when you barely use its features  
> (by that I mean especially no asserts in the code). If it is indeed  
> the case (no hidden asserts somewhere in the unpublished code), it  
> might be easier to run your cases by your code.

I just wanted to use the setup and teardown methods but maybe I am  
forcing a solution that doesn’t fit my problem.

> ```
> def run(result, &progress_block)
> 
> ```
> 
> class TS\_MyTests  
> def self.suite  
> suite = RepeatingTestSuite.new(2)  
> suite \<\< Test\_Suite\_1.suite  
> return suite  
> end  
> end

I will try

> BTW. using a lot of @@ class variables is a mark of a bad design  
> IMHOI… not always, but usually. Maybe splitting into more classes  
> will help.

Good point. I will refactor

> And have a look at test/unit sources if you haven’t done  
> already…there’s much more info that in the docs. Most of the code is  
> easy, except that throw/catch part in the TestCase 😉

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2007, 12:28am UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/6 "2007-07-20T00:28:13Z")

</div>

On 7/19/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I wonder why do you use Test::Unit, when you barely use its features  
> > (by that I mean especially no asserts in the code). If it is indeed  
> > the case (no hidden asserts somewhere in the unpublished code), it  
> > might be easier to run your cases by your code.
> 
> I just wanted to use the setup and teardown methods but maybe I am  
> forcing a solution that doesn’t fit my problem.

Maybe you could use test/unit more - instead of

@@log.test\_results(common.verify\_text(@@test.case\_1[:text\_to\_verify]),  
‘wrong message appeared’)

just write

assert\_equal(@@test.case\_1[:text\_to\_verify], text\_you\_got)

and let test/unit handle the rest for you, provided that you can fix  
the problem with the data.

J.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 18, 2007, 10:08am UTC](https://www.ruby-forum.com/t/re-run-unit-test-suite/109643/7 "2007-07-18T10:08:57Z")

</div>

Hi Jano,

On 17 Jul, 14:36, “Jano S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Maybe if you could post some code how your data and tests looks like  
> somebody would tell you more…
> 
> Jano

This is the test case

Test\_Suite\_1.rb

class Test\_Suite\_1 \< Test::Unit::TestCase  
@@log = Log.new  
@@test = Test::Data.new

def setup #runs before each and every method  
@browser = AUT::Browser.new  
@browser.goto(@@test.begin[:url])  
login = AUT::Login.new(@@test.begin[:username],  
@@test.begin[:password])  
login.sign\_in.click  
end

def teardown #runs after each and every method  
yahoo = AUT::Yahoo.new  
yahoo.log\_out.click  
@browser.close  
end

def test\_case\_1 #got to have a prefix of test  
@@log.detail\_test\_case(@@test.case\_1[:test\_name],  
@@test.case\_1[:test\_description])

```
#pages in sequence
main_page = AUT::Main_Page.new
common = AUT::Common.new

main_page.check_mail_button.click

```

@@log.test\_results(common.verify\_text(@@test.case\_1[:text\_to\_verify]),  
‘wrong message appeared’)  
#if test fails  
rescue =\> e  
@@log.test\_results(FALSE, e.message)  
ensure  
#this has to be on the last test  
@@log.write\_xml\_to\_file()  
end

end

The test data is here

test\_data.rb

module Test  
class Data  
attr\_accessor :begin, :case\_1

```
@@j = 0

  def initialize

  set_up=['http://edit.europe.yahoo.com/config/mail?.intl=uk',

```

‘aidy’, ‘whatever’,  
‘[http://edit.europe.yahoo.com/config/mail?.intl=uk](http://edit.europe.yahoo.com/config/mail?.intl=uk)’,  
‘aidy1’, ‘whatever1’]

```
  test_case_1=['test_case_1', 'check mail', 'There are no messages

```

in your Inbox.',  
‘test\_case\_1’, ‘check mail’, ‘Make a fail’]

```
  @begin= {
    :url => set_up[@@j],
    :username => set_up[@@j+1],
    :password => set_up[@@j+2]
    }

  @case_1={
    :test_name => test_case_1[@@j],
    :test_description => test_case_1[@@j+1],
    :text_to_verify => test_case_1[@@j+2]
    }

    puts @@j
    @@j+=1
  end

```

end  
end

I have been running the Test\_Suite\_1.rb from a batch file

system “test\_suite.bat”  
system “test\_suite.bat”

But I a unable to keep the value of @@j (in test\_data.rb) between the  
system calls, so as to use the next set off data in the array.

Thanks

Aidy
