Running Tests with Rake

Problem

You've been diligent about creating tests for your application and would like a convenient way to run these tests in batches. You want to be able to run all your unit or functional tests with a single command.

Solution

Rails organizes tests into directories named after the type of application code that they test (e.g., functional tests, unit tests, integration tests, etc.). To run all tests in these groups at once, Rake provides a number of testing related tasks:

Test all unit tests and functional tests (and integration tests, if they exist):

$ rake test

Run all functional tests:

$ rake test:functionals

Run all unit tests:

$ rake test:units

Run all integration tests:

$ rake test:integration

Run all test in ./vendor/plugins/**/test (or specify a plug-in to test with PLUGIN= name ):

$ rake test:plugins

Run tests for models and controllers that have been modified in the last 10 minutes:

$ rake test:recent

For projects in Subversion, run tests for models and controllers changes since last commit:

$ rake test:uncommitted

Discussion

Writing tests pays off only if you run them often during development or when environment conditions have changed. If running tests was a tedious process, the chances of them being run regularly would probably lessen. Rake's testing tasks are designed to encourage you to run your tests not only often, but efficiently.

Other than rake test, the testing tasks are designed to run a subset of your application's test code. The idea is that you may not want to run your entire test suite if you've only touched a small portion of your code that's sufficiently decoupled. If you have a lot of tests, running only a portion of them can save you development time. Of course, you should run your whole test suite periodically (at least before every check-in) to make sure bugs don't exist when your entire application interacts.

See Also