LoginSignup
0
1

More than 1 year has passed since last update.

[Laravel]単体テスト(テストケース使用、自動テスト)

Posted at

概要

Laravel のテスト作成時、
全部テストされるのがめんどくさかった

  • グループで指定したメゾットだけテストする
  • ファイルテストする(class)

環境

  • laravel@8
  • Docker
  • TestCase(phpunit)

テスト環境の構築まだな方↓↓

【公式】テストの作成
https://readouble.com/laravel/8.x/ja/testing.html

グループテストする

テストしたいメゾットのコメントに下記内容を記載

  • @test
  • @group groupName

groupNameはグループ名になる
※ TestCaseサンプルコードを記載してるので参照

$ php artisan test --group=testing

// ↓ Docker環境の方
$ docker-compose exec app php artisan test --group=testing

ファイルテストする(class)

XXXTestにはclass
※ 同じ名前のclassもテスト処理する

bash:Feature/Auth/Login/ConstructTest.php
$ php artisan test --filter ConstructTest

// ↓ Docker環境の方
$ docker-compose exec app php artisan test --filter ConstructTest

TestCaseサンプルコード

Feature/Auth/Login/AuthenticatedTest.php

namespace Tests\Feature\Auth\Login;

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

//   ↓ classテスト
class AuthenticatedTest extends TestCase
{
    /**
     * A basic feature test example.
     * 
     * @test ← グループテスト
     * @group testing ← グループテスト
     * 
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/login');

        $response->assertStatus(200);
    }
}

0
1
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
0
1