Single Rail Launch / Function Test

Like a headline.

ruby test / functionsals / whatevertest.rb does not work, so I need to replace all require 'test_helper' with require File.dirname(__FILE__) + '/../test_helper' . For some reason, most of these test patterns have this kind of problem, so I prefer to see if there is a hack that I could work around.

+29
ruby-on-rails unit-testing automated-tests
Nov 08 '08 at 4:14
source share
7 answers

In Linux? why not try (cd test && ruby functionals/whatevertest.rb) . Please note: parentheses are important, because otherwise your current directory will be changed to a subdirectory. What he does is fork another shell, change the subdirectory in it, and run the test.

+9
Nov 08 '08 at 7:50
source share

The following answer is based on: How to run a single test from a rails test suite? (stackoverflow)

But very briefly, here is the answer:

 ruby -I test test/functional/whatevertest.rb 

For a specific functional test, run:

 ruby -I test test/functional/whatevertest.rb -n test_should_get_index 

Just put underscores in the spaces in the test names (as indicated above) or specify a title as follows:

 ruby -I test test/functional/whatevertest.rb -n 'test should get index' 

Note that for unit tests, simply replace functional with unit in the examples above. And if you use bundler to manage your gem dependencies of your application, you will need to run tests using bundle exec as follows:

 bundle exec ruby -I test test/unit/specific_model_test.rb bundle exec ruby -I test test/unit/specific_model_test.rb -n test_divide_by_zero bundle exec ruby -I test test/unit/specific_model_test.rb -n 'test divide by zero' 

Most importantly , note that the argument for the -n switch is the name of the test, and the word "test" is added to it with spaces or underscores, depending on whether you quote the name or not. The reason is that test is a convenience method. The following two methods are equivalent:

 test "should get high" do assert true end def test_should_get_high assert true end 

... and can be executed as one of the following (they are equivalent):

 bundle exec ruby -I test test/integration/misc_test.rb -n 'test should get high' bundle exec ruby -I test test/integration/misc_test.rb -n test_should_get_high 
+46
Nov 02 '11 at 19:35
source share

Try the following:

ruby -Ilib: test test / functionsals / whatevertest.rb

+13
Feb 13 '09 at 21:43
source share

If you are on Rails 4, then rake supports file / directory arguments. Example:

 rake test test/unit/user_test.rb rake test test/unit 
+5
Feb 06 '14 at 9:20
source share

The answer to the title question will be as follows:

 ruby unit/post_test.rb -n selected_test # use to run only one selected test 

but for the body of the question tvanfosson gave a good answer.

+4
Aug 26 '10 at 17:25
source share

After spending endless hours, I finally found a solution (in Rails 3.0.7):

 ruby -I test test/functional/users_controller_test.rb -n "/the_test_name/" 

Note. This only works with coasters (_) in the command. This does not work with spaces!

Define the test with spaces as:

 test "the test name" do 

This solution uses pattern matching, so you can use part of the test name. If the test "should do foo", then one of the following actions will also be performed.

 ruby -I test test/functional/alerts_controller_test.rb -n "/foo/" ruby -I test test/functional/alerts_controller_test.rb -n "/do_foo/" 

The following (with spaces) will not work :

 ruby -I test test/functional/alerts_controller_test.rb -n "/do foo/" 
+4
Sep 05 '12 at 13:45
source share

The most common method for 2 and 3:

ruby -I test test / functional / your_test_file.rb

0
Apr 27 '12 at 7:35
source share



All Articles