覚書です。
とりあえず簡単なfeatureを書いては実行して、どういう動きするのかを確認してみました。
初期設定
https://blog.hanhans.net/2015/06/26/behat-15minutes/
この記事の内容をほぼそのままやりました。
ここで紹介するContextは大体このやり方で使えます。
Contextを試してみる
Then /^print current URL$/
今いるぺーじのURLを出力する。
どのページをテストしているかとかを確認する時に利用するっぽい。
コード
Feature: test Gherkin context
Scenario: check Then print current URL
Given I am on the homepage
Then print current URL
実行結果
Feature: test Gherkin context
Scenario: check Then print current URL # features/test.feature:3
Given I am on the homepage # Behat\MinkExtension\Context\MinkContext::iAmOnHomepage()
Then print current URL # Behat\MinkExtension\Context\MinkContext::printCurrentUrl()
│ http://www.yahoo.co.jp/
Then /^print last response$/
最後のレスポンス内容を出力する。
HTML返されると出力エライことになるから、JSON APIのテスト時とかに使うような気がする。
コード
Feature: test Gherkin context
Scenario: check Then print last response
Given I am on "https://wp-kyoto.net/wp-json/wp/v2/hoge"
Then print last response
実行結果
Feature: test Gherkin context
Scenario: check Then print last response # features/test.feature:7
Given I am on "https://wp-kyoto.net/wp-json/wp/v2/hoge" # Behat\MinkExtension\Context\MinkContext::visit()
Then print last response # Behat\MinkExtension\Context\MinkContext::printLastResponse()
│ https://wp-kyoto.net/wp-json/wp/v2/hoge
│
│ {"code":"rest_no_route","message":"URL\u3068\u30ea\u30af\u30a8\u30b9\u30c8\u30e1\u30bd\u30c3\u30c9\uff08request method\uff09\u306b\u5408\u3063\u305f\u30eb\u30fc\u30c8\u304c\u3042\u308a\u307e\u305b\u3093","data":{"status":404}}
Then the response status code should be (?P<code>\d+)$/
レスポンスステータスコードを確認する。
Then the response status code should not be (?P<code>\d+)$/
もあるので、「レスポンスが500エラーを返していないか?」とか「APIが200を返しているか?」などのテストができる。
サンプルコード
Feature: test Gherkin context
Scenario: check Then response status code should be
Given I am on "https://wp-kyoto.net/wp-json/wp/v2/hoge"
Then the response status code should be 404
Scenario: check Then response status code should not be
Given I am on "https://wp-kyoto.net/wp-json/wp/v2/posts"
Then the response status code should not be 404
実行結果
Feature: test Gherkin context
Scenario: check Then response status code shold be # features/test.feature:11
Given I am on "https://wp-kyoto.net/wp-json/wp/v2/hoge" # Behat\MinkExtension\Context\MinkContext::visit()
Then the response status code should be 404 # Behat\MinkExtension\Context\MinkContext::assertResponseStatus()
Scenario: check Then response status code shold not be # features/test.feature:15
Given I am on "https://wp-kyoto.net/wp-json/wp/v2/posts" # Behat\MinkExtension\Context\MinkContext::visit()
Then the response status code should not be 404
まとめ
今回試したContextでできそうなこと。
- APIが正しいレスポンスを返しているかのテスト
- webアプリのプロビジョンが正しく機能しているか
- APIのレスポンス内容確認