Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何评测ruby单元测试的执行情况?_Ruby_Unit Testing - Fatal编程技术网

如何评测ruby单元测试的执行情况?

如何评测ruby单元测试的执行情况?,ruby,unit-testing,Ruby,Unit Testing,我想知道运行10个最耗时的测试花费了多少时间。。就像我可以使用rspec一样,有什么建议吗?可以在ruby profiler下运行单元测试,方法是使用ruby-rprofile执行代码,而不仅仅是ruby?简短回答: gem install minitest # Install MiniTest gem install minitest_tu_shim # Install Test::Unit shim use_minitest yes # Use MiniTest Test::Unit shi

我想知道运行10个最耗时的测试花费了多少时间。。就像我可以使用rspec一样,有什么建议吗?

可以在ruby profiler下运行单元测试,方法是使用
ruby-rprofile
执行代码,而不仅仅是
ruby

简短回答:

gem install minitest # Install MiniTest
gem install minitest_tu_shim # Install Test::Unit shim
use_minitest yes # Use MiniTest Test::Unit shim instead of stdlib Test::Unit 
ruby your_test.rb -v # Run your test in verbose mode
Ruby 1.9用作其默认测试框架,而不是。MiniTest更小、更快,具有更多有用的特性,并且在很大程度上与Test::Unit向后兼容。其中一个较新的功能是使用
-v
标志测量每个测试所花费的时间。运行e时,请确保将此标志放在脚本之后

如果像在rails中一样,您使用来运行测试,那么您可以在运行时通过运行

rake test TESTOPTS='-v'
或者在任务中通过将
-v
添加到
选项
属性中来指定它,如下所示

Rake::TestTask.new do |t|
  t.options = '-v'
end
最后,如果您使用的是rails,而MiniTest对您来说还不够好,那么您可能会喜欢这个插件。使用方便。将以下行添加到您的
config/environments/test.rb

config.gem "timocratic-test_benchmark",
  :lib => 'test_benchmark',
  :source => 'http://gems.github.com'
安装它与

RAILS_ENV='test' rake gems:install
从那时起,当您运行测试时,您将得到一个很好的排序列表

rake test:units
[...]
Test Benchmark Times: Suite Totals:
7.124 test_destroy(FeedTest)
7.219 test_create(FeedTest)
7.646 test_subscribe_to_auto_discovery(FeedTest)
9.339 test_auto_discover_updates_url(FeedTest)
9.543 test_find_or_create_by_auto_discover_url(FeedTest)
15.780 test_import_from_opml(FeedTest)

很抱歉,
MiniTest
test\u benchmark
插件彼此不兼容,但我强烈建议您尝试
MiniTest
,因为它会让您的测试更快,Ruby 1.9将继续支持它。

Minitest::Profile是一个新的gem,它在标准测试运行后列出了最严重的违规者


另一个解决方案是知道每个测试花费了多少时间,而不仅仅是最耗时的10个。也许可以帮助您。现在我正在尝试将rspec移植到单元测试……感谢您的回答,我正在尝试按照您所说的方式运行它,但我得到了以下信息:/usr/lib/ruby/1.8/profiler.rb:13:in
hash:bignum太大,无法转换为
long'(RangeError)最近版本的
测试单元
gem也显示了在详细模式下花费的时间。哎呀,我已经测试过了,我得到了:ARGH!有人定义了test::unit::TestCase,而不是要求/usr/local/lib/site\u ruby/1.8/rubygems.rb:777:“report\u activate\u error”:找不到RubyGem测试单元(=1.2.3)(gem::LoadError)有什么想法吗?