168

How can I run a single test from a Rails test suite?

rake test ANYTHING seems to not help.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
artemave
  • 6,786
  • 7
  • 47
  • 71

14 Answers14

195

NOTE: This doesn't run the test via rake. So any code you have in Rakefile will NOT get executed.

To run a single test, use the following command from your rails project's main directory:

ruby -I test test/unit/my_model_test.rb -n test_name

This runs a single test named "name", defined in the MyModelTest class in the specified file. The test_name is formed by taking the test name, prepending it with the word "test", then separating the words with underscores. For example:

class MyModelTest < ActiveSupport::TestCase
  test 'valid with good attributes' do
    # do whatever you do
  end

  test 'invalid with bad attributes' do
    # do whatever you do
  end
end

You can run both tests via:

ruby -I test test/unit/my_model_test.rb

and just the second test via

ruby -I test test/unit/my_model_test.rb -n test_invalid_with_bad_attributes
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Darryl
  • 5,907
  • 1
  • 25
  • 36
  • 6
    This doesn't work for me (on functional or unit tests). I get `0 tests, 0 assertions, 0 failures, 0 errors`. Rails 3.0.7. – B Seven Dec 10 '11 at 15:32
  • 12
    I'm personally a fan of the regex form: `-n "/good/"`. Shell escaping is always fun, so I tend to stick to simple regexes, but it's far easier than writing out the full test name all the time. – Groxx Feb 20 '12 at 20:15
  • 5
    It's important to note that for this to work, you should not be in the MAIN directory as specified in the response but in the subdirectory which contain the `test/` folder. For example, if I want to run the test `activesupport/test/core_ext/array_ext_test.rb` I should be in `activesupport/` first. – Vincent B. May 11 '12 at 07:13
  • 3
    @Groxx - Finally a solution! Thank you! `-n "/good/"` worked. – B Seven Aug 17 '12 at 16:32
  • 2
    Be careful running this in 2018+. It bypasses some testing tasks built into rails (like db:test:prepare) which are used for many things, like swapping local data for fixture data, and restoring it again. Running this blew away my local database. Consider a solution that uses the built-in rails testing tasks, like https://stackoverflow.com/a/47006811/1154642 (which worked for me). – bryanbraun Sep 15 '18 at 19:57
134

Run a test file:

rake test TEST=tests/functional/accounts_test.rb

Run a single test in a test file:

rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n /paid accounts/"

(From @Puhlze 's comment.)

laffuste
  • 16,287
  • 8
  • 84
  • 91
  • 11
    Also, if you want to run a single test in in the given file you can add a TESTOPTS environment var which will be passed to the test. E.G. rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n /paid accounts/" – Puhlze May 08 '15 at 15:21
  • 2
    if the latter test command doesnt work, try `rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n '/paid accounts/'"` (enclosing `/paid accounts/` with single quotes) – rrw Aug 22 '16 at 09:23
  • Documentation: http://www.rubydoc.info/gems/rake/13.0.1/Rake/TestTask https://github.com/ruby/rake/blob/master/doc/command_line_usage.rdoc – itsnikolay Feb 16 '20 at 17:03
52

For rails 5:

rails test test/models/my_model.rb
James
  • 1,873
  • 3
  • 22
  • 28
51

Thanks to @James, the answer seems to be:

rails test test/models/my_model.rb:22

Assuming 22 is the line number of the given test. According to rails help:

 $ rails test --help

You can run a single test by appending a line number to a filename:

    bin/rails test test/models/user_test.rb:27

Also, please note that your test should inherit from ActionDispatch::IntegrationTest for this to work (That was my mistake):

class NexApiTest < ActionDispatch::IntegrationTest
.
.
.
Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39
  • 8
    This seems to be the best answer based on the Rails guides, at least for testing a specific line. – tfantina Oct 30 '17 at 10:51
20

Rails 5

I used this way to run single test file (all the tests in one file)

rails test -n /TopicsControllerTest/ -v

Another option is to use the line number (which is printed below a failing test):

rails test test/model/my_model.rb:15
Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
Alupotha
  • 9,710
  • 4
  • 47
  • 48
  • 1
    this is the best answer to the question for recent rails versions, +1 – user000001 Sep 07 '17 at 13:52
  • Note the word inside a pair of forward slashes can match any of the method names. Just be careful any space has to be replaced with `_`. For example, `-n "/many_num/"` would match `test "many numbers" do` and `test "many numbered items" do` etc. – Masa Sakano Jun 28 '21 at 15:46
  • in Rails 6, you have to use `rake test TEST=test/models/MyClassTest.rb` – Siwei Oct 10 '22 at 04:14
10

In my situation for rake only works TESTOPTS="-n='/your_test_name/'":

bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/your_test_name/'"
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
N0xFF
  • 126
  • 1
  • 5
  • 1
    I noticed this, too. I could have sworn it worked without the `=` (the one in `-n=`) in the past, but now it keeps thinking it's a filename instead. – Steve Apr 07 '21 at 23:42
  • Thank you so much for this answer, now I can finally run specific tests... – Guido Tarsia Oct 21 '21 at 21:01
5

To run a single test in the actual Rails suite:

bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb
Brian Rose
  • 1,725
  • 13
  • 10
4

That was a silly midnight question of mine. Rails kindly prints the command it is executing upon rake test. The rest is a cut and paste exercise.

~/projects/rails/actionpack (my2.3.4)$ ruby -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/controller/base_test.rb"
artemave
  • 6,786
  • 7
  • 47
  • 71
4

The best way is to look directly into the guides: http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests

cd actionmailer
bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout
Diego Plentz
  • 6,760
  • 3
  • 30
  • 31
3

To re-run a test that just failed, copy-n-paste the failed test name into

rails test -n [test-name]

EXAMPLE

When your test suite reports this:

> rails test
   ...
   Error:
   PlayersControllerTest#test_should_show_player:
   ActionView::Template::Error: no implicit conversion from nil to integer

you rerun the failing test with this:

rails test -n PlayersControllerTest#test_should_show_player
Jim U
  • 3,318
  • 1
  • 14
  • 24
2

If you want to run a single test, you can just run them as a regular Ruby script

ruby actionmailer/test/mail_layout_test.rb

You can also run a whole suite (eg. ActiveRecord or ActionMailer) by cd-ing into the directory and running rake test inside there.

Aupajo
  • 5,885
  • 6
  • 30
  • 28
  • 1
    Not in Rails -- at least not with the default test files generated. They have "require 'test_helper'" on the first line, but the load path won't have been set up in time. If you change every first line to an explicit require ("require File.join(File.dirname(__FILE__), '..', 'test_helper')"), then your solution works. – James A. Rosen Oct 01 '09 at 22:30
  • ahem, that's `require File.join(File.dirname(__FILE__), '..', 'test_helper')` – James A. Rosen Oct 01 '09 at 22:31
  • @Gaius Double-checked. First one doesn't work, but `cd`-ing in does. Just to be clear (I'm not sure if I'm mis-reading this, but you did say "generated test files"), this is for the Rails library itself, *not* a Rails project. – Aupajo Oct 10 '09 at 03:14
2

If rake is running MiniTest, the option is --name instead of -n.

rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • Nice one. This was giving me a bit of a headache. – vinibol12 Jun 19 '22 at 18:09
  • Funny, I hit that with `Test::Unit`. See https://github.com/ruby/rake/issues/490 So you say that it is not about `TestTask` but about the underlying test framework? I believe it is an issue with the `TestTask` though because if I run directly with Ruby, then it works: `bundle exec ruby test/gnuplot_printer_test.rb -n /write/`. idk if that's the same with minitest though. – akostadinov Apr 06 '23 at 13:03
0

First, access the folder of the lib you want to test(this is important) and then run:

~/Projects/rails/actionview (master)$ ruby -I test test/template/number_helper_test.rb 
Diego Plentz
  • 6,760
  • 3
  • 30
  • 31
0

Rails folder

  bundle install
  bundle exec ruby -I"activerecord/test" activerecord/test/cases/relation/where_test.rb 

Note you need to load appropriate folder: "activerecord/test" (where you have test)

Igor Kasyanchuk
  • 766
  • 6
  • 12