ZenTestMemo
RubyのTest::Unitを利用したテストツール、ZenTestについて。
ZenTestは4つのツール(zentest, unit_diff, autotest, multiruby)と1つのライブラリ(Test::Rails)の詰め合わせ。
そのどれもが、「より楽にテスト駆動開発を行う」ために存在する。
zentest
zentestは、ソースからテストを自動生成する。
こういうソースがあるとする。
    class Customer
      attr_accessor :name
      def initialize(name)
        @name = name
        @rentals = Array.new
      end
      def addRental(aRental)
        @rentals.push(aRental)
      end
      ...
    end
コマンドラインでzentestを実行すると、
$ zentest videostore.rb > test_videostore.rb
こんな感じに生成される。
    # Code Generated by ZenTest v. 2.1.2
    #                 classname: asrt / meth =  ratio%
    #                  Customer:    0 /    3 =   0.00%
    require 'test/unit'
    class TestCustomer < Test::Unit::TestCase
      def test_addRental
        raise NotImplementedError, 'Need to write test_addRental'
      end
      def test_name=
        raise NotImplementedError, 'Need to write test_name='
      end
      def test_statement
        raise NotImplementedError, 'Need to write test_statement'
      end
    end
(from http://zentest.rubyforge.org/ZenTest/files/LinuxJournalArticle_txt.html )
unit_diff
unit_diffは、テストの出力をdiff形式に整形してくれる。
$ ruby test/test_parse_tree.rb | unit_diff Loaded suite test/test_parse_tree Started .....FF......................... Finished in 0.347 seconds. 1) Failure: test_case_stmt2(TestParseTree) [(eval):1]: 6,10c6,8 < [:case, < nil, < [:when, [:array, [:lit, 1]], [:lit, 2]], < [:when, [:array, [:lit, 3]], [:lit, 4]], < [:lit, 5]]]]] --- > [:when, [:array, [:lit, 1]], [:lit, 2]], > [:when, [:array, [:lit, 3]], [:lit, 4]], > [:lit, 5]]]] 32 tests, 32 assertions, 1 failures, 0 errors
expectedとactualが長い文字列のときに便利。
(from http://sean-carley.blogspot.com/2006/04/unitdiff-is-your-friend.html )
autotest
autotestは、ファイルの変更を検出して自動的にテストを実行してくれる。
ファイル名は
lib/asdf.rb test/test_asdf.rb
のようにしておく。
$ autotest ...
いちいちruby test/test_hoge.rbとか打たなくていいので便利。
あと自動でsvn updateしてくれる機能とか、テスト結果をhtmlに出力する機能とかもあるらしい。 (参考:http://subtech.g.hatena.ne.jp/secondlife/20061107/1162826406 )
multiruby
multirubyは、各バージョンのRubyに対して自動的にテストを実行してくれる。
% ./bin/multiruby -I../../RubyInline/dev ../../RubyInline/dev/test_inline.rb VERSION = 1.8.2 Loaded suite ../../RubyInline/dev/test_inline Started ................................................... Finished in 3.385808 seconds. 51 tests, 78 assertions, 0 failures, 0 errors RESULT = 0 VERSION = 1.8.3 Loaded suite ../../RubyInline/dev/test_inline Started ................................................... Finished in 3.21357 seconds. 51 tests, 78 assertions, 0 failures, 0 errors RESULT = 0 VERSION = 1.8.4 Loaded suite ../../RubyInline/dev/test_inline Started ................................................... Finished in 3.642159 seconds. 51 tests, 78 assertions, 0 failures, 0 errors RESULT = 0
Rubyのライブラリを書いていて、1.8.xの全部でテスト動かすのめんどくせー!っていう時に便利。
初回起動時にRubyのtarballを取ってきて~/.multiruby(もしくは環境変数のMULTIRUBY)以下にインストールする。 他のバージョンのRubyを追加したいときは .multiruby/versions/ 以下にtarballを置くっぽい。
#青木さんのforall-ruby(google)とちょっと被ってる?
Test::Rails
未調査。
インストール
RubyGemsを使うのが簡単。
gem install ZenTest
View on github | Report issue