0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPUnit を利用して使い回しできるテストコードを作成する

Last updated at Posted at 2025-08-06

同一処理で別データを利用したいときに試したことを
まとめてみました

想定

APIリクエスト時にさまざまなデータパターンを用意して検証したい

実装

テストコードを作成

php artisan make:test UserStoreTest

その1

1つのテストコードにまとめて書く

<?php

namespace Tests\Feature;

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

class UserStoreTest extends TestCase
{
    /**
     * A basic feature test example.
     */
    public function test_example(): void
    {
        $data = [
            [
                'name' => 'test',
                'email' => 'test@example.com',
                'password' => '1qazxsw2'
            ],
            [
                'name' => 'dummy123',
                'email' => 'dummy123@example.com',
                'password' => '1qazxsw21'
            ]
        ];
        foreach ($data as $key => $value) {
            $response = $this->post(route('user.create'), [
                'name' => $value['name'],
                'email' => $value['email'],
                'password' => $value['password']
            ]);

            $response->assertOk();
            $this->assertDatabaseHas('users', [
                'name' => $value['name']
            ]);
        }
    }
}

その2

データを別途用意して呼び出す

データを別途用意する際、DataProviderモジュールを利用し、
テストコードに指定する必要があります

<?php

namespace Tests\Feature;

use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

class UserStoreTest extends TestCase
{
    /**
     * A basic feature test example.
     */
    #[DataProvider('dataproviderForCreate')]
    public function test_example(array $data): void
    {
        $response = $this->post(route('user.create'), [
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => $data['password']
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('users', [
            'name' => $data['name']
        ]);
    }

    public static function dataproviderForCreate()
    {
        return [
            '正常系' => [
                [
                    'name' => 'test',
                    'email' => 'test@example.com',
                    'password' => '1qazxsw2'
                ]
            ],
            '正常系1' => [
                [
                    'name' => 'dummy123',
                    'email' => 'dummy123@example.com',
                    'password' => '1qazxsw21'
                ]
            ],
        ];
    }
}

実行結果

その1

1つのテストコードにまとめて書く

root@5c512732ee1e:/var/www/html# php artisan test ./tests/Feature/UserStoreTest.php 

   PASS  Tests\Feature\UserStoreTest
  ✓ example                                                                                                                                     0.70s  

  Tests:    1 passed (4 assertions)
  Duration: 0.82s

その2

データを別途用意して呼び出す

root@5c512732ee1e:/var/www/html# php artisan test ./tests/Feature/UserStoreTest.php 

   PASS  Tests\Feature\UserStoreTest
  ✓ example with data set "正常系"                                                                                                                 0.67s  
  ✓ example with data set "正常系1"                                                                                                                0.03s  

  Tests:    2 passed (4 assertions)
  Duration: 0.82s

まとめ

どちらでも同じことができましたが、
その2のほうがどのデータパターンでエラーになっているかわかりやすいと思いました

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?