LoginSignup
4
2

More than 3 years have passed since last update.

Laravel テストコードを書いてみよう

Last updated at Posted at 2020-09-29

目的

  • Laravelのフューチャーテストコードの書き方をまとめる

実施環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.5)
ハードウェア MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
プロセッサ 2 GHz クアッドコアIntel Core i5
メモリ 32 GB 3733 MHz LPDDR4
グラフィックス Intel Iris Plus Graphics 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHP バージョン 7.4.8 Homwbrewを用いて導入
Laravel バージョン 8.6.0 commposerを用いてこちらの方法で導入→Mac Laravelの環境構築を行う
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いてこちらの方法で導入→Mac HomebrewでMySQLをインストールする

前提条件

  • 実施環境と同じ、もしくは準ずる環境が構築されていること。

前提情報

  • Laravelのテストはフューチャーテストとユニットテストが存在しこの記事ではフューチャーテストの方法を紹介する。
  • MacのローカルにLaravelアプリを作成し$ php artisan serveコマンドを用いてローカルサーバを起動して確認するものする。
  • 「特定の文字がビューに表示されているか」と「Httpステータスコードの200が帰ってくるか」を確認する。

概要

  1. アプリの作成
  2. ルート情報の記載
  3. コントローラーファイルの作成と記載
  4. ビューファイルの作成と記載
  5. テストコードの作成と記載
  6. テストの実行

詳細

  1. アプリの作成

    1. 任意のディレクトリで下記コマンドを実行してLaravelアプリを作成する。

      $ laravel new feature_test
      
    2. 先のコマンドで作成されたfeature_testディレクトリに移動する。

  2. ルート情報の記載

    1. feature_testディレクトリで下記コマンドを実行してルーティングファイルを開く。

      $ vi routes/web.php
      
    2. 下記のルーティング情報を追記する。

      feature_test/routes/web.php
      Route::get('/hello', 'App\Http\Controllers\HelloController@index');
      
    3. 追記後のルーティングファイルの内容を下記に記載する。

      feature_test/routes.web.php
      <?php
      
      use Illuminate\Support\Facades\Route;
      
      /*
      |--------------------------------------------------------------------------
      | Web Routes
      |--------------------------------------------------------------------------
      |
      | Here is where you can register web routes for your application. These
      | routes are loaded by the RouteServiceProvider within a group which
      | contains the "web" middleware group. Now create something great!
      |
      */
      
      Route::get('/', function () {
          return view('welcome');
      });
      // 下記を追記する。
      Route::get('/hello', 'App\Http\Controllers\HelloController@index');
      
  3. コントローラーファイルの作成と記載

    1. feature_testディレクトリで下記コマンドを実行してコントローラファイルを作成する。

      $ php artisan make:controller HelloController
      
    2. feature_testディレクトリで下記コマンドを実行して先に作成したコントローラファイルを開く。

      $ vi app/Http/Controllers/HelloController.php
      
    3. 開いたコントローラファイルに下記のアクションを追記する。

      feature_test/app/Http/Controllers/HelloController.php
          public function index()
      {
          return view('hello/index');
      }
      
    4. 追記後のコントローラファイルの内容を下記に記載する。

      feature_test/test/Http/Controllers/HelloController.php
      <?php
      
      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      
      class HelloController extends Controller
      {
          public function index()
          {
              return view('hello/index');
          }
      }
      
  4. ビューファイルの作成と記載

    1. feature_testディレクトリで下記コマンドを実行してビューファイルを格納するディレクトリを作成する。

      $ mkdir resources/views/hello
      
    2. feature_testディレクトリで下記コマンドを実行してビューファイルを作成する。

      $ vi resources/views/hello/index.blade.php
      
    3. 開いたファイルに下記の内容を記載する。

      resources/views/hello/index.blade.php
      <h1>hello japan!</h1>
      
    4. feature_testディレクトリで下記コマンドを実行してローカルサーバを起動する。

      $ php artisan serve
      
    5. 下記にアクセスして「Hello japan!」の文字が表示されていることを確認する。

  5. テストコードの作成と記載

    1. feature_testディレクトリで下記コマンドを実行してテストコード用のファイルを作成する。

      $ php artisan make:test HelloIndexPageTest
      
    2. feature_testディレクトリで下記コマンドを実行して先に作成したテストコードファイルを開く。

      $ vi tests/Feature/HelloIndexPageTest.php
      
    3. 開いたテストコードファイルを下記の様に追記・修正をする。

      feature_test/tests/Feature/HelloIndexPageTest.php
      <?php
      
      namespace Tests\Feature;
      
      use Illuminate\Foundation\Testing\RefreshDatabase;
      use Illuminate\Foundation\Testing\WithFaker;
      use Tests\TestCase;
      
      class HelloIndexPageTest extends TestCase
      {
          /**
           * A basic feature test example.
           *
           * @return void
           */
          public function testStatusCode()
          {
              $response = $this->get('/hello');
      
              $response->assertStatus(200);
          }
      
          public function testBody()
          {
              $response = $this->get('/hello');
      
              $response->assertSeeText('hello japan!');
          }
      }
      
  6. テストの実行

    1. feature_testディレクトリで下記コマンドを実行してテストを実行する。

      $ vendor/bin/phpunit tests/Feature/HelloIndexPageTest.php
      
    2. 下記の様に出力されればテストは完了であり、当該ページはテスト条件を満たしている。

      OK (2 tests, 2 assertions)
      

参考文献

4
2
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
4
2