8

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?

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

this indicates a poor test setup, and is usually caused by one method running tests in a different order than the other; one of which causes tests to fail. Likely because you are not using transactions and tests are not cleaning up after themselves, or not properly requiring the fixtures they use.
3

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.

Comments

2

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.