Module and Test::Unit

Hi,

Is there any way we can use test assertations in a module, or do we
always need to create a test class and inherit test unit?

I am using assertations for GUI tests and have modules with re-usable
methods.

thank you

aidy

aidy wrote:

Hi,

Is there any way we can use test assertations in a module, or do we
always need to create a test class and inherit test unit?

I am using assertations for GUI tests and have modules with re-usable
methods.

I'm not sure if this is what you're asking, but I do this sort of thing quite a lot:

module ServiceTests
   def test_foo
     assert @thing.foo
   end
   def test_bar
     assert @thing.bar
   end
end

class RawTests < Test::Unit::TestCase
   include ServiceTests
   def setup
     @thing = Thing.new
   end
end

class XMLRPCTest < Test::Unit::TestCase
   include ServiceTests
   def setup
     client = XMLRPC::Client.new('localhost', '/', $port)
     @thing = client.proxy('thing')
   end
end

That ensures that return values and the like are correct across both native and RPC calls, without overly duplicating code. Is that what you were after?

···

--
Alex

You can mix in the assertion module even if you are not using
Test::Unit::TestCase. Thus:

module mine
  include Test::Unit::Assertions
  def my_func
    do_something
    assert_equal 2, x
  end
end

The only thing "wrong" with this is that your assertions won't be
counted if this module is actually, later, used by a test case. To fix
that problem, see http://www.io.com/~wazmo/blog/archives/2006_05.html

Bret

bpettichord@gmail.com wrote:

http://www.io.com/~wazmo/blog/archives/2006_05.html

Very useful blog. I'll link it from mine.

cheers

aidy