Ruby-on-rails 如何从 Rails 测试套件运行单个测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1506780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a single test from a rails test suite?
提问by artemave
How can I run a single test from a rails test suite?
如何从 rails 测试套件运行单个测试?
rake test ANYTHINGseems to not help.
rake test ANYTHING似乎没有帮助。
回答by Darryl
NOTE:This doesn't run the test via rake. So any code you have in Rakefilewill 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
回答by laffuste
Run a test file:
运行测试文件:
rake test TEST=tests/functional/accounts_test.rb
Run a single testin a test file:
在测试文件中运行单个测试:
rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n /paid accounts/"
(From @Puhlze 's comment.)
(来自@Puhlze 的评论。)
回答by James
For rails 5:
对于导轨 5:
rails test test/models/my_model.rb
回答by Roozbeh Zabihollahi
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 --helpYou can run a single test by appending a line number to a filename:
bin/rails test test/models/user_test.rb:27
$ rails test --help您可以通过将行号附加到文件名来运行单个测试:
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
.
.
.
回答by Alupotha
In rails 5,
在导轨 5 中,
I used this way to run single test file(all the tests in one file)
我用这种方式运行单个测试文件(一个文件中的所有测试)
rails test -n /TopicsControllerTest/ -v
回答by Brian Rose
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
回答by artemave
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 test。剩下的就是剪切和粘贴练习。
~/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"
回答by Aupajo
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 testinside there.
您还可以通过cd-ing 进入目录并rake test在其中运行来运行整个套件(例如 ActiveRecord 或 ActionMailer)。
回答by Diego Plentz
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#running-tests
cd actionmailer
bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout
回答by N0xFF
In my situation for rakeonly works TESTOPTS="-n='/sample/'":
在我的情况下,rake仅适用于TESTOPTS="-n='/sample/'":
bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/sample/'"

