sudo gem install cucumber mechanize rspec webratPHP – cucumber – GitHub
を参考にCucumberでPHPアプリケーションの受け入れテストを作成・実行してみます。さらにこのチュートリアルでは日本語でテストを記述してみます。
Cucumberのインストール
Ruby 1.9.1をすでにインストールしてある環境です。まずはCucumber・Webrat・mechanize・rspecをインストールします。
$ sudo sudo gem install cucumber mechanize rspec webrat |
ディレクトリの作成
テストするPHPアプリのトップディレクトリに以下のディレクトリを作成します。
features features/support features/step_definitions
環境設定ファイルの作成
features/support/env.rbを以下の内容で作成します。RSpecやWebratを読み込んで、各種設定を行っています。Seleniumを利用するといった場合はこのファイルを編集して設定を変更します。
# RSpec require 'spec/expectations' # Webrat require 'webrat' require 'test/unit/assertions' World(Test::Unit::Assertions) Webrat.configure do |config| config.mode = :mechanize end World do session = Webrat::Session.new session.extend(Webrat::Methods) session.extend(Webrat::Matchers) session end |
Cucumberのコマンドライン引数の設定
Cucumberのコマンドライン引数のデフォルトを設定するために以下の内容でルートディレクトリにcucumber.ymlを作成します。–language jaを指定することで日本語でテストを記述できるようになります。以下のdefaultではコマンドラインに結果が出力されますが、結果をHTMLで出力する設定を記述しておき、テスト実行時に適宜設定を指定することができます。
default: --language ja features |
ステップの作成
features/step_definitionsディレクトリにwebrat_steps.rbとresult_steps.rbの2つのテストステップファイルを作成します。
features/step_definitions/webrat_steps.rb
Webratでページにアクセスしたりボタンをクリックしたりできるステップを作成します。
# -*- encoding: UTF-8 -*- 前提 /^(.*)ページを表示している$/ do |path| @response = visit "http://localhost/php_with_cucumber#{path}" end もし /^(.*)ページを表示する$/ do |path| @response = visit "http://localhost/php_with_cucumber#{path}" end もし /^"(.*)"ボタンをクリックする$/ do |button| @response = click_button(button) end もし /^"(.*)"リンクをクリックする$/ do |link| @response = click_link(link) end もし /^"(.*)"に"(.*)"と入力する$/ do |field, value| @response = fill_in(field, :with => value) end もし /^"(.*)"から"(.*)"を選択する$/ do |field, value| @response = select(value, :from => field) end もし /^"(.*)"をチェックする$/ do |field| @response = check(field) end もし /^"(.*)"のチェックを外す$/ do |field| @response = uncheck(field) end もし /^"(.*)"を選択する$/ do |field| @response = choose(field) end もし /^パスが"(.*)"のファイルを"(.*)"に添付する $/ do |path, field| @response = attach_file(field, path) end |
ページの表示ではhttp://localhost/php_with_cucumberを直に書き込んで、テストを記述するfeatureファイルではアドレスを省略できるようにしています。
features/step_definitions/result_steps.rb
結果の検査を行うステップを作成します。
# -*- encoding: UTF-8 -*- ならば /^"(.*)"と表示される$/ do |text| response_body.to_s.force_encoding("UTF-8").should =~ /#{text}/m end ならば /^"(.*)"と表示されない$/ do |text| response_body.to_s.force_encoding("UTF-8").should_not =~ /#{text}/m end ならば /^(\w+)メッセージが表示さる$/ do |message_type| @response.should have_xpath("//*[@class='#{message_type}']") end ならば /^(.*)リクエストが失敗する/ do |_| @response.should_not be_successful end ならば /ページ読み込みが成功する/ do @response.code.should == "200" end |
今回のテストでは使用しないステップも含まれています。実際に動作するか検証してませんが、こんな感じで作成するということで参考にしてください。
テスト対象のコード
実際にはテストを記述してコードを作成していくかもしれませんが、今回はすでに作成した以下のPHPコードをindex.phpとして保存してテストしてみます。機能はテキストエリアにテキストを入力してSubmitをクリックすると、入力したテキストが表示されるというものです。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head> <title>PHP with Cucumber</title> </head> <body> <h1>PHP with Cucumber</h1> <?php if(!isset($_GET['text']) or empty($_GET['text'])) print '<p>テキストを入力してください。</p>'; ?> <form action="/php_with_cucumber/index.php" method="get"> <p><textarea name="text" rows="10" cols="100"></textarea></p> <p><input type="submit" value="Submit" /></p> </form> <p class="printed_text"><?php if(isset($_GET['text'])) print $_GET['text']; ?></p> <p><a href="./">リセット</a></p> </body> </html> |
テストの作成
テストを作成します。featuresディレクトリにshow.featureファイルを作成します。ページの表示・結果の表示・表示のリセットをテストします。
機能: トップページの表示とサブミット シナリオ: トップページの表示 もし /ページを表示する ならば ""と表示される かつ "テキストを入力してください。"と表示される シナリオ: テキストの入力と結果の表示してリセットする 前提 /ページを表示している もし "text"に"テスト用のテキストです。"と入力する かつ "submit"ボタンをクリックする ならば "テスト用のテキストです。"と表示される かつ "テキストを入力してください。"と表示されない もし "リセット"リンクをクリックする ならば "テキストを入力してください。"と表示される |
もっと機能があれば機能ごとにファイルを分割します。またこの例では、テスト内容は不十分ですので、ステップやシナリオを追加して試してみてください。
テストの実行
ではテストを実行してみます。コマンドラインでルートディレクトリに移動してcucumberを実行します。
$ cucumber Using the default profile... 機能: トップページの表示とサブミット シナリオ: トップページの表示 # features/show.feature:3 もし /ページを表示する # features/step_definitions/webrat_steps.rb:7 ならば ""と表示される # features/step_definitions/result_steps.rb:3 かつ "テキストを入力してください。"と表示される # features/step_definitions/result_steps.rb:3 シナリオ: テキストの入力と結果の表示してリセットする # features/show.feature:8 前提 /ページを表示している # features/step_definitions/webrat_steps.rb:3 もし "text"に"テスト用のテキストです。"と入力する # features/step_definitions/webrat_steps.rb:19 かつ "submit"ボタンをクリックする # features/step_definitions/webrat_steps.rb:11 ならば "テスト用のテキストです。"と表示される # features/step_definitions/result_steps.rb:3 かつ "テキストを入力してください。"と表示されない # features/step_definitions/result_steps.rb:7 もし "リセット"リンクをクリックする # features/step_definitions/webrat_steps.rb:15 ならば ページ読み込みが成功する # features/step_definitions/result_steps.rb:19 かつ "テキストを入力してください。"と表示される # features/step_definitions/result_steps.rb:3 2 scenarios (2 passed) 11 steps (11 passed) 0m0.029s |
すべてのシナリオがパスしました。ここで、たとえば「ならば “テスト用のテキストです。”と表示される」を「ならば “テストのテキストです。”と表示される」に変更してCucumberを実行してみると、11 steps (1 failed, 4 skipped, 6 passed)となります。コマンドラインではカラーで表示されるので、真っ赤な画面だったらfailedで、緑だったらpassedです。
おまけ
作成したファイルの書庫: php_with_cucumber.zip
最終的なディレクトリ・ファイル構成
. |-- cucumber.yml |-- features | |-- show.feature | |-- step_definitions | | |-- result_steps.rb | | `-- webrat_steps.rb | `-- support | `-- env.rb `-- index.php