> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Console tests

> Learn how to test Artisan commands in Laravel, including expectations for input and output, exit code verification, and console event verification.

# Console tests

Laravel lets you write concise tests for Artisan commands that include input and output.

<Info>
  This page is aligned with Laravel's latest console testing API and also covers `expectsSearch` for verifying Laravel Prompts search inputs.
</Info>

## Introduction

Use the `artisan` method to run the command and chain expectations to verify it.

<Tabs>
  <Tab title="Pest">
    ```php theme={null}
    test('question command', function () {
        // Verify user input and output in sequence
        $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);
    });
    ```
  </Tab>

  <Tab title="PHPUnit">
    ```php theme={null}
    public function test_question_command(): void
    {
        // Verify the command's interactive flow
        $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);
    }
    ```
  </Tab>
</Tabs>

```mermaid theme={null}
flowchart TD
    A[Run command via artisan] --> B[Define input expectations]
    B --> C[Verify output or table]
    C --> D[Verify exit status]
```

## Success / failure assertions

You can verify the command's success or failure by checking the exit status.

```php theme={null}
$this->artisan('inspire')->assertExitCode(0);
$this->artisan('inspire')->assertSuccessful();
$this->artisan('inspire')->assertFailed();
```

## Input / output expectations

### Input expectations

You can mock user interactions for question prompts and search inputs.

```php theme={null}
// Mock both question and search inputs
$this->artisan('example')
    ->expectsQuestion('What is your name?', 'Taylor Otwell')
    ->expectsSearch('What is your name?', search: 'Tay', answers: [
        'Taylor Otwell',
        'Taylor Swift',
        'Darian Taylor',
    ], answer: 'Taylor Otwell')
    ->assertExitCode(0);
```

### Output expectations

You can verify exact matches, partial matches, or table output.

```php theme={null}
// Verify that only the expected output is displayed
$this->artisan('users:all')
    ->expectsOutput('The expected output')
    ->doesntExpectOutput('Unexpected output')
    ->expectsOutputToContain('expected')
    ->expectsTable([
        'ID',
        'Email',
    ], [
        [1, 'taylor@example.com'],
        [2, 'abigail@example.com'],
    ])
    ->assertExitCode(0);
```

## Confirmation expectations

For yes/no confirmation prompts, use `expectsConfirmation`.

```php theme={null}
$this->artisan('module:import')
    ->expectsConfirmation('Do you really wish to run this command?', 'no')
    ->assertExitCode(1);
```

## Console events

By default, `CommandStarting` and `CommandFinished` are not fired during tests.

Add `WithConsoleEvents` to test classes that need to verify these events.

```php theme={null}
<?php

namespace Tests\Feature;

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

class ConsoleEventTest extends TestCase
{
    use WithConsoleEvents;
}
```

<Tip>
  Use `WithConsoleEvents` only for the tests that need it to keep your general test speed high.
</Tip>


## Related topics

- [Artisan console](/en/artisan.md)
- [Browser tests (Dusk)](/en/dusk.md)
- [Tutorial - Laravel Console Starter](/en/packages/laravel-console-starter/tutorial.md)
- [Laravel Prompts](/en/prompts.md)
- [Laravel Pint](/en/pint.md)
