On Mon, May 20, 2002 at 10:04:07PM +0900, Dave Thomas wrote: > > So it could be a great opportunity for a documenting tool > > like RDoc to browse tests and feature their information > > as documentation near the documented methods. > Actually, by an amazing coincidence... :) That's something we've been > talking about doing. There's a brief hold up while I struggle with a > philosophical question. > > In (say) Java, people tend to keep the unit tests in separate files. I > think this is for two reasons. First the tests add bulk to the class > files. Second, the additional verbiage in Java means the tests obscure > the meaning of the code. > > Neither of these apply to Ruby, and yet we still tend to put the unit > tests in a separate file. So I'm wondering what happens it we break > that tradition: > > > class Multiplier > > test :numeric, "Test that numeric multiplication works" do > m = Multipler.new(3) > assert_equal(6, m.times(2)) > assert_equal(-9, m.times(-3)) > end > > test :alpha, "Test other uses of the '*' method" do > m = Multiplier.new("hi") > assert_equal("hihi", m.times(2)) > assert_equal("", m.times(0)) > end > > def initialize(by) > @by = by > end > > def times(n) > @by * n > end > > end > > When run with ruby -rtest ..., the test cases get run. When rdoc'd, > the test code gets extracted and used as class-level documentation. > > I'm still not sure about this - what do folks think? Here are my two cents. I usually put each major class in its own file together with its tests. The interpreter only sees the tests if the file to be run is the one that contains them, not if the file was required in another file. I do it this way because this allows to iteratively run and develop the class being working on. From Emacs it goes like: write test, run with C-c s, see test results, modify class, run, and so on. No loading or saving involved, speed goes up. It looks like this: class Foo def bar ... end end if $0 == __FILE__ require 'test/unit' class TestFoo < Test::Unit::TestCase def test_bar ... end end end With `ruby foo.rb' on the command line, test will be run. With `require "foo.rb"' in some other file, they won't even be defined. If a test.rb file containing just the following is available somewhere: $TESTING = true ...then the above code can be modified like this: if $TESTING require 'test/unit' class TestFoo < Test::Unit::TestCase ... end end ...and be run with `ruby -rtest foo.rb', like someone suggested. Not as easy parse-wise as an explicit directive in the class body, but it works for me as a way to keep things clean and still get development speed. Massimiliano