How To Test Artisan Commands in Laravel 5
I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.
Anyone an idea how to write it?
laravel unit-testing laravel-5.1
add a comment |
I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.
Anyone an idea how to write it?
laravel unit-testing laravel-5.1
1
laravel.com/docs/master/artisan#calling-commands-via-code ?
– andrewtweber
Nov 9 '15 at 21:01
stackoverflow.com/questions/34814954/…
– bernie
Jan 15 '16 at 15:55
This was a better approach for me: stackoverflow.com/a/41122816/470749
– Ryan
Jun 23 '17 at 16:13
add a comment |
I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.
Anyone an idea how to write it?
laravel unit-testing laravel-5.1
I build an Artisan Command to receive data from a socket, and I want to write a unit-testing for this command but I'm not sure how to write such a test.
Anyone an idea how to write it?
laravel unit-testing laravel-5.1
laravel unit-testing laravel-5.1
edited Aug 26 '16 at 6:17
mnv
5,57253152
5,57253152
asked Nov 9 '15 at 14:51
Thomas.YuThomas.Yu
4431822
4431822
1
laravel.com/docs/master/artisan#calling-commands-via-code ?
– andrewtweber
Nov 9 '15 at 21:01
stackoverflow.com/questions/34814954/…
– bernie
Jan 15 '16 at 15:55
This was a better approach for me: stackoverflow.com/a/41122816/470749
– Ryan
Jun 23 '17 at 16:13
add a comment |
1
laravel.com/docs/master/artisan#calling-commands-via-code ?
– andrewtweber
Nov 9 '15 at 21:01
stackoverflow.com/questions/34814954/…
– bernie
Jan 15 '16 at 15:55
This was a better approach for me: stackoverflow.com/a/41122816/470749
– Ryan
Jun 23 '17 at 16:13
1
1
laravel.com/docs/master/artisan#calling-commands-via-code ?
– andrewtweber
Nov 9 '15 at 21:01
laravel.com/docs/master/artisan#calling-commands-via-code ?
– andrewtweber
Nov 9 '15 at 21:01
stackoverflow.com/questions/34814954/…
– bernie
Jan 15 '16 at 15:55
stackoverflow.com/questions/34814954/…
– bernie
Jan 15 '16 at 15:55
This was a better approach for me: stackoverflow.com/a/41122816/470749
– Ryan
Jun 23 '17 at 16:13
This was a better approach for me: stackoverflow.com/a/41122816/470749
– Ryan
Jun 23 '17 at 16:13
add a comment |
2 Answers
2
active
oldest
votes
It is much easier now:
<?php
class YourCommandTest extends TestCase
public function testExample()
$this->artisan('command', ['param' => 'value']);
1
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
That will depend entirely on what you want to happen. There would be a setup before the$this->artisan()
call and the assertions after it.
– André Castelo
Jun 7 '18 at 20:27
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
add a comment |
Example of test
<?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);
1
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
1
I run tests with option--coverage-html
to generate coverage report:phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.
– mnv
Jun 25 '16 at 20:25
1
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings inphpunit.xml
.
– mnv
Jun 26 '16 at 12:51
1
I have not optionprocessUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
1
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33611788%2fhow-to-test-artisan-commands-in-laravel-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is much easier now:
<?php
class YourCommandTest extends TestCase
public function testExample()
$this->artisan('command', ['param' => 'value']);
1
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
That will depend entirely on what you want to happen. There would be a setup before the$this->artisan()
call and the assertions after it.
– André Castelo
Jun 7 '18 at 20:27
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
add a comment |
It is much easier now:
<?php
class YourCommandTest extends TestCase
public function testExample()
$this->artisan('command', ['param' => 'value']);
1
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
That will depend entirely on what you want to happen. There would be a setup before the$this->artisan()
call and the assertions after it.
– André Castelo
Jun 7 '18 at 20:27
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
add a comment |
It is much easier now:
<?php
class YourCommandTest extends TestCase
public function testExample()
$this->artisan('command', ['param' => 'value']);
It is much easier now:
<?php
class YourCommandTest extends TestCase
public function testExample()
$this->artisan('command', ['param' => 'value']);
answered Aug 8 '17 at 11:50
Roma RushRoma Rush
4,0531914
4,0531914
1
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
That will depend entirely on what you want to happen. There would be a setup before the$this->artisan()
call and the assertions after it.
– André Castelo
Jun 7 '18 at 20:27
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
add a comment |
1
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
That will depend entirely on what you want to happen. There would be a setup before the$this->artisan()
call and the assertions after it.
– André Castelo
Jun 7 '18 at 20:27
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
1
1
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
Where is your assertion ? What do you assert for ?
– Mkey
Mar 8 '18 at 11:57
That will depend entirely on what you want to happen. There would be a setup before the
$this->artisan()
call and the assertions after it.– André Castelo
Jun 7 '18 at 20:27
That will depend entirely on what you want to happen. There would be a setup before the
$this->artisan()
call and the assertions after it.– André Castelo
Jun 7 '18 at 20:27
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
@Mkey My use case was to create X DB records using factories before $this->artisan. Then after I assert X jobs were queued.
– Carlton
Jul 13 '18 at 12:49
add a comment |
Example of test
<?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);
1
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
1
I run tests with option--coverage-html
to generate coverage report:phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.
– mnv
Jun 25 '16 at 20:25
1
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings inphpunit.xml
.
– mnv
Jun 26 '16 at 12:51
1
I have not optionprocessUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
1
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
|
show 4 more comments
Example of test
<?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);
1
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
1
I run tests with option--coverage-html
to generate coverage report:phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.
– mnv
Jun 25 '16 at 20:25
1
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings inphpunit.xml
.
– mnv
Jun 26 '16 at 12:51
1
I have not optionprocessUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
1
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
|
show 4 more comments
Example of test
<?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);
Example of test
<?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);
edited Nov 15 '18 at 16:10
William Desportes
10238
10238
answered May 26 '16 at 19:39
mnvmnv
5,57253152
5,57253152
1
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
1
I run tests with option--coverage-html
to generate coverage report:phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.
– mnv
Jun 25 '16 at 20:25
1
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings inphpunit.xml
.
– mnv
Jun 26 '16 at 12:51
1
I have not optionprocessUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
1
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
|
show 4 more comments
1
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
1
I run tests with option--coverage-html
to generate coverage report:phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.
– mnv
Jun 25 '16 at 20:25
1
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings inphpunit.xml
.
– mnv
Jun 26 '16 at 12:51
1
I have not optionprocessUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
1
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
1
1
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
Found this approach simple and useful for acceptance testing. However, it will not register code coverage for the command itself.
– alariva
Jun 25 '16 at 19:12
1
1
I run tests with option
--coverage-html
to generate coverage report: phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.– mnv
Jun 25 '16 at 20:25
I run tests with option
--coverage-html
to generate coverage report: phpunit --coverage-html coverage_path
. And I see coverage of methods what called inside command.– mnv
Jun 25 '16 at 20:25
1
1
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in
phpunit.xml
.– mnv
Jun 26 '16 at 12:51
@alariva, we have identical versions of phpunit and laravel. I think, problem may be with settings in
phpunit.xml
.– mnv
Jun 26 '16 at 12:51
1
1
I have not option
processUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
I have not option
processUncoveredFilesFromWhitelist="true"
– mnv
Jun 26 '16 at 19:58
1
1
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
I've noteced some work pending in my test case. But I believe the approach works just fine as you say. Thanks for hinting. I will update once I recha to cover those missing lines.
– alariva
Jun 27 '16 at 1:01
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33611788%2fhow-to-test-artisan-commands-in-laravel-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
laravel.com/docs/master/artisan#calling-commands-via-code ?
– andrewtweber
Nov 9 '15 at 21:01
stackoverflow.com/questions/34814954/…
– bernie
Jan 15 '16 at 15:55
This was a better approach for me: stackoverflow.com/a/41122816/470749
– Ryan
Jun 23 '17 at 16:13