[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

calling a testcase from another class

Mario Ruiz

2/6/2008 10:14:00 AM

Hi everybody,
I have a few classes containing a few tescase methods.
I would like to create a new class in order to run different tescases
methods of different classes:

file: testcases/myclass1.rb
class Myclass1 < Test::Unit::Testcase
def setup
end
def test01_example
end
def test02_example
end
def test03_example
end
def teardown
end
end

file: testcases/myclass2.rb
class Myclass2 < Test::Unit::Testcase
def setup
end
def test01_example
end
def test02_example
end
def teardown
end
end

file: testsuites/running.rb
class RunningTestCases < Test::Unit::Testcase
def setup
end
def test01_beep

require 'testcases/myclass1'
Myclass1.new().test03_example()
Myclass1.new().test01_example()

require 'testcases/myclass2'
Myclass2.new().test01_example()
end
def teardown
end
end


This is more or less what I'm trying to do, but it's not working.

Any idea?
--
Posted via http://www.ruby-....

9 Answers

Phlip

2/6/2008 2:25:00 PM

0

Mario Ruiz wrote:

> I have a few classes containing a few tescase methods. I would like to
> create a new class in order to run different tescases methods of
> different classes:

Why?

Yes there are reasons, but generally test cases should not be reusable.
Push whatever code you need to reuse into "helper" classes, and inherit
these into your test cases, as raw functions, not test cases.

module MyTestStuff
def test01_example
# reuse an entire case here
end
def assemble_example
# reuse the code that assembles your test targets here
@assembly = whatever
end
def assert_example
# reuse your application-specific assertion here
end
end

class Myclass2 < Test::Unit::Testcase
include MyTestStuff
def setup
assemble_example
end
def test01_example
assert 42 == @assembly
end
def test02_example
assert_example
end
end # this will also run test01_example

> def test01_beep
>
> require 'testcases/myclass1'
> Myclass1.new().test03_example()
> Myclass1.new().test01_example()
>
> require 'testcases/myclass2'
> Myclass2.new().test01_example()

Okay, why?

If you reeally need to do that (I can't think of a reason that doesn't
lead to coupling your code), then include MyTestStuff, and just call into
the whole list directly:

test03_example()
test01_example()
test01_example()

(Also, google for "assert 2.0", to help simplify your assertions. It
folds all the minor variations, such as assert_equal, assert_match, into
one powerful assert{ anything }.)

--
Phlip

Mario Ruiz

2/7/2008 9:54:00 AM

0

In the example I was inheriting from Test::Unit::Testcase but I'm not
doing that in the real example, I'm inheriting from a superclass that is
inheriting from Test::Unit::Testcase, but I think it doesn't matter.

So any ideas..... I'll really appreciate it.

Thank you.
--
Posted via http://www.ruby-....

Thomas Preymesser

2/7/2008 12:43:00 PM

0

Hi Mario,


On 06/02/2008, Mario Ruiz <mario@betware.com> wrote:
>
> Hi everybody,
> I have a few classes containing a few tescase methods.
> I would like to create a new class in order to run different tescases
> methods of different classes:
>
> file: testcases/myclass1.rb
> class Myclass1 < Test::Unit::Testcase


it's class Myclass1 < Test::Unit::TestCase


> file: testsuites/running.rb
> class RunningTestCases < Test::Unit::Testcase
> def setup
> end
> def test01_beep
>
> require 'testcases/myclass1'
> Myclass1.new().test03_example()
> Myclass1.new().test01_example()


are you coming from Java-Programming?


you don't need all the stuff in your running.rb script.

Just create a running.rb file with this content:

require 'test/unit'
require 'myclass1'
require 'myclass2'

Calling this script all your tests will be executed.

For further examples you might read some tutorials for organizing test in
test-cases and test-suites.

-Thomas





>


--=20
Thomas Preymesser
thopre@gmail.com
thomas@thopre.com
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.word...
http://www.t...

Mario Ruiz

2/7/2008 12:50:00 PM

0

Hi Thomas, I knew it.
What I was trying to do is to run not all the test cases in each file.
For example only test03_example() and test01_example() from MyClass1
and test01_example() from MyClass2

--
Posted via http://www.ruby-....

Mario Ruiz

2/8/2008 10:38:00 AM

0

Another solution could be using the 'include' sentence but the result is
the same, it doesn't work
--
Posted via http://www.ruby-....

Thomas Preymesser

2/8/2008 11:13:00 AM

0

Hi Mario,

On 07/02/2008, Mario Ruiz <mario@betware.com> wrote:
>
> Hi Thomas, I knew it.
> What I was trying to do is to run not all the test cases in each file.
> For example only test03_example() and test01_example() from MyClass1
> and test01_example() from MyClass2


it will not help to create only specific test-objects in your script.
At the end of your script ALL test-definitions which where required are
executed - this is the way how test-unit works.

-Thomas

--=20
Thomas Preymesser
thopre@gmail.com
thomas@thopre.com
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.word...
http://www.t...

Mario Ruiz

2/8/2008 11:19:00 AM

0

So it's impossible to execute only a few test cases methods of different
classes in a new class... isn't it?
--
Posted via http://www.ruby-....

Phlip

2/8/2008 1:24:00 PM

0

Mario Ruiz wrote:

> So it's impossible to execute only a few test cases methods of different
> classes in a new class... isn't it?

The modular system I suggested will work fine.

And, again, there's generally no good reason to do this. Nobody has made
test cases easy to re-use because everyone who practices Developer
Testing generally follows the Test Isolation principle. After each test
case, the test suite should erase all objects, so each test case can
start fresh. Otherwise, to diagnose one test case failure, you'd often
have to look at the test cases that ran before it, and this would lose
many of the benefits of testing.

Now what are you actually trying to accomplish?

--
Phlip

Mario Ruiz

2/12/2008 1:47:00 PM

0

So it's impossible to execute only a few test cases methods of different
classes in a new class... isn't it?

--
Posted via http://www.ruby-....