Writing testable code is an important part of building a robust, maintainable, and agile project. Support for PHP's most widely used testing framework, PHPUnit, is built right into Laravel. PHPUnit is configured using the phpunit.xml
file, which resides in the root directory of every new Laravel application.
The tests
directory, also in the root directory, contains the individual testing files which hold the logic for testing each portion of your application. Of course, it is your responsibility as a developer to write these tests as you build your application, but Laravel includes an example file, ExampleTest.php
, to get you going.
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5');
}
}
In the testBasicExample()
method, we visit the site's index page and make sure we see the text Laravel 5
somewhere on that page. If the text is not present, the test will fail and generate an error.