Rob Sanheim
12/6/2006 4:52:00 AM
Hi,
<snip>
> Your answers here solved a problem I posted yesterday about another
> messed up test. I have now deleted that post because "I've seen the
> light."
>
> Below is a toy testing setup. I stuck with my numbered approach for
> the moment because it fit these tests. This setup worked quite nicely.
> If you're in the mood to comment on them, I'd be a grateful recipient.
> But I think I'm OK on this topic now. Incidentally, I thought about
> trying to use lamda expressions to reduce the duplication, but enough
> is enough.
Why do you want to number your tests? Your tests should be written in
such a way that they don't depend upon the order they run, so
numbering them to try and get a specific order is a bad idea. If its
just to get a nice output when you run the suite, you should look at
tweaking the test runner or autotest or whatever you use to run your
tests.
> Again, thank you for providing such thoughtful comments.
>
> Best wishes,
> Richard
>
> SetupTeardown.rb
> =============
> class Foo
> MyConst = "Foo's Constant"
> def initialize(n)
> @n = n
> end
> def bar
> "I'm Foo#bar with #{MyConst}#{@n}"
> end
> end
>
> puts Foo.new(1).bar if __FILE__ == $0
>
> TestSetupTeardown.rb
> ================
> # "TestSetupTeardown"
>
> require 'test/unit'
> require 'tc_Test1'
>
> tc_Test.rb
> ========
> require './SetupTeardown'
> require 'test/unit'
> class TestST < Test::Unit::TestCase
> def setup
> @instance1 = Foo.new(1)
> @instance2 = Foo.new(2)
> end
> def teardown
> @instance1 = nil
> @instance2 = nil
You don't need to set these to nil, as a new instance of the test
class will be create for each test case. You normally only see
teardown used for things like closing a database connection, making
sure a stubbed method gets removed, etc.
> end
> def test1
> assert_equal("I'm Foo#bar with Foo's Constant1",
> @instance1.bar)
> assert_equal("I'm Foo#bar with Foo's Constant2",
> @instance2.bar)
> end
> end
- Rob