从控制器/端点执行 PHP artisan 测试

问题描述 投票:0回答:1

我想在 laravel 的端点中执行命令“php artisan test”,我创建了命令:

<?php 
namespace App\Console\Commands;

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

class RunTests extends Command
{
    protected $signature = 'run:test';

    protected $description = 'Run tests and display output';

    public function handle()
    {
        Artisan::call('test');
    }
}

我添加到kernel.php:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\RunTests::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        // $schedule->command('cache:prune-stale-tags')->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

后来创建了端点:

Route::get('/test', function(){
    Artisan::call('run:test');
});

但这不起作用,我尝试创建一个命令,使用“exec”函数执行,但没有任何效果,我想执行我调用路由一个端点的所有测试。

laravel testing command laravel-artisan pest
1个回答
0
投票

为什么不直接从路由文件调用 php artisan test 并打印输出?

Route::get('/test', function(){
    // here call test directly and return or dd output
    Artisan::call('test');
    // dd(Artisan::output())
    return Artisan::output();
});
© www.soinside.com 2019 - 2024. All rights reserved.