2015-11-09 7 views
25

私はArtisanコマンドをソケットから受信するようにビルドしますが、このコマンドのユニットテストを書きたいと思いますが、そのようなテストを書く方法はわかりません。LaravelのArtisanコマンドをテストする方法5

誰でもどのようにそれを書くのか?テスト

<?php 

use Illuminate\Foundation\Testing\WithoutMiddleware; 
use Illuminate\Foundation\Testing\DatabaseMigrations; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 

class YourCommandTest extends TestCase 
{ 
    use DatabaseTransactions; 

    public function testExample() 
    { 
     Artisan::call('your_command', [ 
      'command_parameter_1' => 'value1', 
      'command_parameter_2' => 'value2', 
     ]); 

     // If you need result of console output 
     $resultAsText = Artisan::output(); 

     $this->assertTrue(true); 
    } 

} 
+1

http://laravel.com/docs/master/artisan#calling-commands-via-code? – andrewtweber

+0

http://stackoverflow.com/questions/34814954/laravel-5-console-artisan-command-unit-tests – bernie

+0

これは私にとって優れたアプローチでした。https://stackoverflow.com/a/41122816/470749 – Ryan

答えて

28

例今ははるかに簡単です:

<?php 

class YourCommandTest extends TestCase 
{ 

    public function testExample() 
    { 
     $this->artisan('command', ['param' => 'value']); 
    } 

} 
+1

Foundこのアプローチは、受け入れテストのために単純かつ有用である。ただし、コマンド自体のコードカバレッジは登録されません。 – alariva

+1

オプション '--coverage-html'でテストを実行して、カバレッジレポートを生成します:' phpunit --coverage-html coverage_path'。そして、私は内部コマンドを呼び出したメソッドのカバレッジを参照してください。 – mnv

+0

それは面白いです、私は本当になぜ私はそれらをカバーされていない取得できません。ここで[私の質問は、あなたがそれを同じケースを見つける](http://stackoverflow.com/questions/38032285/which-is-an-acceptable-approach-for-testing-commands-in-laravel-5-2- with-phpunit)を使用します。 – alariva

10

+0

あなたの主張はどこですか?あなたは何を主張しますか? – Mkey

関連する問題