Laravel Logo

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|