Behatエクステンション
の作り方
@hanhan1978
2015/06/28
PHP Conf Fukuoka 2015
自己紹介
所属
@hanhan1978
Ryo Tomidokoro
Blog : http://hanhans.net/blog/hanhan/
アウトライン
1.Behatについて
2.Behatエクステンションの作り方
1. Behatについて
Behatについて
• BDDテストフレームワーク
• Cucumberにinspireされて作られた。
• テストシナリオはGherkin形式
• PHP製
Behatでできること
• デフォルト状態では何もない。
• テスト(feature)、アサーション(step)は自分で
作る
• extensionを導入して使うことが多い
主なextension
• Mink
• Symfony2
• Laravel
• CakePHP
• Drupal
最近のトレンド
behat
phpspec
rspecにはまだまだ及ばない
behat
phpspec
rspec
Behatでテストを作る流れ
1. Featureを書く
# language: en
Feature: Do Some Sample Testing
Scenario: ls
Given I am in the "directory"
When I execute "command"
Then I should get "file1,file2"
2. Stepを実装する
<?php
/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context, SnippetAcceptingContext
{
/**
* @Given I am in the :arg1
*/
public function iAmInThe($arg1)
{
throw new PendingException();
}
}
3. テストを実行する
./bin/behat
Feature: Do Some Sample Testing
Scenario: ls
Given I am in the "directory"
TODO: write pending definition
When I execute "command"
Then I should get "file1,file2"
1 scenario (1 pending)
3 steps (1 pending, 2 skipped)
0m0.06s (9.26Mb)
作るファイルは最低限2つ
.features/
├── bootstrap
│   └── FeatureContext.php
└── sample.feature
Minkを使ったウェブアプリのE2E
テスト
1. エクステンションの読み込み
default:
extensions:
BehatMinkExtension:
base_url: http://www.yahoo.co.jp/
goutte: ~
suites:
default:
contexts:
- 'BehatMinkExtensionContextMinkContext'
paths:
features: features
bootstrap: %behat.paths.features%/bootstrap
2. Featureを書く
# language : en
Feature: stock search
Scenario: get stock code
Given I am on the homepage
When I follow "ファイナンス"
When I fill in "searchText" with "九州電力"
When I press "searchButton"
Then I should see "9508"
3. テストを実行する
.$ ./bin/behat
Feature: stock search
Scenario: get stock code
Given I am on the homepage)
When I follow "ファイナンス"
When I fill in "searchText" with "九州電力"
When I press "searchButton"
Then I should see "9508"
1 scenario (1 passed)
5 steps (5 passed)
0m0.77s (13.74Mb)
作るファイルは最低限3つ
.
├─ behat.yml
└─ features/
├── bootstrap
│   └── FeatureContext.php
└── sample.feature
Behatの良さとは?
• PHP製であるということ
• featureを書く => stepを書く「直感的」
• テスト作成のコストが低い

(エクステンション利用)
2. Behatエクステンション
の作成
エクステンションとは?
• 実体はstepの集まり
• 自作のstepをプロジェクト間で共有する
エクステンションを作る流れ
1. インタフェースを用意する
src
├── Context
│   ├── Initializer
│   │   └── RestApiAwareInitializer.php
│    └── RestApiContext.php
└── ServiceContainer
└── RestApiExtension.php
2. 自作エクステンションの読み込み
default:
extensions:
BehatRestApiExtension:
base_url: http://127.0.0.1:3000/
suites:
default:
contexts:
- 'BehatRestApiExtensionContextRestApiContext'
paths:
features: features
bootstrap: %behat.paths.features%/bootstrap
3. RestAPIテスト用拡張Step一覧
$ ./bin/behat -dl
default | When /^I set header "([^"]*)" with "([^"]*)"$/
default | When /^I set parameter "([^"]*)" with "([^"]*)"$/
default | When /^I set array parameter "([^"]*)" with "([^"]*)"$/
default | When /^I send GET request to "([^"]*)"$/
default | Then /^I should get json response equal to '([^']*)'$/
default | Then /^the response status code should be (d+)$/
default | Then /^the response content-type should be "([^"]*)"$/
default | When /^I send POST request to "([^"]*)"$/
4. Featureを書く
# language : en
#
Feature: REST API TEST
Scenario: Test GET Request
When I set header "Authorization" with "Bearer 12345678"
When I set parameter "username" with "hanhan1978"
When I send GET request to "get"
Then I should get json response equal to
'{"user":"hanhan","prof":"PHPer"}'
Then the response status code should be 200
Then the response content-type should be "application/json"
5. テスト実行する
Feature: REST API TEST
Scenario: Test GET Request
When I set header "Authorization" with "Bearer 12345678"
When I set parameter "username" with "hanhan1978"
When I send GET request to "get"
Then I should get json response equal to
'{"username":"hanhan1978","profile":"PHPer"}'
Then the response status code should be 200
TThen the response content-type should be application/json"
サンプルソース
https://github.com/hanhan1978/RestApiExtension
エクステンションを作る意味
• テストstepを共有する
• OSSエクステンションによるBehatの普及
• 既存エクステンションへの理解も深まる
まとめ
• Behatは、使い勝手の良いBDDフレームワーク
• 操作は直感的。PHP製
• お昼休みとかに試せるよ!
http://hanhans.net/blog/hanhan/2015/06/26/15分で
作れるbehatテスト-e2e編/
http://hanhans.net/blog/hanhan/2015/06/26/behat
エクステンション作成/
http://hanhans.net/blog/hanhan/2015/06/26/はじめて
のbehat/
関連Blogポスト

behatエクステンションの作り方