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

后端 未结 12 1710
故里飘歌
故里飘歌 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:39

    In rails 5,

    I used this way to run single test file(all the tests in one file)

    rails test -n /TopicsControllerTest/ -v
    

    look here https://stackoverflow.com/a/41183694/3626659

    0 讨论(0)
  • 2020-12-07 09:39

    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)

    0 讨论(0)
  • 2020-12-07 09:41

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

    0 讨论(0)
  • 2020-12-07 09:41

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

    bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/sample/'"
    
    0 讨论(0)
  • 2020-12-07 09:43

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

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