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.

William Yeung
  • 10,368
  • 9
  • 36
  • 42
  • Possible duplicate of [How to run a single test from a rails test suite?](https://stackoverflow.com/questions/1506780/how-to-run-a-single-test-from-a-rails-test-suite) – krubo Nov 18 '18 at 16:52

7 Answers7

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
Community
  • 1
  • 1
user664833
  • 18,397
  • 19
  • 91
  • 140
  • Thanks that was helpfull. Standard directory is : test/functional/ instead of test/functionals/ so i edited your answer. Thanks – vdaubry Jan 24 '12 at 07: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 Jan 24 '12 at 16:45
  • @BSeven What version of Ruby are you using with Rails 3.0.7? – user664833 Sep 05 '12 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 Sep 05 '12 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 Sep 06 '12 at 02:32
  • I just installed `rails 3.0.7`, and built a new "depot" app (`rails new depot`), `cd depot`, `bundle install` (to install sqlite3), `rails g scaffold User name:string`, `bundle exec rake db:migrate`, `RAILS_ENV=test bundle exec rake db:migrate`, and then I ran the following tests with `ruby 1.9.2p290`, and all of them worked: `ruby -I test test/unit/user_test.rb` and `ruby -I test test/unit/user_test.rb -n test_the_truth` and `ruby -I test test/unit/user_test.rb -n 'test_the_truth'`. The test's actual name is "the truth". Also, I am in *the app's root directory*. – user664833 Sep 06 '12 at 06:59
  • @user664833 - OK, I went through that process and all 3 commands worked. I guess there is something with my gems or monkey patching in my other project that is breaking this functionality. It worked for a functional test as well. The important thing to know is that the test name must be preceded by `test`: `test_the_truth`. – B Seven Sep 15 '12 at 19:48
  • @BSeven I have updated the answer to reflect this. Sorry that it was misleading. – user664833 Oct 31 '12 at 21:59
13

Try this:

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

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.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 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 :) – William Yeung Nov 08 '08 at 07: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
Swanand
  • 12,317
  • 7
  • 45
  • 62
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/"
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • 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 Apr 10 '14 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 Jun 27 '14 at 16:00
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.

Community
  • 1
  • 1
Szymon Jeż
  • 8,273
  • 4
  • 42
  • 60
  • I got the following error: `:29:in `require': no such file to load -- test_helper (LoadError)` Does it work with functional test? – B Seven Oct 06 '11 at 16:37
0

most conventional method for 2 and 3 is:

ruby -I test test/functional/your_test_file.rb

alexey_the_cat
  • 1,812
  • 19
  • 33