[ANN] Copland to Needle article on RubyGarden

T

tony summerfelt

BottomFeeder is Smalltalk-based, actually.

yeah, i forgot that...
BloatWarePlus.

especially if you run win2k that doesn't have .net....
I use that too, but it could do with some work...
And besides, it's not very featureful.

the only real feature i could use with sage is keeping old items.
thunderbird's rss does that, same with any reader that actually pulls
in the feeds and not just the xml...


http://home.cogeco.ca/~tsummerfelt1
telnet://ventedspleen.dyndns.org
 
M

Mohammad Khan

Hello,

I am using Test::Unit for couple of days.

I have a script something like this:

class TestClass < Test::Unit::TestCase

def setup
@foo = Foo.new
@run = Foo.run(@foo.key)
@rerun = Foo.rerun(@run.key)
end

def test_run
assert_equal(@foo.key, @run.key)
end

def test_rerun
assert_equal(@run.key, @rerun.key)
end

end


If I run the above script without any option, would #setup be executed twice, once for #test_run and once for #test_rerun.

Thanks in advance,
Mohammad
 
H

Henrik Horneber

If I run the above script without any option, would #setup be executed twice, once for #test_run and once for #test_rerun.

Just to be nitpicky, you would get errors about Foo not being defined ... :)

I guess you meant:

require "test/unit"

class TestClass < Test::Unit::TestCase

def setup
puts "in setup"
end

def test_run
assert_equal(true, true)
end

def test_rerun
assert_equal(true, true)
end

end


outputs:
ruby test_setup.rb
Loaded suite test_setup
Started
in setup
in setup
 
M

Mohammad Khan

Just to be nitpicky, you would get errors about Foo not being defined ... :)
:)


I guess you meant:

require "test/unit"

class TestClass < Test::Unit::TestCase

def setup
puts "in setup"
end

def test_run
assert_equal(true, true)
end

def test_rerun
assert_equal(true, true)
end

end

I am confused about the '.' before the on second output 'in setup'.
what does that '.' mean?

Going back to my original example.
If I want to use @foo, @run, @rerun (already initiated) in #test_rerun,
what should I do?

Or, is there any convention something like, #setup_run (would be used by
only #test_run) or #setup_rerun (would be used by only #test_rerun)

Henrik, Thanks for you reply.
 
H

Henrik Horneber

I am confused about the '.' before the on second output 'in setup'.
what does that '.' mean?

Oh, that's just the ConsoleRunners' way of saying a test has succeeded.
Going back to my original example.
If I want to use @foo, @run, @rerun (already initiated) in #test_rerun,
what should I do?

You could put both asserts in one test_function. There is no rule saying
you can only have one assertion per test function. Don't know if this is
feasible in your environment/setting though.

For example:

class TestClass < Test::Unit::TestCase

def setup
@foo = Foo.new
@run = Foo.run(@foo.key)
@rerun = Foo.rerun(@run.key)
end

def test_foo_runs
assert_equal(@foo.key, @run.key)
assert_equal(@run.key, @rerun.key)
end
end

Or, is there any convention something like, #setup_run (would be used by
only #test_run) or #setup_rerun (would be used by only #test_rerun)

Not that I know of (which more or less doesn't mean anything). But since
only test_* methods get called from the test framework and TestClass
otherwise is a perfectly fine 'regular' class, you could write your own
setup_rerun method and call it from within your test function before
calling assert. But somehow I get the impression that this will result
in pretty bad code duplication or bloated test functions.

IANATDE
( I am not a test design expert )

:)

Henrik
 
M

Mohammad Khan

Oh, that's just the ConsoleRunners' way of saying a test has succeeded.


You could put both asserts in one test_function. There is no rule saying
you can only have one assertion per test function. Don't know if this is
feasible in your environment/setting though.

For example:

class TestClass < Test::Unit::TestCase

def setup
@foo = Foo.new
@run = Foo.run(@foo.key)
@rerun = Foo.rerun(@run.key)
end

def test_foo_runs
assert_equal(@foo.key, @run.key)
assert_equal(@run.key, @rerun.key)
end
end

Thanks again, Henrik.

I have reason to keep two assert_equal in two #test_methods.
Bottom line, I need @foo, @run and @rerun initiated once and I might
have two or more methods like #test_run, #test_rerun, #test_rererun will
use @foo, @run and @rerun.
 
H

Henrik Horneber

Don't know if this is
I have reason to keep two assert_equal in two #test_methods.
Bottom line, I need @foo, @run and @rerun initiated once and I might
have two or more methods like #test_run, #test_rerun, #test_rererun will
use @foo, @run and @rerun.


That's what I feared. Sorry, I have no idea how to accomplish that. :(

Henrik
 
B

Brian Schröder

Hello,

I am using Test::Unit for couple of days.

I have a script something like this:

class TestClass < Test::Unit::TestCase

def setup
@foo = Foo.new
@run = Foo.run(@foo.key)
@rerun = Foo.rerun(@run.key)
end

def test_run
assert_equal(@foo.key, @run.key)
end

def test_rerun
assert_equal(@run.key, @rerun.key)
end

end

If you want to setup the variables once for the test case, you should use:

class TestClass < Test::Unit::TestCase
def initialize
super
@foo = Foo.new
@run = Foo.run(@foo.key)
@rerun = Foo.rerun(@run.key)
end

def test_run
assert_equal(@foo.key, @run.key)
end

def test_rerun
assert_equal(@run.key, @rerun.key)
end
end

Would fill each variable only once.

Regards,

Brian
 
M

Mohammad Khan

Brian,

It didn't work.


[mkhan@localhost temp]$ cat a.rb
#!/usr/bin/env ruby

require 'test/unit'


class TestClass < Test::Unit::TestCase

def initialize
super
puts "in setup"
end

def test_run
assert_equal(true, true)
end

def test_rerun
assert_equal(true, true)
end

def test_rererun
assert_equal(true, true)
end

end
[mkhan@localhost temp]$ ruby a.rb
/usr/local/lib/ruby/1.8/test/unit/testcase.rb:51:in `initialize': wrong
number of arguments(1 for 0) (ArgumentError)
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:51:in `new'
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:51:in `suite'
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:50:in `catch'
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:50:in `suite'
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:48:in `each'
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:48:in `suite'
from
/usr/local/lib/ruby/1.8/test/unit/collector/objectspace.rb:25:in
`collect'
from
/usr/local/lib/ruby/1.8/test/unit/collector/objectspace.rb:23:in
`each_object'
from
/usr/local/lib/ruby/1.8/test/unit/collector/objectspace.rb:23:in
`collect'
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:52
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:48:in `[]'
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:183:in
`run'
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:13:in `run'
from /usr/local/lib/ruby/1.8/test/unit.rb:275
from /usr/local/lib/ruby/1.8/test/unit.rb:275
 
H

Henrik Horneber

require "test/unit"

class TestClass < Test::Unit::TestCase

def initialize(arg)
super(arg)
puts "in setup"
end

def test_run
assert_equal(true, true)
end

def test_rerun
assert_equal(true, true)
end

def test_rererun
assert_equal(true, true)
end

end

gives:

ruby test_setup.rb
in setup
in setup
in setup
Loaded suite test_setup
Started
...
Finished in 0.0 seconds.

3 tests, 3 assertions, 0 failures, 0 errors
Exit code: 0



close, but no cigar

Henrik
 
M

Mohammad Khan

require "test/unit"

class TestClass < Test::Unit::TestCase

def initialize(arg)
super(arg)
puts "in setup"
end

def test_run
assert_equal(true, true)
end

def test_rerun
assert_equal(true, true)
end

def test_rererun
assert_equal(true, true)
end

end

gives:


in setup
in setup
in setup
Loaded suite test_setup
Started
...
Finished in 0.0 seconds.

3 tests, 3 assertions, 0 failures, 0 errors



close, but no cigar

Henrik


In other words, I am still stuck! :-(
 
B

Brian Schröder

[]
close, but no cigar

Ok, I should test before I post ;).

You can always use this:


require 'test/unit'

class TestClass < Test::Unit::TestCase
puts "in setup"
@@foo = Array.new

def test_run
assert_equal(@@foo, [])
end

def test_rerun
assert_equal(@@foo, [])
end

def test_rererun
assert_equal(@@foo, [])
end
end


$ ruby a.rb
in setup
Loaded suite a
Started
...
Finished in 0.002371 seconds.

3 tests, 3 assertions, 0 failures, 0 errors

Regards,

Brian
 
N

Nathaniel Talbott

If you want to setup the variables once for the test case, you should
use:

class TestClass < Test::Unit::TestCase
def initialize
super
@foo = Foo.new
@run = Foo.run(@foo.key)
@rerun = Foo.rerun(@run.key)
end

def test_run
assert_equal(@foo.key, @run.key)
end

def test_rerun
assert_equal(@run.key, @rerun.key)
end
end

Would fill each variable only once.

That actually doesn't work, because the test class is instantiated once
for each run. One way to solve this is like so:

class TestClass < Test::Unit::TestCase
@@foo = Foo.new
@@run = Foo.run(@@foo.key)
@@rerun = Foo.rerun(@run.key

def test_run
assert_equal(@@foo.key, @@run.key)
end

def test_rerun
assert_equal(@run.key, @rerun.key)
end
end

Now, while I won't claim there's never a reason to do this, it seems
like a major code smell to me. Might be time to break out the air
freshener :)

HTH,


Nathaniel
Terralien, Inc.

<:((><
 
M

Mohammad Khan

That actually doesn't work, because the test class is instantiated once
for each run. One way to solve this is like so:

class TestClass < Test::Unit::TestCase
@@foo = Foo.new
@@run = Foo.run(@@foo.key)
@@rerun = Foo.rerun(@run.key

def test_run
assert_equal(@@foo.key, @@run.key)
end

def test_rerun
assert_equal(@run.key, @rerun.key)
end
end

Now, while I won't claim there's never a reason to do this, it seems
like a major code smell to me. Might be time to break out the air
freshener :)

HTH,


Nathaniel
Terralien, Inc.

<:((><

Thanks a lot, Brian and Nathaniel.
 

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
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top