概要
cucumberの勉強のために簡単なテストの例を書いてました。
cucumberのキモはテスト内容の概要のわかりやすさですかね。
環境
- ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
- rubygems 1.3.1
- cucumber (0.10.0)
- gherkin (2.3.2)
- cucumberを入れるのにgherkinが必要だった。
書いたコード
check.featureとcheck_step.rbは同じディレクトリに配置する。
check.feature
テスト内容を記述する。
-
|
check.feature |
# language: ja
機能: 2つの値か同じか調査
xとyが同じ値かテストする。
シナリオ: 2つの値が同じか
もし x = 5
かつ y = 5
ならば xとyは同じ値である
|
- 日本語でテストを書くために一番上の# language: jaが必要
check_step.rb
テストの定義を記述する。
-
|
check_step.rb |
#!ruby -Ku
When /^(\w+) = (\w+)$/ do |target, value|
# @targets = Hash.new unless @targets
# @targets[target] = value
instance_variable_set("@#{target}", value)
end
Then /^(\w+)と(\w+)は同じ値である$/ do |target0, target1|
# x = @targets[target0]
# y = @targets[target1]
x = instance_variable_get("@#{target0}")
y = instance_variable_get("@#{target1}")
raise "同じ値ではない" unless x == y
end
|
- instance_variable_setで@xや@yを定義している(詳細は下記参照)。
- instance_variable_xxxの代わりにコメントアウトを外しても同じ動作である。
実行方法と結果
-
|
実行方法と結果 |
$ cucumber check.feature
# language: ja
機能: 2つの値か同じか調査
xとyが同じ値かテストする。
シナリオ: 2つの値が同じか # check.feature:5
もしx = 5 # check_step.rb:3
かつy = 5 # check_step.rb:3
ならばxとyは同じ値である # check_step.rb:9
1 scenario (1 passed)
3 steps (3 passed)
0m0.004s
|
参考リンク
最終更新:2010年12月24日 07:32