Testing: Getting Started

    Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box and a file is already set up for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.

    By default, your application's tests directory contains two directories: Feature and Unit. Unit tests are tests that focus on a very small, isolated 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.

    When running tests via phpunit, Laravel will automatically set the configuration environment to testing because of the environment variables defined in the phpunit.xml file. Laravel also automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.

    To create a new test case, use the make:test Artisan command:

    1. <?php
    2. namespace Tests\Unit;
    3. use Tests\TestCase;
    4. use Illuminate\Foundation\Testing\RefreshDatabase;
    5. {
    6. * A basic test example.
    7. *
    8. * @return void
    9. */
    10. public function testBasicTest()
    11. {
    12. $this->assertTrue(true);
    13. }