This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Another recommendation for your test suite
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Make sure that when folks run your unit tests, they're running against the *local copy*. Otherwise, you're either testing against an already installed version or you're forcing your users to install *before* they test, which is not what you want.
Here's a little something I stick at the top of my test suites:
# tc_foo.rb
base = File.basename(Dir.pwd)
if base == "test" || base =~ /foo/
Dir.chdir ".." if base == "test"
$LOAD_PATH.unshift(Dir.pwd + "/lib")
Dir.chdir("test") rescue nil
end
...
require "foo"
require "test/unit"
So, assuming we're in the foo-1.0.0 directory, we can run the test suite as either "ruby test/tc_foo.rb", or cd to the "test" directory first and run "ruby tc_foo.rb" from there. In either case, it will attempt to use the version of the foo library in the current package, not one that may already be installed.
This is also why I always include a VERSION test. It's another way to make sure I'm testing against the version I'm expecting, and not an already installed version of the library.