How to run a single test from a Rails test suite?

Ruby on-RailsUnit TestingRake

Ruby on-Rails Problem Overview


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

rake test ANYTHING seems to not help.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

For rails 5:

rails test test/models/my_model.rb

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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/'"

Solution 7 - Ruby on-Rails

To run a single test in the actual Rails suite:

bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb

Solution 8 - Ruby on-Rails

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"

Solution 9 - Ruby on-Rails

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

Solution 10 - Ruby on-Rails

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.

Solution 11 - Ruby on-Rails

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

Solution 12 - Ruby on-Rails

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

rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"

Solution 13 - Ruby on-Rails

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 

Solution 14 - Ruby on-Rails

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionartemaveView Question on Stackoverflow
Solution 1 - Ruby on-RailsDarrylView Answer on Stackoverflow
Solution 2 - Ruby on-RailslaffusteView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJamesView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRoozbeh ZabihollahiView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAlupothaView Answer on Stackoverflow
Solution 6 - Ruby on-RailsN0xFFView Answer on Stackoverflow
Solution 7 - Ruby on-RailsBrian RoseView Answer on Stackoverflow
Solution 8 - Ruby on-RailsartemaveView Answer on Stackoverflow
Solution 9 - Ruby on-RailsDiego PlentzView Answer on Stackoverflow
Solution 10 - Ruby on-RailsAupajoView Answer on Stackoverflow
Solution 11 - Ruby on-RailsJim UView Answer on Stackoverflow
Solution 12 - Ruby on-RailsDon KirkbyView Answer on Stackoverflow
Solution 13 - Ruby on-RailsDiego PlentzView Answer on Stackoverflow
Solution 14 - Ruby on-RailsIgor KasyanchukView Answer on Stackoverflow