From: Massimiliano Mirra Date: 2001-10-22T06:36:33+09:00 Subject: [ruby-talk:22902] First steps in unit testing I'm using rubyunit and it is my first experience with unit testing in general. I'd like to hear some opinions on a simple style matter. Let's say this class is being tested: class Point def initialize(x=0, y=0) @x,@y=x,y self end def move(by_x, by_y) @x += by_x @y += by_y self end attr_reader :x,:y end I've seen three styles of writing the class that does the testing: 1) class TestPoint < RUNIT::TestCase def setup @p = Point.new end def setupMoved @p.move(10,6) end attach_setup :setupMoved, :testMoved def testMoved assert_equal( @p.x, 10 ) assert_equal( @p.y, 6 ) end end 2) class TestPoint < RUNIT::TestCase def testMoved point = movedPoint assert_equal( @p.x, 10 ) assert_equal( @p.y, 6 ) end def movedPoint Point.new.move(10, 6) end end 3) class TestPoint < RUNIT::TestCase def testMoved point = Point.new.move(10,6) assert_equal( @p.x, 10 ) assert_equal( @p.y, 6 ) end end In other words, 1) uses specific setups, 2) too but with factory methods, and 3) does it all in the testing method. Aside from the readability in example 3), that however could go down as the test goes up in setup requirements, are there significant advantages to one way over the others? Massimiliano