[ruby-list:49744] Re: test-unitで使用するテストケースを動的に生成したい
From:
Masafumi Yokoyama <myokoym@...>
Date:
2014-01-27 12:46:35 UTC
List:
ruby-list #49744
横山です。
2014年1月25日 14:25 古川大輔 <mogya99@yahoo.co.jp>:
> 教えていただいた案だとテストごとにbrowserを定義しないといけないと思ったので、そこをまとめるイメージで書いてみました。
1つのテストケースクラスに複数のモジュールを
includeする手もありそうだなあと思いました。
(あえてクラスを分けているということでしたらすみません)
# search_tests.rb
module SearchTests
def test_search1
STDERR.puts "[test]%s with %s"%['search1',browser.to_s]
end
end
# add_tests.rb
module AddTests
def test_add1
STDERR.puts "[test]%s with %s"%['add1',browser.to_s]
end
end
# test-browser1.rb
class Browser1Test < Test::Unit::TestCase
include SearchTests
include AddTests
def browser
"browser1"
end
end
# test-browser2.rb
class Browser2Test < Test::Unit::TestCase
include SearchTests
include AddTests
def browser
"browser2"
end
end
# run-test.rb
require "test-unit"
base_dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(base_dir)
require "add_tests"
require "search_tests"
exit Test::Unit::AutoRunner.run(true, base_dir)
このやり方の利点は、特定のブラウザだけで実行するテストを
書きやすい(includeするしないで切り替えられる)ことで、
欠点は全てのブラウザで実行するテストでもいちいちincludeを
書かなければならないことだと思います。
実際にやってみると、
$ ruby run-test.rb
Loaded suite .
Started
[test]add1 with browser1
.[test]search1 with browser1
.[test]add1 with browser2
.[test]search1 with browser2
.
Finished in 0.004 seconds.
4 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifica
tions
100% passed
1000.00 tests/s, 0.00 assertions/s
こんな感じでした。
--
Masafumi Yokoyama @myokoym