How to run one test from a set of rails tests?

How to run one test from rails test suite?

rake test ANYTHING doesn't seem to help.

+101
ruby-on-rails unit-testing rake
01 Oct '09 at 22:09
source share
11 answers

NOTE. This does not run the test through rake . This way, any code you have in the Rakefile will NOT be executed.

To run a single test, use the following command in the rails project 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 name test_ is formed by entering a test name, adding it to the word "test", and 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 with:

 ruby -I test test/unit/my_model_test.rb 

and only the second test through

 ruby -I test test/unit/my_model_test.rb -n test_invalid_with_bad_attributes 
+132
01 Oct '09 at 23:44
source share

Run the test file :

 rake test TEST=tests/functional/accounts_test.rb 

Run a single test in the test file:

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

(From @Puhlze's comment.)

+66
Apr 13 '14 at 17:13
source share

For rails 5:

 rails test test/models/my_model.rb 
+26
Jun 28 '16 at 2:38 on
source share

Thanks @James, the answer seems to be:

 rails test test/models/my_model.rb:22 

Assuming 22 is the line number of this test. As recommended by rails:

  $ rails test --help 

You can run one test by adding a line number to the file name:

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

Also note that your test must inherit from ActionDispatch :: IntegrationTest for this to work (this was my mistake):

 class NexApiTest < ActionDispatch::IntegrationTest . . . 
+19
Oct 30 '17 at 0:53
source share

In rails 5,

I used this method to run one test file (all tests in one file)

 rails test -n /TopicsControllerTest/ -v 

look here https://stackoverflow.com/a/2129603

+8
Dec 16 '16 at 11:55
source share

To run a single test in a real Rails set:

 bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb 
+5
Dec 20 '11 at 20:15
source share

It was a stupid midnight question. Rails kindly prints a command executed using rake test . The rest is an exercise for cutting and pasting.

 ~/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" 
+3
03 Oct '09 at 15:19
source share

If you want to run one test, you can just run them like a regular Ruby script

 ruby actionmailer/test/mail_layout_test.rb 

You can also run the whole set (e.g. ActiveRecord or ActionMailer) with cd -ing in the directory and run rake test inside.

+1
01 Oct '09 at 22:20
source share

The best way is to look directly at 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 
+1
Nov 27 '16 at 21:26
source share

First go to the library folder that you want to test (this is important), and then run:

 ~/Projects/rails/actionview (master)$ ruby -I test test/template/number_helper_test.rb 
0
Oct 21 '16 at 19:05
source share

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

 bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/sample/'" 
0
Feb 05 '19 at 22:16
source share



All Articles