quick get start


1. Introduction

Larravel is rooted in testing. In fact, PHPUnit based support for testing is out of the box, and phpunit.xml The file has been configured for the application. The framework also provides convenient auxiliary methods to allow you to test applications gracefully.

By default, tests The directory contains two subdirectories: Feature and Unit , which are used for functional testing and unit testing respectively. Unit testing focuses on small, isolated code. In fact, most unit tests may focus on a single method. Functional testing can be used to test code in large blocks, including previous interactions of several components, or even a complete HTTP request.

Feature and Unit The test directory is provided by default ExampleTest.php File. After installing the new Larravel application, simply run it on the command line phpunit You can run the test.

2. Environment

When running the test, Larravel will automatically set the environment variable to testing , this is because phpunit.xml The corresponding settings are already available in. Larave also automatically configures the session and cache drivers as array , which means that the session and cache will not be persisted during testing.

If necessary, you can also define other test environment configuration values. testing Environment variables can be set in phpunit.xml File, but you need to use Artisan commands before running tests config:clear Clear the configuration cache.

3. Create&Run Test

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

 //Create a test class in the Feature directory php artisan make:test UserTest //Create a test class in the Unit directory php artisan make:test UserTest --unit

After creating the test class, you can define the test method just like using PHPUnit. To run the test, just execute it on the terminal phpunit Command:

 <? php namespace Tests\Unit; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ExampleTest extends TestCase { /** *Simple test example * * @return void */ public function testBasicTest() { $this->assertTrue(true); } }
Note: If you define your own setUp Method, ensure that the parent::setUp()

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: serialize

>>Next: HTTP test