33
Behatエクステンション の作り方 @hanhan1978 2015/06/28 PHP Conf Fukuoka 2015

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

Embed Size (px)

Citation preview

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

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

@hanhan1978 2015/06/28

PHP Conf Fukuoka 2015

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

自己紹介

所属

@hanhan1978

Ryo Tomidokoro

Blog : http://hanhans.net/blog/hanhan/

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

アウトライン

1.Behatについて

2.Behatエクステンションの作り方

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

1. Behatについて

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

Behatについて• BDDテストフレームワーク

• Cucumberにinspireされて作られた。

• テストシナリオはGherkin形式

• PHP製

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

Behatでできること• デフォルト状態では何もない。

• テスト(feature)、アサーション(step)は自分で作る

• extensionを導入して使うことが多い

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

主なextension• Mink

• Symfony2

• Laravel

• CakePHP

• Drupal

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

最近のトレンド

behat

phpspec

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

rspecにはまだまだ及ばない

behat

phpspec

rspec

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

Behatでテストを作る流れ

Page 11: 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"

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

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(); } }

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

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)

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

作るファイルは最低限2つ .features/ ├── bootstrap │   └── FeatureContext.php └── sample.feature

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

Minkを使ったウェブアプリのE2Eテスト

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

1. エクステンションの読み込みdefault: extensions: Behat\MinkExtension: base_url: http://www.yahoo.co.jp/ goutte: ~ suites: default: contexts: - 'Behat\MinkExtension\Context\MinkContext' paths: features: features bootstrap: %behat.paths.features%/bootstrap

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

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"

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

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)

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

作るファイルは最低限3つ. ├─ behat.yml └─ features/ ├── bootstrap │   └── FeatureContext.php └── sample.feature

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

Behatの良さとは?• PHP製であるということ

• featureを書く => stepを書く「直感的」

• テスト作成のコストが低い(エクステンション利用)

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

2. Behatエクステンションの作成

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

エクステンションとは?

• 実体はstepの集まり

• 自作のstepをプロジェクト間で共有する

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

エクステンションを作る流れ

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

1. インタフェースを用意するsrc ├── Context │   ├── Initializer │   │   └── RestApiAwareInitializer.php │    └── RestApiContext.php └── ServiceContainer └── RestApiExtension.php

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

2. 自作エクステンションの読み込みdefault: extensions: Behat\RestApiExtension: base_url: http://127.0.0.1:3000/ suites: default: contexts: - 'Behat\RestApiExtension\Context\RestApiContext'

paths: features: features bootstrap: %behat.paths.features%/bootstrap

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

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 "([^"]*)"$/

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

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"

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

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"

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

サンプルソースhttps://github.com/hanhan1978/RestApiExtension

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

エクステンションを作る意味

• テストstepを共有する

• OSSエクステンションによるBehatの普及

• 既存エクステンションへの理解も深まる

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

まとめ

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

• Behatは、使い勝手の良いBDDフレームワーク

• 操作は直感的。PHP製

• お昼休みとかに試せるよ!

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

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ポスト