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?

More than 1 year has passed since last update.

LaravelでのDuskテストのバッチ処理と定期実行

Last updated at Posted at 2023-06-04

記載していること

この記事では、Laravel Duskをバッチで利用する方法について説明する。
Duskは、ブラウザ自動化テストで、処理の仕方が決まっている。

このduskをコマンドで実行するように、定期的に実行する方法を記載した。

準備

Duskインストール

composer require --dev laravel/dusk
php artisan dusk:install

Duskテストの作成

php artisan dusk:make TestName

コンソールコマンドの作成

php artisan make:command RunDuskTestsCommand

app/Console/CommandsディレクトリにRunDuskTestsCommand.phpが作成される。

作成例

1.RunDuskTestsCommand.phpを編集
作成したコマンド内で、handleメソッドに、Artisan::call('dusk')などと記述してDuskテストを実行。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class RunDuskTestsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'run:dusk-tests';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Duskのテスト';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        //
        $this->info('Running Dusk tests...');
        Artisan::call('dusk');
    }
}

以下のコマンドが生成され、php artisan listで確認できる。
コマンドのファイル修正で、リアルに反映される。(Laravel5.4以降にそうなったらしい)

 run
  run:dusk-tests          Duskのテスト

以下のコマンドのバッチ実行で、php artisan duskと同じ結果になる。

php artisan run:dusk-tests

※上記の手順をスケジュール登録して実行する場合は以下の手順を追加で行います。
2.スケジュールの設定
app/Console/ディレクトリにあるKarnel.phpに以下を追加

protected function schedule(Schedule $schedule)
{
    $schedule->command('run:dusk-tests')->daily();
}

ここでは schedule メソッド内に run:dusk-tests コマンドを daily として追加している。

3.crontab -eで以下を追加
path-to-your-projectは作成したプロジェクト

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

まとめ

Laravelのバッチ処理処理機能を、dusk、スケジュール登録を例に記載した。

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?