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

后端 未结 12 1711
故里飘歌
故里飘歌 2020-12-07 09:09

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

rake test ANYTHING seems to not help.

相关标签:
12条回答
  • 2020-12-07 09:48

    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
    
    0 讨论(0)
  • 2020-12-07 09:50

    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 讨论(0)
  • 2020-12-07 09:55

    For rails 5:

    rails test test/models/my_model.rb
    
    0 讨论(0)
  • 2020-12-07 09:57

    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.

    0 讨论(0)
  • 2020-12-07 10:03

    To run a single test in the actual Rails suite:

    bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb
    
    0 讨论(0)
  • 2020-12-07 10:03

    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"
    
    0 讨论(0)
提交回复
热议问题