Testing in Rails: Part 11 - Running Unit Tests
This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with an introduction and overview of the ideas behind testing.
One final post about unit tests and then we will move on to functional testing.
Running All Tests in a Single Test File
Up to now we have run our unit tests by simply asking Ruby to process the file that contains our tests.
1 | >ruby test/unit/wine_test.rb |
When you are first developing unit tests on a single model, this may be the easiest way to do it. It will run all the tests in the WineTest file.
Running a Single Test
What if you want to focus on one test in particular? What if your unit test file is large and takes a long time to run. Running the all the tests each time can be a waste of time.
What if you have a few tests in the file that are not finished yet? Those failures can distract and even confuse you while you work on a single test.
In these situations it would be better to run one test at a time. Ruby will do that if you include the “-n” flag (as in “name of the method”) followed by the name of the test method you want to run.
1 | >ruby test/unit/wine_test.rb -n test_relationships |
Only the test_relationships method will be executed. Even though we specified a single method, Ruby still loads in fixtures and preforms the setup method before running the test. It just ignores the other tests in the file as if they did not exist.
Running All Unit Tests
One of the benefits of writing tests is that you can test your application often and easily to ensure that everything still works. Once you have built unit tests for every model in your Rails application, it would not be considered “easy” if you had to run each and every test one by one from the command line. Can you imagine having an application with 20 or 30 models and having to test each one every time you ran the test suite? I can guarantee that it would keep you from doing it “often”.
This is where Rake is helpful. Rake allows us to make Ruby tasks easier to perform. There are a few Rake commands that you should learn. If you ever forget them, you can type rake -T from the command line while in the root of your Rails application to see a list of all available Rake commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Run all tests >rake test # Same as the above # ('rake test' is the default action for rake) >rake # Run all unit tests >rake test:units # Run all functional tests >rake test:functionals # Run all integration tests >rake test:integration |
Obviously, until you have written functional or integration tests, you will want to stick with rake test:units. I am simply listing all four now so that you can have them at your disposal throughout the rest of this tutorial.
The good news is: whenever you feel like you need a “sanity check” and want to run all of your tests at once, it is only a single command away! And a combination of these testing commands will provide you with some fine-grained control over which tests you run.

February 28th, 2008 at 9:03 am
[...] Testing in Rails: Part 11 - Running Unit Tests - More testing for fun and profit. [...]
March 2nd, 2008 at 11:49 am
You’ll probably mention this later in the series, but the best way I’ve found to run your tests for Rails is using the ZenTest autotest, having your tests automatically run whenever you modify your code or the test is just the best way to keep on top of testing. I have it running any time I’m changing anything in the codebase.
March 3rd, 2008 at 7:00 am
This is really helpful - keep up the good work.
Unfortunately it means I have a lot more work to do to retrofit sufficient testing to my 3/4 finished application.
March 13th, 2008 at 9:29 am
I really liked your series of introduction to testing Ruby/Rails! Lots of good tips here. Keep up all the good work!
I’m with Robin2 on this one; I’ll have to put in some more work into testing. Guess I’m just too grown into the habit of loading up the browser everytime, but with these articles, I’m all ready for testing.
March 25th, 2008 at 4:21 am
another way of testing one test / one testcase
rake test:blog -> only the Blog Testcase
rake spec:blog -> only the Blog Spec
rake test:blog:create -> only the tests matching /create/ in Blog
rake spec:blog:delete -> only the first example matching /create/ in Blog
rake test:blog_C -> only the BlogController Test
http://pragmatig.wordpress.com/2008/03/19/testing-a-single-example-spec-testcase-test/
May 28th, 2008 at 7:30 am
Thanks very much for this series. Like Robin2 above I’m now in the process of writing full unit tests for my first full Rails app. Looking forward to you words on functional and integration tests!
March 25th, 2009 at 2:39 pm
I wrote some simple rake tasks to accomplish some unit test enhancements - they might be of interest to you:
http://www.misuse.org/science/2009/03/25/continuous-testing-and-testing-single-methods-in-ruby-on-rails/
May 28th, 2009 at 3:05 am
Hi Kevin
Many thanks to writing this most useful series..Expecting a series on functional tests
Thanks again
sk