170

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

rake test ANYTHING seems to not help.

1

14 Answers 14

198

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
5
  • 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
    Commented Dec 10, 2011 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
    Commented Feb 20, 2012 at 20:15
  • 6
    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.
    Commented May 11, 2012 at 7:13
  • 3
    @Groxx - Finally a solution! Thank you! -n "/good/" worked.
    – B Seven
    Commented Aug 17, 2012 at 16:32
  • 3
    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 stackoverflow.com/a/47006811/1154642 (which worked for me).
    – bryanbraun
    Commented Sep 15, 2018 at 19:57
138

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.)

4
  • 12
    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
    Commented May 8, 2015 at 15:21
  • 3
    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)
    – pokeymond
    Commented Aug 22, 2016 at 9:23
  • 1
  • I have to get rid of the space between -n and the '/abcd/' part of the TESTOPTS variable.
    – jcoleman
    Commented Sep 3 at 15:48
55

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
.
.
.
1
  • 9
    This seems to be the best answer based on the Rails guides, at least for testing a specific line.
    – tfantina
    Commented Oct 30, 2017 at 10:51
52

For rails 5:

rails test test/models/my_model.rb
2
  • 11
    But this runs all test cases in the file, the question asks to run a single test...
    – user000001
    Commented Sep 7, 2017 at 13:50
  • 7
    You can run a single test by appending a line number to a filename:bin/rails test test/models/user_test.rb:27
    – Segfault
    Commented Mar 25, 2020 at 18:16
22

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
3
  • 1
    this is the best answer to the question for recent rails versions, +1
    – user000001
    Commented Sep 7, 2017 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. Commented Jun 28, 2021 at 15:46
  • in Rails 6, you have to use rake test TEST=test/models/MyClassTest.rb
    – Siwei
    Commented Oct 10, 2022 at 4:14
11

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/'"
2
  • 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
    Commented Apr 7, 2021 at 23:42
  • Thank you so much for this answer, now I can finally run specific tests... Commented Oct 21, 2021 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
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"
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
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
3

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

rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"
2
  • Nice one. This was giving me a bit of a headache.
    – vinibol12
    Commented Jun 19, 2022 at 18:09
  • Funny, I hit that with Test::Unit. See 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. Commented Apr 6, 2023 at 13:03
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.

3
  • 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. Commented Oct 1, 2009 at 22:30
  • ahem, that's require File.join(File.dirname(__FILE__), '..', 'test_helper') Commented Oct 1, 2009 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
    Commented Oct 10, 2009 at 3:14
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 
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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