From what i have learned and read about Laravel is that it is built with testing in mind. Out of the box it supports PHPUnit testing and when you install Laravel a phpunit.xml file is already setup and configured for your new application. At the same time Laravel also ships with a convenient box of helper methods that allow you to expressively test your applications.

Bu default the test directory in a Laravel application contains two directories: Feature and Unit.

  • Unit tests are tests that focus on a very small, portion of your code. In fact, most unit tests probably focus on a single method.
  • Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint.

In a new project with Laravel you will get an ExampleTest.php file, where both feature and unit test directories are presented. When you have installed Laravel either usung Homebrew or Laragun, try rinning phpunit from your command line. This will execute the tests in your application.

The Environment

When running tests in your Laravel application via phpunit, Laravel will automatically set the configuration for your environment to testing. the reason is that the environment variables are defines in the phpunit.xml file.

By default Laravel will automatically configure the session and cache to the array driver, while the test is going on. Due to this no cached data og session will be persisted while you are testing.

If you wan’t, you can define other testing environment configuration values if you need that. The testing environment variables is possible to configure in the phpunit.xml file – remember to clear the configuration cache from the command line using config:clear. It’s a built in command in Artisan, which should be executed before running any tests.

You may also create a .env.testing file in the root of your new project. Doing so will override the .env file, when you run PHPUnit tests or when executing a Artisan command from your command line.

Creating & Running Tests

It’s very easy to get started creating a new test case. Simply use this built in command in Artisan:

// Create a test in the Feature directory...
php artisan make:test YourTest

// Create a test in the Unit directory...
php artisan make:test YourTest --unit

Now we got a test generated, then we can define methods as we normally would do using PHPUnit or any other Unit testing from other languages. In order to run the tests, we can execute phpuni from the command line.


namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
     * A basic test example.
     * @return void
    public function testBasicTest()


This was a short article on how to work with PHPUnit testing in PHP and Laravel. As you can see it’s very easy to get started creating tests and configuring them.

Leave a Reply