re-run unit test-suite

A

aidy.lewis

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
 
J

Jano Svitok

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?

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

2. 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
 
R

Ryan Davis

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?

Subclass the test suite, overriding setup with your parametrised
data. You'll reuse the test methods with different data for each
subclass.
 
A

aidy.lewis

Hi Jano,

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',
'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
 
J

Jano Svitok

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

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
 
A

aidy.lewis

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.
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

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
 
J

Jano Svitok

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top