How can I run a single test from a Rails test suite?
rake test ANYTHING
seems to not help.
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
0 tests, 0 assertions, 0 failures, 0 errors
. Rails 3.0.7.
-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.
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.
Commented
May 11, 2012 at 7:13
-n "/good/"
worked.
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.)
rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n '/paid accounts/'"
(enclosing /paid accounts/
with single quotes)
Commented
Aug 22, 2016 at 9:23
-n
and the '/abcd/'
part of the TESTOPTS
variable.
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
.
.
.
For rails 5:
rails test test/models/my_model.rb
bin/rails test test/models/user_test.rb:27
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
_
. 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
rake test TEST=test/models/MyClassTest.rb
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/'"
=
(the one in -n=
) in the past, but now it keeps thinking it's a filename instead.
To run a single test in the actual Rails suite:
bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb
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"
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
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
If rake is running MiniTest, the option is --name
instead of -n
.
rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"
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
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.
require File.join(File.dirname(__FILE__), '..', 'test_helper')
Commented
Oct 1, 2009 at 22:31
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.
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
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)