我编写了一个Artisan命令,用于从套接字接收数据,现在想为该命令编写单元测试,但不确定如何编写测试。
有没有任何想法可以帮助我编写这个测试?
我编写了一个Artisan命令,用于从套接字接收数据,现在想为该命令编写单元测试,但不确定如何编写测试。
有没有任何想法可以帮助我编写这个测试?
测试示例
<?php
class YourCommandTest extends TestCase
{
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);
}
}
--coverage-html
运行测试以生成覆盖率报告:phpunit --coverage-html coverage_path
。我可以看到命令内调用的方法的覆盖率。 - Nickphpunit.xml
的设置上。 - Nick现在容易多了:
<?php
class YourCommandTest extends TestCase
{
public function testExample()
{
$this->artisan('command', ['param' => 'value']);
}
}
$this->artisan()
调用之前会有一些设置,之后是断言。 - André Castelo$this->artisan('custom:command CA')->expectsOutput('Searching')
。但是我收到了一个错误Call to a member function expectsOutput() on int
。 - commadelimited也许对某些人有用
Laravel 5.7中的Artisan命令测试案例
public function test_console_command()
{
$this->artisan('your_command')
->expectsQuestion('What is your name?', 'Ajay Makwana')
->expectsQuestion('Which language do you program in?', 'PHP')
->expectsOutput('Your name is Ajay Makwana and you program in PHP.')
->assertExitCode(0);
}
https://laravel-news.com/testing-artisan-commands-in-laravel-5-7