代码之家  ›  专栏  ›  技术社区  ›  Claudio Acciaresi

如何分析Ruby单元测试的执行情况?

  •  4
  • Claudio Acciaresi  · 技术社区  · 15 年前

    我想知道运行这10个最耗时的测试需要多少时间。就像我能处理RSPEC一样,有什么建议吗?

    3 回复  |  直到 10 年前
        1
  •  7
  •   Joseph Holsten    15 年前

    简短回答:

    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
    

    红宝石1.9的用途 MiniTest 作为其默认测试框架而不是 Test::Unit . 小型测试更小、更快,具有更多有用的特性,并且在很大程度上与test::unit向后兼容。其中一个较新的特性是测量每个测试所用的时间 -v 旗帜。运行e时,请确保将此标志放在脚本之后

    如果,像在轨道上一样,你使用 Rake::TestTask 要运行测试,您可以在运行时通过运行

    rake test TESTOPTS='-v'
    

    或者通过添加 -V options 属性,就像这样

    Rake::TestTask.new do |t|
      t.options = '-v'
    end
    

    最后,如果您使用的是Rails,而Minitest对您来说还不够好,那么您可能会感激 test_benchmark 插件。用法简单。将以下行添加到 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)
    

    很抱歉这么说 最小的 以及 测试基准 插件彼此不兼容,但我强烈建议您尝试 最小的 因为它将使您的测试更快,并将继续在Ruby1.9中得到支持。

        2
  •  0
  •   Timo Geusch    15 年前

    可以在Ruby分析器下运行单元测试,方法是使用 ruby -rprofile 而不仅仅是 ruby ?

        3
  •  0
  •   mahemoff    10 年前

    配置文件是一个新的gem,它列出了标准测试运行后的最严重的问题。

    https://github.com/nmeans/minitest-profile

    推荐文章