views:

607

answers:

4

When I want to run all my unit tests, I run rake test:units. To run all my functional tests, I run rake test:functionals. If I want to run all the test cases in one file, I run

ruby test/unit/username_test.rb

A few people have been telling me I should run rake instead such as

rake test:units TEST=test/unit/username_test.rb

For running tests, they say I should always run rake. I know I should run rake if I'm testing all my unit tests. But what if it's just one file or one particular test method in a file that I'm testing? Should I still use rake? Is there any difference between the two? Do I get any benefit from running rake over ruby? Is there any disadvantage to running ruby rather than rake?

+1  A: 

No I dont think so. Rake seems to be a convenient way to run all tests, all unit tests or all functional/controller tests. For a single file, I use the ruby object_test.rb approach.. shorter and works fine for my rails home project.

Gishu
+3  A: 

Sadly, the two are not the same. Running the tests under rake can wind up pulling things from different places than when you run the test directly (more a problem when you have multiple versions of gems, etc. on your system).

The intent is that tests run under rake should be in an environment that matches what rails would produce; I can not attest to how closely they match, but I have seen that that passed when run directly but failed when run via rake or rails (and visa versa).

MarkusQ
A: 

Before checking in at the very least I'd recommend running rake to hit everything, in order to be assured that nothing unexpected has broken.

Plain ruby seems ideal for fast testing of single files during iterations.

Be aware that running everything through rake can produce different results to running everything individually, as I found to my confusion recently - I was doing something slightly wrong in one test that worked successfully in isolation but that left a problem lying around for a subsequent test that only showed up when I used rake.

Mike Woodhouse
A: 

Running rake will ensure that the rails environment is set up correctly. Running a file independently can (depending on the complexity of your app) create problems.