Ruby on Rails: running tests

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

ruby test / unit / username_test.rb

Several people told me that I should use rake, for example

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

To run the tests, they say that I should always use rake. I know that I should use rake if I test all my unit tests. But what if it is only one file or one specific testing method in the file that I am testing? Should I use a rake? Is there a difference between the two? Do I get any benefit from running a ruby โ€‹โ€‹rake? Is there a drawback in working with a ruby, and not with a rake?

+7
ruby-on-rails unit-testing testing
source share
5 answers

Unfortunately, these two are not the same. Running tests under Rake can lead to extracting things from different places than when running the test directly (more problems when your system has several versions of gems, etc.).

The intent is that tests run under Rake should be in an environment that matches what rails will produce; I canโ€™t testify how close they match, but I saw tests that took place on a direct start, but failed on passing through a rake or rails (and vice versa).

+6
source share

Before starting the test, I would recommend running rake to hit everything, to be sure that nothing unexpected has broken.

A regular ruby โ€‹โ€‹seems ideal for quickly testing individual files during iterations.

Keep in mind that running everything through rake can lead to different results for starting everything individually, as I found that lately I got a little mixed up - in one test that worked successfully in isolation, I did something a little wrong, but this left a problem lying around the subsequent test, which appeared only when I used rake.

+3
source share

No, I do not think so. Rakes seem to be a convenient way to run all tests, all unit tests, or all functional / control tests. For a single file, I use the ruby object_test.rb approach .. shorter and works fine for my main rails project.

+2
source share

They must be the same. if they are not, you are 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 are 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 makes tests fail.

Usually the reason for this is that you are not using transactions and / or tests are not cleared after you. For example, not properly requiring the devices they test later, and instead rely on the existing state of the database.

You can use any of these methods. if something breaks, you are doing something wrong in your tests, and you have to fix your code.

0
source share

These two options do not match. The rake will perform a preliminary test load.

The goal is that the tests performed under the rake must be in an environment that is consistent with what the rails will create;

One of the differences I noticed is the rake, which occurs due to the loading of the device, which could be circumvented with ruby.

I would recommend using rake if you are not using the ruby โ€‹โ€‹command line for only one test in a file with the -n option.

0
source share

All Articles