Max
Max

Reputation: 287

Ruby on Rails: Running Tests

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?

Upvotes: 8

Views: 6479

Answers (5)

MarkusQ
MarkusQ

Reputation: 21950

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 tests that passed when run directly but failed when run via rake or rails (and visa versa).

Upvotes: 7

Marlin Pierce
Marlin Pierce

Reputation: 10099

The two are not the same. Rake will do some preliminary test loading.

The intent is that tests run under rake should be in an environment that matches what rails would produce;

One difference I've noticed is with rake some of the fixture loading happens which could be by-passed with ruby.

I'd recommend using rake, unless you are using the ruby command line to one just one test in the file with the -n option.

Upvotes: 0

Steven Soroka
Steven Soroka

Reputation: 19902

They should be identical. if they are not, you're definitely doing something wrong.

As I said in my other comments, if you get tests that pass in one, but fail in the other, you're doing something very wrong; this indicates a poor test setup, and is usually caused by a different test run order between the two test approaches; one of which causes tests to fail.

Usually the cause of this is that you are not using transactions and/or tests are not cleaning up after themselves. For example, not properly requiring the fixtures they later test for, and instead relying on the pre-existing database state.

you are free to use either method. if something breaks, you're doing something wrong in your tests, and you should fix your code.

Upvotes: 0

Mike Woodhouse
Mike Woodhouse

Reputation: 52326

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.

Upvotes: 3

Gishu
Gishu

Reputation: 136663

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.

Upvotes: 2

Related Questions