6

Yes I know the same question has been asked (How to run a single test from a rails test suite?), but the solution doesn't seem to work for Rails 3. Maybe it works for Rails 2?

How to run a single test in Rails 3.0.7 using Unit::Test? Not a single test file, but a single test.

test "the truth" do
  assert false
end

ruby -I test test/functional/test_file.rb -n "the truth" generates 0 tests, 0 assertions, 0 failures, 0 errors

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
B Seven
  • 44,484
  • 66
  • 240
  • 385

4 Answers4

6

Try Regular Expressions

bundle exec ruby -I test test/functional/test_file.rb -n "/truth/"
Vikrant Chaudhary
  • 11,089
  • 10
  • 53
  • 68
3

The "ruby -I (...) -n" method only works for me in Rails 3 like this:

bundle exec ruby -I test test/functional/test_file.rb -n "the truth"

But then again, I'm using jRuby so YMMV

ffoeg
  • 2,336
  • 1
  • 14
  • 13
  • 1
    What is `test`? I get `0 tests, 0 assertions, 0 failures, 0 errors `. – B Seven Dec 10 '11 at 17:51
  • 1
    I presume you are referring to 'test' in "-I test"? -I test adds RAILS_ROOT/test directory to the ruby load path. – ffoeg Dec 10 '11 at 19:00
  • try bundle exec ruby -I test test/functional/test_file.rb -n the_truth – ffoeg Dec 10 '11 at 19:00
  • Sorry, yes I meant `-I test`. I gather it is the text `test`. – B Seven Dec 10 '11 at 19:26
  • I did `bundle exec ruby -I test test/functional/test_file.rb -n the_truth`, and got `0 tests, 0 assertions, 0 failures, 0 errors `. – B Seven Dec 10 '11 at 19:28
  • That's very helpful. I can see that the test is called "test_the_truth". I tried that as well as another test in another file. Putting a `puts` statement inside the test shows that it is not being run...apparently it isn't being found...I wonder what could cause that... – B Seven Dec 11 '11 at 02:36
  • @BSeven I am also getting same result "Finished in 0.010000s, 0.0000 runs/s, 0.0000 assertions/s. 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips" can you tell me what to do to get successful result. – Akanksha Rathore Jan 31 '15 at 11:45
2

For some odd reason, you are supposed to put underscores instead of spaces in your testline and then use regular expression. So this works for me:

bundle exec ruby -I test test/functional/test_file.rb -n "/the_truth/"
Steve Quezadas
  • 724
  • 2
  • 11
  • 27
2

Try single_test

Marek Příhoda
  • 11,108
  • 3
  • 39
  • 53
  • I can't get it to work. The documentation is cryptic and I get `can't modify frozen string`. – B Seven Dec 10 '11 at 16:06
  • A nice tutorial covering how to use single_test (besides other options) is [here](http://flavio.castelli.name/rails_execute_single_test). It also covers how to run a single test – Marek Příhoda Dec 10 '11 at 16:17
  • Yes, I read that. It doesn't work. The two methods he suggests are `ruby -I` and single_test gem. – B Seven Dec 10 '11 at 16:22