以下の記述で最低限のテストを実行できる環境を作れる。
コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# テスト用のDBとユーザーを作る mysql > CREATE DATABASE db_name_for_testing; mysql > CREATE USER 'user_for_testing'@'host_name' IDENTIFIED BY 'password'; mysql > GRANT ALL ON db_name_for_testing TO 'user_for_testing'@'host_name'; # .envをコピーして.env.testingを作る $ cp .env .env.tesgint # .env.testingを編集する APP_ENV=testing DB_DATABASE=db_name_for_testing DB_USERNAME=user_for_testing DB_PASSWORD=password # テスト用DBをmigrate $ php artisan migrate --env=testing // --envで環境指定 # テストの生成 $ php artisan make:test MyTest // "tests/Feature"に生成 $ php artisan make:test MyTest --unit // "tests/Unit"に生成 # テストの記述 # tests/Feature/MyTest.php public function myTest() { $response = $this->get('/'); $response->assertStatus(200); } # テストの実行 $ php artisan test // 全てのファイル $ php artisan test tests/Feature/MyTest // ファイルを指定して実行 |
Laravel 7.x テスト: テストの準備
https://readouble.com/laravel/7.x/ja/testing.html
※「テスト」の他のページも参照
コメントを残す