Running Unit Tests With Artisan's Test Runner

In the 7.x branch, Laravel added an artisan test
command which acts as a wrapper for PHPUnit. It provides information about the tests as they’re running and stops on the first failure unlike PHPUnit’s default behavior of running all the tests and showing all the failures. It also provides a nice output when the tests fail.
The following article will provide a brief overview of how we can use it and quickly compare it to phpunit
.
To start out let’s look at what happens when we run phpunit
for our project.
vagrant@ubuntu-xenial:/var/www$ ./vendor/bin/phpunit
PHPUnit 8.5.5 by Sebastian Bergmann and contributors.
...............I 16 / 16 (100%)
Time: 1.46 seconds, Memory: 30.00 MB
OK, but incomplete, skipped, or risky tests!
Tests: 16, Assertions: 21, Incomplete: 1.
Now let’s look at the same tests if we run php artisan test
.
vagrant@ubuntu-xenial:/var/www$ php artisan test
PASS Tests\Unit\ExampleTest
✓ basic test
PASS Tests\Unit\ProjectTest
✓ null end date indicates not competed
✓ we can override name
✓ we can get multiple projects
✓ non null end date indicates competed
✓ we can get multiple completed projects
PASS Tests\Unit\TaskTest
✓ example
PASS Tests\Feature\ExampleTest
✓ basic test
PASS Tests\Feature\ProjectTest
✓ example
PASS Tests\Feature\Recipe\PlanTest
✓ create
WARN Tests\Feature\RecipeTest
✓ can access index method
✓ can access create method
✓ can submit with no data
✓ can submit with correct data
✓ can view specific recipe
i can make specific recipe
Tests: 1 incompleted, 15 passed
Time: 1.39s
The nice part of this feature is when we have a failing test. When running our test suite with a failing test we’ll get the following output from phpunit
.
vagrant@ubuntu-xenial:/var/www$ ./vendor/bin/phpunit
PHPUnit 8.5.5 by Sebastian Bergmann and contributors.
........F......I 16 / 16 (100%)
Time: 1.86 seconds, Memory: 30.00 MB
There was 1 failure:
1) Tests\Feature\ProjectTest::testExample
Expected status code 200 but received 302.
Failed asserting that 200 is identical to 302.
/var/www/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:185
/var/www/tests/Feature/ProjectTest.php:20
FAILURES!
Tests: 16, Assertions: 21, Failures: 1, Incomplete: 1.
However, if we run the test using php artisan test
we get the following nicer output.
vagrant@ubuntu-xenial:/var/www$ php artisan test
FAIL Tests\Feature\ProjectTest
✕ example
Tests: 1 failed, 8 passed, 7 pending
Expected status code 200 but received 302. Failed asserting that 200 is identical to 302.
at tests/Feature/ProjectTest.php:20
16| public function testExample()
17| {
18| $response = $this->get('/projects');
19|
> 20| $response->assertStatus(200);
21| }
22| }
23|
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows