본문 바로가기

콘솔 테스트

번역일: 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이 아닌 값을 반환합니다. 이를 더 직관적으로 표현하고 싶다면 assertSuccessfulassertFailed를 활용하세요.

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

입력 / 출력 단언

Laravel은 expectsQuestion 메서드를 사용해 콘솔 명령어의 사용자 입력을 손쉽게 모킹(mock)할 수 있습니다. 또한 expectsOutput으로 출력 내용을, assertExitCode로 종료 코드를 검증할 수 있습니다.

예를 들어 아래와 같은 명령어가 있다고 가정합니다.

Artisan::command('question', function () { $name = $this->ask('이름이 무엇인가요?'); $language = $this->choice('선호하는 언어를 선택하세요.', [ 'PHP', 'Ruby', 'Python', ]); $this->line('이름: '.$name.', 선호 언어: '.$language.'.'); });

이 명령어는 다음과 같이 테스트할 수 있습니다.

Pest

test('console command', function () { $this->artisan('question') ->expectsQuestion('이름이 무엇인가요?', '홍길동') ->expectsQuestion('선호하는 언어를 선택하세요.', 'PHP') ->expectsOutput('이름: 홍길동, 선호 언어: PHP.') ->doesntExpectOutput('이름: 홍길동, 선호 언어: Ruby.') ->assertExitCode(0); });

PHPUnit

/** * 콘솔 명령어 테스트 */ public function test_console_command(): void { $this->artisan('question') ->expectsQuestion('이름이 무엇인가요?', '홍길동') ->expectsQuestion('선호하는 언어를 선택하세요.', 'PHP') ->expectsOutput('이름: 홍길동, 선호 언어: PHP.') ->doesntExpectOutput('이름: 홍길동, 선호 언어: Ruby.') ->assertExitCode(0); }

Laravel Promptssearch 또는 multisearch 기능을 사용하는 명령어라면, expectsSearch로 사용자 입력, 검색 결과, 최종 선택값을 모킹할 수 있습니다.

Pest

test('console command', function () { $this->artisan('example') ->expectsSearch('이름이 무엇인가요?', search: '홍', answers: [ '홍길동', '홍경래', '홍준표' ], answer: '홍길동') ->assertExitCode(0); });

PHPUnit

/** * 콘솔 명령어 테스트 */ public function test_console_command(): void { $this->artisan('example') ->expectsSearch('이름이 무엇인가요?', search: '홍', answers: [ '홍길동', '홍경래', '홍준표' ], answer: '홍길동') ->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); }

출력 결과의 일부만 검증하고 싶을 때는 expectsOutputToContaindoesntExpectOutputToContain을 사용하세요.

Pest

test('console command', function () { $this->artisan('example') ->expectsOutputToContain('홍길동') ->assertExitCode(0); });

PHPUnit

/** * 콘솔 명령어 테스트 */ public function test_console_command(): void { $this->artisan('example') ->expectsOutputToContain('홍길동') ->assertExitCode(0); }

확인(Confirmation) 단언

"yes" 또는 "no" 형태의 확인을 요구하는 명령어는 expectsConfirmation 메서드로 테스트할 수 있습니다.

$this->artisan('module:import') ->expectsConfirmation('정말 이 명령어를 실행하시겠습니까?', 'no') ->assertExitCode(1);

테이블 출력 단언

명령어가 Artisan의 table 메서드로 테이블 형태의 데이터를 출력하는 경우, 전체 출력을 문자열로 검증하는 것은 번거로울 수 있습니다. 이때는 expectsTable 메서드를 사용하세요. 첫 번째 인수로 헤더 배열을, 두 번째 인수로 데이터 배열을 전달합니다.

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

콘솔 이벤트

기본적으로 테스트 실행 중에는 Illuminate\Console\Events\CommandStartingIlluminate\Console\Events\CommandFinished 이벤트가 발생하지 않습니다. 특정 테스트 클래스에서 이 이벤트를 활성화하려면 Illuminate\Foundation\Testing\WithConsoleEvents 트레이트를 추가하면 됩니다.

Pest

<?php use Illuminate\Foundation\Testing\WithConsoleEvents; uses(WithConsoleEvents::class); // ...

PHPUnit

<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\WithConsoleEvents; use Tests\TestCase; class ConsoleEventTest extends TestCase { use WithConsoleEvents; // ... }

이 문서는 Laravel 공식 문서(MIT)를 한국 개발자를 위해 번역·재구성한 것입니다.

번역일: 2026년 6월 20일