LoginSignup
7
8

More than 5 years have passed since last update.

Behat-Laravel-Extensionを使ってLaravel5のログイン認証をテストする

Posted at

やること

Laravel5にcomposerでBehat-Laravel-Extensionを追加してログイン認証をテストしてみる。
GitHub:Behat-Laravel-Extension

まずはLaravel標準のログイン認証を練習台にして、behatの書き方に慣れようという狙い。

Behat-Laravel-Extensionを導入する

以前書いた記事Laravel5.1でbehatを使ってみるを参照

テストする内容

Laravel5にデフォルトで用意してある認証の動作として把握してる+追加した以下の振る舞いをテストする。
※ルートをログイン必須にしていたり、デザインやエラー文言の変更をしているので、そこは変更した内容で書いています。

  1. ログインが必要なページ(ルート)にアクセス時にログインしていない場合、ログインページに遷移する事。
  2. メールアドレスの入力は必須。(パスワードは入力)
  3. パスワードの入力は必須。(メールアドレスは入力)
  4. メールアドレスとパスワード必須。
  5. 登録されていないメールアドレスと、パスワードでログインできない。
  6. 登録されているメールアドレスと、パスワードでログインできる。

featureを書く前に書き方を勉強

Behat-Laravel-ExtensionをREADME.md通りに入れていれば
MinkExtensionが継承されているので、MinkExtensionを使った場合の書き方になる。

使える書き方は以下のコマンドで確認が可能。
$ ./vendor/behat/behat/bin/behat -dl

日本語でも書くことができるので、その場合テストを書くfeatureファイルの頭に
# language: ja
と追加してあげればよい。

日本語で書く場合も英語で書く場合も、MinkExtensionのリポジトリで言語ファイルを見ると分かりやすい。
https://github.com/Behat/MinkExtension/blob/master/i18n/ja.xliff

featureを書いてみる

以上のことを確認したところで実際に書いてみる。
コメントで意味を書いてますが、2回目以降の同じ意味はスルー

auth.feature
# テスト名と内容の説明
Feature: Laravel WebAuth
  It has access to the top page.
  Change the behavior redirect on whether authenticated.

# 1.ログインが必要なページ(ルート)にアクセス時にログインしていない場合、ログインページに遷移する事。
  Scenario: Non Authenticated on redirect to login page 
    # トップページにアクセスしたら
    Given I am on the homepage
    # ログインページへリダイレクトされる
    Then I should be on "/auth/login"


# 2. メールアドレスの入力は必須。(パスワードは入力)
  Scenario: try required error of email
    # ログインページにアクセスして
    Given I am on "/auth/login"
    # formに値を入れる
    When I fill in the following:
    # input[name=email]は空
      | email |  |
    # input[name=password]には「password123456」って入力する
      | password | password123456 |
    # 「ログインする」って書いてあるボタンを押す
    And I press "ログインする"
    # 再びログインページに遷移する
    Then I should be on "/auth/login"
    # 「ログインに失敗しました。」って表示されている
    And I should see "ログインに失敗しました。"
    # 「メールアドレスは必須です。」って表示されている
    And I should see "メールアドレス は必須です。"


# 3. パスワードの入力は必須。(メールアドレスは入力)
  Scenario: try required error of password, and email input keep
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password |  |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "パスワード は必須です。"
    # メールアドレスは入力していた内容が保持されている
    And the "email" field should contain "test@example.com"


# 4. メールアドレスとパスワード必須。
  Scenario: try required error of email and password
    Given I am on "/auth/login"
    When I fill in the following:
      | email |  |
      | password |  |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "メールアドレス は必須です。"
    And I should see "パスワード は必須です。"


# 5. 登録されていないメールアドレスと、パスワードでログインできない。
  Scenario: try error on login
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password | passwordng |
    And I press "ログインする"
    Then I should be on "/auth/login"
    And I should see "ログインに失敗しました。"
    And I should see "メールアドレスとパスワードをお確かめください。"
    And the "email" field should contain "test@example.com"


# 6. 登録されているメールアドレスと、パスワードでログインできる。
  Scenario: auth success
    Given I am on "/auth/login"
    When I fill in the following:
      | email | test@example.com |
      | password | passwordok |
    And I press "ログインする"
    # ログイン後のページにリダイレクトされている!
    Then I should be on "/"
    # 「ログアウト」という内容がレスポンスに含まれている
    # ※デザイン上ログアウトという文字をtooltipにしているので I should see では判定できないのでこちらを使う。
    And the response should contain "ログアウト"

テストを走らせる

以下のコマンドでテストしてみる。
$ ./vendor/behat/behat/bin/behat

テスト結果が表示され、無事成功すればOK!
ブラウザで同じ操作すると動くけど、behatだと失敗するという場合
featureファイルに以下の中から特定するのにちょうどよさそうなのを追加して状況を確認すると特定しやすい。

Then print current URL
Then print last response
Then show last response
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8