[英]How to run a single test from a Rails test suite?
如何从 Rails 测试套件运行单个测试?
rake test ANYTHING
似乎都无济于事。
注意:这不会通过rake
运行测试。 所以你在Rakefile
中的任何代码都不会被执行。
要运行单个测试,请在 rails 项目的主目录中使用以下命令:
ruby -I test test/unit/my_model_test.rb -n test_name
这将运行一个名为“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
您可以通过以下方式运行这两个测试:
ruby -I test test/unit/my_model_test.rb
并且只是第二个测试通过
ruby -I test test/unit/my_model_test.rb -n test_invalid_with_bad_attributes
运行测试文件:
rake test TEST=tests/functional/accounts_test.rb
在测试文件中运行单个测试:
rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n /paid accounts/"
(来自@Puhlze 的评论。)
对于导轨 5:
rails test test/models/my_model.rb
感谢@James,答案似乎是:
rails test test/models/my_model.rb:22
假设 22 是给定测试的行号。 根据rails帮助:
$ rails test --help
您可以通过将行号附加到文件名来运行单个测试:
bin/rails test test/models/user_test.rb:27
另外,请注意,您的测试应该继承自 ActionDispatch::IntegrationTest 才能正常工作(这是我的错误):
class NexApiTest < ActionDispatch::IntegrationTest
.
.
.
我用这种方式运行单个测试文件(一个文件中的所有测试)
rails test -n /TopicsControllerTest/ -v
另一种选择是使用行号(打印在失败的测试下方):
rails test test/model/my_model.rb:15
在我的rake
情况下,仅适用TESTOPTS="-n='/your_test_name/'"
:
bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/your_test_name/'"
在实际的 Rails 套件中运行单个测试:
bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb
那是我的一个愚蠢的午夜问题。 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"
最好的方法是直接查看指南: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
如果您想运行单个测试,您可以将它们作为常规 Ruby 脚本运行
ruby actionmailer/test/mail_layout_test.rb
您还可以通过cd
-ing 进入目录并在其中运行rake test
来运行整个套件(例如 ActiveRecord 或 ActionMailer)。
要重新运行刚刚失败的测试,请将失败的测试名称复制粘贴到
rails test -n [test-name]
当您的测试套件报告此情况时:
> rails test
...
Error:
PlayersControllerTest#test_should_show_player:
ActionView::Template::Error: no implicit conversion from nil to integer
你用这个重新运行失败的测试:
rails test -n PlayersControllerTest#test_should_show_player
如果 rake 正在运行 MiniTest,则选项是--name
而不是-n
。
rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"
首先,访问要测试的库的文件夹(这很重要),然后运行:
~/Projects/rails/actionview (master)$ ruby -I test test/template/number_helper_test.rb
导轨文件夹
bundle install
bundle exec ruby -I"activerecord/test" activerecord/test/cases/relation/where_test.rb
请注意,您需要加载适当的文件夹:“activerecord/test”(您有测试的地方)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.