31

As title.

ruby test/functionals/whatevertest.rb doesn't work, that requires me to replace all require 'test_helper' to require File.dirname(__FILE__) + '/../test_helper'. For some reason most of those test templates have such issue, so I rather to see if there is a hack I could get around it.

1

7 Answers 7

49

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

But very briefly, here's 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 places of spaces in test names (as above), or quote the title as follows:

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

Note that for unit tests just replace functional with unit in the examples above. And if you're using bundler to manage your application's gem dependencies, you'll have to execute the tests with 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 to the -n switch is the name of the test, and the word "test" prepended to it, with spaces or underscores depending on whether you're quoting 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 either 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
8
  • Thanks that was helpfull. Standard directory is : test/functional/ instead of test/functionals/ so i edited your answer. Thanks
    – vdaubry
    Commented Jan 24, 2012 at 7:35
  • Oops.. thanks for the edit! I must have mistakenly slipped the s in there because sometimes I execute just the functional tests via bundle exec rake test:functionals, which is plural. Note: same for running all unit tests.. just replace functionals with units.
    – user664833
    Commented Jan 24, 2012 at 16:45
  • @BSeven What version of Ruby are you using with Rails 3.0.7?
    – user664833
    Commented Sep 5, 2012 at 17:26
  • @BSeven what specifically have you tried that doesn't work (you said "this")? I'll assume that have you tried everything above, in which case, you should try -n test_the_name_of_your_test, where test_the_name_of_your_test is "the name of your test" with test_ preceding it (and underscores in place of spaces). For example, -n test_should_create_item.
    – user664833
    Commented Sep 5, 2012 at 23:20
  • @user664833 - "This" is ruby -I test test/functional/whatevertest.rb -n selected_test and ruby -I test test/functional/whatevertest.rb -n 'selected test w long name'. Prepending test_ did not help. In all 3 cases, the output is ` 0 tests, 0 assertions, 0 failures, 0 errors`.
    – B Seven
    Commented Sep 6, 2012 at 2:32
13

Try this:

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

1
  • 4
    I actually run it with: ruby -I test test/functional/whatevertest.rb but it ends up being the same thing. Commented Jan 18, 2010 at 15:30
9

On Linux? why not try (cd test && ruby functionals/whatevertest.rb). Note, the parentheses are important as otherwise your current directory will change to the subdirectory. What it does is fork another shell, change to the subdirectory in it, and run the test.

1
  • thats a good one. I changed a bit to (cd test && ruby functional/whatsoevertest.rb) so it works properly with the relative directory structure. Makes perfect sense :) Commented Nov 8, 2008 at 7:58
5

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

rake test test/unit/user_test.rb
rake test test/unit
4

The answer for the title question would be:

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.

1
  • I got the following error: <internal:lib/rubygems/custom_require>:29:in require': no such file to load -- test_helper (LoadError)` Does it work with functional test?
    – B Seven
    Commented Oct 6, 2011 at 16:37
4

After spending endless hours on this, I finally found the solution (in Rails 3.0.7) :

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

Note, this only works with underbars (_) in the command. It 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 is "should do foo", then either of the following will work as well.

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/"
2
  • 1
    I found out that if you escape "space" in pattern It will work. So something like this works: ruby -I test test/my_test.rb -n "/do\ foo/"
    – Oto Brglez
    Commented Apr 10, 2014 at 11:27
  • for some reason leaving the spaces worked for me, I'm ruby -v ruby 1.9.3p484 and >> Rails::version => "2.3.18"
    – mswieboda
    Commented Jun 27, 2014 at 16:00
0

most conventional method for 2 and 3 is:

ruby -I test test/functional/your_test_file.rb

Not the answer you're looking for? Browse other questions tagged or ask your own question.