콘솔 테스트
업데이트됨번역일: 2026년 6월 20일
이 페이지는 원문이 업데이트되어 번역이 갱신되었습니다.
- 원문 수정
- 2026년 6월 20일
- 번역 갱신
- 2026년 6월 20일
콘솔 테스트
소개
Laravel은 HTTP 테스트를 간편하게 만드는 것처럼, 커스텀 Artisan 커맨드 테스트를 위한 간단한 API도 제공합니다.
성공 / 실패 검증
Artisan 커맨드가 어떤 종료 코드(exit code)로 끝났는지 검증하는 방법부터 살펴보겠습니다. 테스트에서 artisan 메서드로 커맨드를 실행한 뒤, assertExitCode 메서드로 원하는 종료 코드와 일치하는지 확인할 수 있습니다.
Pest
test('console command', function () {
$this->artisan('inspire')->assertExitCode(0);
});PHPUnit
/**
* 콘솔 커맨드 테스트
*/
public function test_console_command(): void
{
$this->artisan('inspire')->assertExitCode(0);
}특정 종료 코드로 끝나지 않았음을 검증하려면 assertNotExitCode 메서드를 사용합니다.
$this->artisan('inspire')->assertNotExitCode(1);일반적으로 터미널 커맨드는 성공 시 0, 실패 시 0이 아닌 값을 반환합니다. 이를 좀 더 직관적으로 검증할 수 있도록 assertSuccessful과 assertFailed 메서드도 제공합니다.
$this->artisan('inspire')->assertSuccessful();
$this->artisan('inspire')->assertFailed();입력 / 출력 검증
Laravel은 expectsQuestion 메서드를 통해 콘솔 커맨드의 사용자 입력을 손쉽게 모킹(mock)할 수 있습니다. 또한 expectsOutput으로 출력 내용을, assertExitCode로 종료 코드를 검증할 수 있습니다.
예를 들어 다음과 같은 콘솔 커맨드가 있다고 가정합니다.
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.'.');
});이 커맨드는 아래와 같이 테스트할 수 있습니다.
Pest
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);
});PHPUnit
/**
* 콘솔 커맨드 테스트
*/
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);
}Laravel Prompts의 search 또는 multisearch 기능을 사용하는 경우, expectsSearch 어서션으로 사용자 입력, 검색 결과, 선택값을 모킹할 수 있습니다.
Pest
test('console command', function () {
$this->artisan('example')
->expectsSearch('What is your name?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
});PHPUnit
/**
* 콘솔 커맨드 테스트
*/
public function test_console_command(): void
{
$this->artisan('example')
->expectsSearch('What is your name?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
}커맨드가 아무것도 출력하지 않는지 확인하려면 doesntExpectOutput 메서드를 인수 없이 사용합니다.
Pest
test('console command', function () {
$this->artisan('example')
->doesntExpectOutput()
->assertExitCode(0);
});PHPUnit
/**
* 콘솔 커맨드 테스트
*/
public function test_console_command(): void
{
$this->artisan('example')
->doesntExpectOutput()
->assertExitCode(0);
}출력 전체가 아닌 일부 문자열의 포함 여부를 검증하려면 expectsOutputToContain과 doesntExpectOutputToContain 메서드를 활용하세요.
Pest
test('console command', function () {
$this->artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
});PHPUnit
/**
* 콘솔 커맨드 테스트
*/
public function test_console_command(): void
{
$this->artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
}확인(Confirmation) 검증
"yes" / "no" 형태의 확인 입력이 필요한 커맨드를 테스트할 때는 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\CommandStarting과 Illuminate\Console\Events\CommandFinished 이벤트가 발생하지 않습니다. 특정 테스트 클래스에서 이 이벤트를 활성화하려면 Illuminate\Foundation\Testing\WithConsoleEvents 트레이트를 추가하면 됩니다.
Pest
<?php
use Illuminate\Foundation\Testing\WithConsoleEvents;
pest()->use(WithConsoleEvents::class);
// ...PHPUnit
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\WithConsoleEvents;
use Tests\TestCase;
class ConsoleEventTest extends TestCase
{
use WithConsoleEvents;
// ...
}