LoginSignup
18

More than 5 years have passed since last update.

Laravelアプリで開発テスト - 超準備編 -

Posted at

はじめに

Laravel アプリでの開発テストについて何回かに分けて記事を書いていきたいと思います。(いまのところ4回)

最初の記事はLaravel でテストを書いてみようという超準備編です。公式のマニュアルドキュメントにも載っていますがおさらいも含めてやってみましょう。

前提

  • PHP7.2
  • Laravel5.5(LTS)

プロジェクトをつくる

$ composer create-project --prefer-dist laravel/laravel crud-app "5.5.*"

crud-app というディレクトリができました。Laravel は初期でサンプルのテストコードがあるので早速実行してみましょう。Laravel のテストは PHPUnit をベースにしており、projectをつくったときに一緒にインストールされるので composer コマンドで実行できます。

$ composer exec -v phpunit
> __exec_command: phpunit
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 159 ms, Memory: 12.00MB

OK (2 tests, 2 assertions)

成功しましたね。どういうテストが走っているのか実際にみてみましょう。Laravel ではテストコードは tests ディレクトリ配下に書いていきます。

tests/
├── CreatesApplication.php
├── Feature
│   └── ExampleTest.php
├── TestCase.php
└── Unit
    └── ExampleTest.php

デフォルトでは Feature と Unit というディレクトリに分かれていて、Feature には機能テスト、Unit にはユニットテストを書く構成になっています。中をみるとFeature は Controller のテスト、 Unit は それ以外のテストという感じになっています。

tests/Feature/ExampleTest.php
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}
tests/Unit/ExampleTest.php
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

開発テストの環境をつくる

公式のドキュメントでは Feature のテストについての記載が多くありますが、昨今のシステムでは役割毎にクラスを分けることが多いと思います。なので今回はデフォルトのディレクトリをバッサリ削除してアプリのディレクトリに合わせて構成していきます。

tests/
├── CreatesApplication.php
├── Http
│   └── Controllers
│       └── ExampleTest.php
└── TestCase.php

tests/Feature にあった ExampleTest.phptests/Http/Controllers の下に移動させました。ファイルを開いて namespace をディレクトリに合わせて変更します。

tests/Http/Controllers/ExampleTest.php
<?php

namespace Tests\Http\Controllers;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

// あとは一緒

構成変えたのでテストを実行してみます。

$ composer exec -v phpunit
> __exec_command: phpunit
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.



Time: 34 ms, Memory: 4.00MB

No tests executed!

No tests executed になってしまいました。Laravelでのテストは tests/Featuretests/Unit にあることが初期で設定されているのでテストがないことになってしまってます。こちらの設定を変更します。

テストの設定は phpunit.xml で設定されています。アプリディレクトリの直下に phpunit.xml があるのでこちらをエディタで開いて testsuites のところを以下のように変更します。

phpunit.xml
    <testsuites>
        <testsuite name="App">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>

この設定にすることで tests ディレクトリ配下にある xxxTest.php というファイルをテストコードとしてみなしてくれるようになります。変更を保存したらまたテストを実行してみます。

$ composer exec -v phpunit
> __exec_command: phpunit
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 181 ms, Memory: 12.00MB

OK (1 test, 1 assertion)

今度はちゃんと実行されてテストがpassしたことがわかります。これで開発テストをする準備は完了です。

まとめ

  • Laravelは開発テストの環境は初期からある
  • 初期のテストは破棄してアプリのディレクトリ構成をあわせる
  • tests 以下の構成を変えたら phpunit.xml を変更する

次回はDBをつかったテストについて書いていく予定です。

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
18