Peter Marklund

Peter Marklund's Home

Fri April 17, 2009
Programming

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 Rake::TestTask.new. 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 Rake::TestTask.new will execute something like this on the command line (I broke the line up for readability):

ruby
-Ilib:test
"/Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb"
"test/functional/application_controller_test.rb"
..all other test files here...
-v

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...

Comments

Allen said over 5 years ago:

Thanks, I have been looking for this

--------------------------------------------------------------------------------

Vikram said over 4 years ago:

Thanks! this was exactly looking for!

--------------------------------------------------------------------------------

Chad Bearden said over 4 years ago:

Excellent! I use this all the time, now.

--------------------------------------------------------------------------------

Jack said over 4 years ago:

I am glad I have found this article, thanks!

--------------------------------------------------------------------------------