Peter Marklund

Peter Marklund's Home


Rails Tip: Running Tests with Verbose Output

If you want to run your Rails tests with verbose output you can use the TESTOPTS argument like this:

rake test TESTOPTS="-v"

Now instead of just getting dots as progress indicators you get a list of test methods (blocks) and test cases like this:

test_create_with_errors(NewsletterTest): .
test_destroy(NewsletterTest): .
test_send_all(NewsletterTest): F
test_send_one(NewsletterTest): F
test_update(NewsletterTest): .
test_is_active?(PlanTest): .

It took me a while to track down the TESTOPTS argument. Let's look at the chain of execution when you invoke rake in a Rails app. First, rake will load the Rakefile in the rails root which in turn will boot up your Rails app, require rake itself, the rake test task, as well as all the Rails rake tasks. The default Rails rake task is test and it is defined in the testing.rake file in railties/lib/tasks. The test Rake task will invoke the test:units test:functionals test:integration Rake tasks in sequence. Those are defined by invoking The Rake::TestTask class is defined in the testtask.rb file in your rake gem installation (use gem which rake to find it). It is in this file that the TESTOPTS argument is documented. It turns out that will execute something like this on the command line (I broke the line up for readability):

..all other test files here...

Notice the -v (for verbose) option as the last argument. So where does Test::Unit enter the picture? Well, each of your test files requires test/test_helper.rb which in turn requires test_help.rb in your Rails installation which does a require 'test/unit'. You can check out the Test::Unit sources by opening up $RUBY_LIB/1.8/test in your editor. It turns out that it's autorunner.rb in test/unit that parses out the "-v" command line options and automagically runs your tests. It's as simple as that...

4 comment(s)


Jack said 2010-06-23 15:13:

I am glad I have found this article, thanks!

Chad Bearden said 2010-02-24 22:11:

Excellent! I use this all the time, now.

Vikram said 2009-12-23 13:13:

Thanks! this was exactly looking for!

Allen said 2009-05-01 18:39:

Thanks, I have been looking for this