基本
Laravel 5のユニットテストについて
-
雛形の作成
雛形は、"artisan make:test [名前]"で作成することができます。 ミドルウェアのCheckForMaintenanceModeクラス (app/Http/Middleware/CheckForMaintenanceMode.php) のユニットテスト雛形を作成する場合は、以下のようにします。
$ php artisan make:test Http/Middleware/CheckForMaintenanceModeTest実行後、以下のファイルが作成されます。
tests/Http/Middleware/CheckForMaintenanceModeTest.php
作成された雛形の中身は以下のようになります。
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CheckForMaintenanceModeTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
ユニットテストの実行
composerのinstallコマンドを実行していれば、phpunitはvendor/phpunit以下にインストールされています。 また、phpunitコマンドのパスは以下になります。
vendor/phpunit/phpunit/phpunit
phpunitコマンドのパスを設定しておけば、testsディレクトリで、phpunitを実行するとユニットテストが実行できます。 また、phpunitコマンドのパスを設定せずに、testsディレクトリに以下のようなシェルスクリプトを作成して実行することもできます。
例 tests/test.sh
#!/bin/sh PHPUNIT=../vendor/phpunit/phpunit/phpunit $PHPUNIT Http/Middleware/CheckForMaintenanceModeTest.php
実行する場合は、以下のようにtestsディレクトリに移動してスクリプトを実行します。
$ cd tests/ $ sh test.sh