Reputation: 6946
How can I run a single test from a Rails test suite?
rake test ANYTHING
seems to not help.
Upvotes: 173
Views: 125094
Reputation: 6217
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
Upvotes: 200
Reputation: 56650
If rake is running MiniTest, the option is --name
instead of -n
.
rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"
Upvotes: 3
Reputation: 10093
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
Upvotes: 22
Reputation: 136
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/'"
Upvotes: 11
Reputation: 3366
To re-run a test that just failed, copy-n-paste the failed test name into
rails test -n [test-name]
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
Upvotes: 3
Reputation: 774
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)
Upvotes: 0
Reputation: 7347
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
.
.
.
Upvotes: 56
Reputation: 7190
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
Upvotes: 4
Reputation: 7190
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
Upvotes: 0
Reputation: 17095
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.)
Upvotes: 139
Reputation: 1725
To run a single test in the actual Rails suite:
bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb
Upvotes: 5
Reputation: 6946
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"
Upvotes: 4
Reputation: 6055
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.
Upvotes: 2