Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 764
Max Avatar asked Mar 26 '09 16:03

Max


People also ask

How do I run a Ruby test?

If your tests don't require any specific actions before start and you don't want to configure additional options, such as code coverage, you can run them by using the following options: Place the caret at the test class to run all tests in that class, or at the test method, and press Ctrl+Shift+F10 .

Do Rails tests run in parallel?

Rails now can run tests in parallel with minitest. If you're using another test framework, various gems enable this as well.

How do you run a Minitest in Rails?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

How do I run an RSpec test?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.


3 Answers

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

like image 90
MarkusQ Avatar answered Sep 30 '22 12:09

MarkusQ


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.

like image 41
Mike Woodhouse Avatar answered Sep 30 '22 12:09

Mike Woodhouse


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.

like image 33
Gishu Avatar answered Sep 30 '22 13:09

Gishu