Inheritance Question

D

David Solis

Hello,

I'm having difficulty understanding inheritance in this case. In the
program below, I'm inheriting from <Test::Unit::TestCase. I also want my
other class in this program to inherit from <Test::Unit::TestCase. My
goal is to be able to make assertions in both classes.

If a run the program as written below, it works.

require 'test/unit'
require 'test/unit/ui/console/testrunner'

class TC_MyTest < Test::Unit::TestCase
def test_myMethod
puts 'test 1'
assert(true, 'test 1')
@test = TC_MyClass.new()
@test_new = @test.test_myTestCase
end
End

class TC_MyClass
def initialize
end
def test_myTestCase
puts 'test 2'
#assert(true, 'test 2')
puts 'test 3'
puts 'test 4'
end
end

But if try to do this, I get an error:

class TC_MyTest < Test::Unit::TestCase
def test_myMethod
puts 'test 1'
assert(true, 'test 1')
@test = TC_MyClass.new()
@test_new = @test.test_myTestCase
end
End

class TC_MyClass < Test::Unit::TestCase
def initialize
end
def test_myTestCase
puts 'test 2'
assert(true, 'test 2')
puts 'test 3'
puts 'test 4'
end
end
 
S

skhisma

This error is related to an earlier post.
Test::Unit::TestCase#initialize takes one argument.

require 'test/unit'

class FooTester < Test::Unit::TestCase
def initialize(foo)
super(foo)
end
def test_one
assert(true)
end
end

see the earlier post for a much better explination by Ross. :)

- Geoff
 
R

Ross Bamford

Hello,

I'm having difficulty understanding inheritance in this case. In the
program below, I'm inheriting from <Test::Unit::TestCase. I also want my
other class in this program to inherit from <Test::Unit::TestCase. My
goal is to be able to make assertions in both classes.

If all you want to do is make assertions, you could try something like:

require 'test/unit/assertions'

class MyClass
include Test::Unit::Assertions

def something(something_else)
assert_equal 2, something_else.length
end
end

Subclassing Test::Unit::TestCase will rarely work out well since I
believe Test::Unit automatically collects testcases and runs them before
ruby quits.
 
M

Mike Harris

David said:
Hello,

I'm having difficulty understanding inheritance in this case. In the
program below, I'm inheriting from <Test::Unit::TestCase. I also want my
other class in this program to inherit from <Test::Unit::TestCase. My
goal is to be able to make assertions in both classes.

If a run the program as written below, it works.

require 'test/unit'
require 'test/unit/ui/console/testrunner'

class TC_MyTest < Test::Unit::TestCase
def test_myMethod
puts 'test 1'
assert(true, 'test 1')
@test = TC_MyClass.new()
@test_new = @test.test_myTestCase
end
End

class TC_MyClass
def initialize
end
def test_myTestCase
puts 'test 2'
#assert(true, 'test 2')
puts 'test 3'
puts 'test 4'
end
end

But if try to do this, I get an error:

class TC_MyTest < Test::Unit::TestCase
def test_myMethod
puts 'test 1'
assert(true, 'test 1')
@test = TC_MyClass.new()
@test_new = @test.test_myTestCase
end
End

class TC_MyClass < Test::Unit::TestCase
def initialize
end
def test_myTestCase
puts 'test 2'
assert(true, 'test 2')
puts 'test 3'
puts 'test 4'
end
end
i know people already said this, but I'm saying it again cause it might
get lost in the noise.

remove the initialize method
 
J

Justin Rich

I noticed that removing the initialize method allows the tests to run.
Is there something in test /unit that requires an initialize
definition to have a certain interface?

-Justin
 
D

David Solis

Thanks Everybody for your help. So I did get it to work by removing the
initialize method with the classes in one source file. When I seperated
them into their own files, I got this message:

C:/automation/src/thdf/projects/core/testharness/fixtures/admin_administration/test.rb:11:in
`test_myMethod'

1 tests, 1 assertions, 0 failures, 1 errors



I know some of you are wondering why I want to do this?

I'm writing automated scripts using Watir to drive a web app. The app
I'm testing has many repeatable steps. My goal is to break out the
repeatable patterns into it's own classes and call them as needed. I
need to be able to make assertions in the those classes. So when I
started to think about how I can do this, I came up with the program
above to see if this was possible. So far it's been touch and go.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top