命令行测试

未匹配的标注
本文档最新版为 10.x,旧版本可能放弃维护,推荐阅读最新版!

控制台测试

介绍

除了简化 HTTP 测试之外,Laravel 还提供了一个简单的 API 来测试应用程序的 自定义控制台命令

期望成功/失败

首先,让我们探索如何对 Artisan 命令的退出代码进行断言。为此,我们将使用 artisan 方法从我们的测试中调用 Artisan 命令。然后,我们将使用 assertExitCode 方法断言该命令以给定的退出代码完成:

test('console command', function () {
    $this->artisan('inspire')->assertExitCode(0);
});
/**
 * 测试控制台命令。
 */
public function test_console_command(): void
{
    $this->artisan('inspire')->assertExitCode(0);
}

你可以使用 assertNotExitCode 方法断言命令没有以给定的退出代码退出:

$this->artisan('inspire')->assertNotExitCode(1);

当然,所有终端命令通常在成功时以 0 状态码退出,在不成功时以非零退出码退出。因此,为方便起见,你可以使用 assertSuccessfulassertFailed 断言来断言给定命令是否以成功的退出代码退出:

$this->artisan('inspire')->assertSuccessful();

$this->artisan('inspire')->assertFailed();

期望输入/输出

Laravel 允许你使用 expectsQuestion 方法轻松 「mock」控制台命令的用户输入。此外,你可以使用 assertExitCodeexpectsOutput 方法指定你希望通过控制台命令输出的退出代码和文本。例如,考虑以下控制台命令:

Artisan::command('question', function () {
    $name = $this->ask('What is your name?');

    $language = $this->choice('Which language do you prefer?', [
        'PHP',
        'Ruby',
        'Python',
    ]);

    $this->line('Your name is '.$name.' and you prefer '.$language.'.');
});

你可以通过以下测试来测试该命令:

test('console command', function () {
    $this->artisan('question')
         ->expectsQuestion('What is your name?', 'Taylor Otwell')
         ->expectsQuestion('Which language do you prefer?', 'PHP')
         ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
         ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
         ->assertExitCode(0);
});
/**
 * Test a console command.
 */
public function test_console_command(): void
{
    $this->artisan('question')
         ->expectsQuestion('What is your name?', 'Taylor Otwell')
         ->expectsQuestion('Which language do you prefer?', 'PHP')
         ->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
         ->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
         ->assertExitCode(0);
}

你还可以使用doesntExpectOutput方法断言控制台命令不会生成任何输出:

test('console command', function () {
    $this->artisan('example')
         ->doesntExpectOutput()
         ->assertExitCode(0);
});
/**
 * Test a console command.
 */
public function test_console_command(): void
{
    $this->artisan('example')
            ->doesntExpectOutput()
            ->assertExitCode(0);
}

expectsOutputToContaindoesntExpectOutputToContain 方法可用于对输出的一部分进行断言

test('console command', function () {
    $this->artisan('example')
         ->expectsOutputToContain('Taylor')
         ->assertExitCode(0);
});
/**
 * Test a console command.
 */
public function test_console_command(): void
{
    $this->artisan('example')
            ->expectsOutputToContain('Taylor')
            ->assertExitCode(0);
}

确认期望

当编写一个期望以「是」或「否」答案形式确认的命令时,你可以使用 expectsConfirmation 方法

$this->artisan('module:import')
    ->expectsConfirmation('Do you really wish to run this command?', 'no')
    ->assertExitCode(1);

表期望值

如果命令使用 Artisan 的 table 方法显示信息表,则为整个表编写输出期望值可能会很麻烦。相反,您可以使用expectsTable方法。此方法接受表的标头作为其第一个参数,并将表的数据作为其第二个参数:

$this->artisan('users:all')
    ->expectsTable([
        'ID',
        'Email',
    ], [
        [1, 'taylor@example.com'],
        [2, 'abigail@example.com'],
    ]);

控制台事件

默认情况下,在运行应用程序的测试时,不会调度Illuminate\Console\Events\CommandStartingIlluminate\Console\Events\CommandFinished事件。但是,您可以通过将Illuminate\Foundation\Testing\WithConsoleEvents特征添加到该类来为给定测试类启用这些事件:

<?php

use Illuminate\Foundation\Testing\WithConsoleEvents;

uses(WithConsoleEvents::class);

// ...
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\WithConsoleEvents;
use Tests\TestCase;

class ConsoleEventTest extends TestCase
{
    use WithConsoleEvents;

    // ...
}

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/laravel/11.x/co...

译文地址:https://learnku.com/docs/laravel/11.x/co...

上一篇 下一篇
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
贡献者:4
讨论数量: 0
发起讨论 只看当前版本


暂无话题~