如何从rails测试套件运行单个测试?

时间:2021-04-19 01:23:49

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

如何从rails测试套件运行单个测试?

rake test ANYTHING seems to not help.

耙子试验似乎没有任何帮助。

10 个解决方案

#1


117  

NOTE: This doesn't run the test via rake. So any code you have in Rakefile will NOT get executed.

注意:这不是通过rake运行测试。所以Rakefile中所有的代码都不会被执行。

To run a single test, use the following command from your rails project's main directory:

要运行单个测试,请使用以下来自rails项目主目录的命令:

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:

它运行一个名为“name”的测试,在指定文件的MyModelTest类中定义。test_name是通过取测试名形成的,它在前面加上单词“test”,然后用下划线分隔单词。例如:

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

#2


49  

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

(从@Puhlze发表评论。)

#3


13  

For rails 5:

rails 5:

rails test test/models/my_model.rb

#4


10  

Thanks to @James, the answer seems to be:

感谢@James,答案似乎是:

rails test test/models/my_model.rb:22

Assuming 22 is the line number of the given test. According to rails help:

假设22是给定测试的行数。根据rails帮助:

 $ 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):

另外,请注意,您的测试应该从ActionDispatch::IntegrationTest继承来工作(这是我的错误):

class NexApiTest < ActionDispatch::IntegrationTest
.
.
.

#5


6  

In rails 5,

在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://*.com/a/41183694/3626659

看这里https://*.com/a/41183694/3626659

#6


5  

To run a single test in the actual Rails suite:

要在实际的Rails套件中运行一个测试:

bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb

#7


3  

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.

那是我半夜里愚蠢的问题。Rails好心地打印它在rake测试上执行的命令。剩下的就是剪切粘贴练习。

~/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"

#8


1  

If you want to run a single test, you can just run them as a regular Ruby script

如果您想运行一个测试,您可以将它们作为一个常规的Ruby脚本来运行

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.

你也可以运行整个套件。ActiveRecord或ActionMailer)通过cd进入目录并在其中运行rake测试。

#9


0  

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 

#10


0  

The best way is to look directly into the guides: http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests

最好的方法是直接查看指南:http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#运行测试

cd actionmailer
bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout

#1


117  

NOTE: This doesn't run the test via rake. So any code you have in Rakefile will NOT get executed.

注意:这不是通过rake运行测试。所以Rakefile中所有的代码都不会被执行。

To run a single test, use the following command from your rails project's main directory:

要运行单个测试,请使用以下来自rails项目主目录的命令:

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:

它运行一个名为“name”的测试,在指定文件的MyModelTest类中定义。test_name是通过取测试名形成的,它在前面加上单词“test”,然后用下划线分隔单词。例如:

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

#2


49  

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

(从@Puhlze发表评论。)

#3


13  

For rails 5:

rails 5:

rails test test/models/my_model.rb

#4


10  

Thanks to @James, the answer seems to be:

感谢@James,答案似乎是:

rails test test/models/my_model.rb:22

Assuming 22 is the line number of the given test. According to rails help:

假设22是给定测试的行数。根据rails帮助:

 $ 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):

另外,请注意,您的测试应该从ActionDispatch::IntegrationTest继承来工作(这是我的错误):

class NexApiTest < ActionDispatch::IntegrationTest
.
.
.

#5


6  

In rails 5,

在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://*.com/a/41183694/3626659

看这里https://*.com/a/41183694/3626659

#6


5  

To run a single test in the actual Rails suite:

要在实际的Rails套件中运行一个测试:

bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb

#7


3  

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.

那是我半夜里愚蠢的问题。Rails好心地打印它在rake测试上执行的命令。剩下的就是剪切粘贴练习。

~/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"

#8


1  

If you want to run a single test, you can just run them as a regular Ruby script

如果您想运行一个测试,您可以将它们作为一个常规的Ruby脚本来运行

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.

你也可以运行整个套件。ActiveRecord或ActionMailer)通过cd进入目录并在其中运行rake测试。

#9


0  

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 

#10


0  

The best way is to look directly into the guides: http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests

最好的方法是直接查看指南:http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#运行测试

cd actionmailer
bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout