Quick start
Create and Running Tests
- Introduction
- Laravel was born with testing in its DNA. In fact, Laravel supports PHPUnit for testing by default, and has a phpunit.xml file configured for your application. The framework also provides some convenient helper functions so that you can test your application more intuitively. By default, your application's
directory contains two subdirectories: Feature and
Unittest example file is provided in both the
Feature and Unit
directories. After installing a new Laravel application, run the phpunit
command from the command line to run the tests.
When using You can create other necessary test environment configurations at will. In addition, you can also create a make:testEnvironment
phpunit
for testing, Laravel will automatically set the environment to testing based on the environment variables set in the
phpunit.xml file
, and store the Session and cache in the form of array
, which means that no Session or cache data will be persisted during testing. testing
Environment variables can be modified in the phpunit.xml
file, but before running the test, make sure to clear the cache of configuration information using the config:clear
Artisan command ! .env.testing
file in the root directory of your project to run unit tests, or use the command with --env=testing# When using the Artisan command with the ## option, the variables in the
.env file will be overwritten by this file.
Use case:
// 在 Feature 目录下创建一个测试类...php artisan make:test UserTest// 在 Unit 目录下创建一个测试类...php artisan make:test UserTest --unit
phpunit command on the terminal:
<?php namespace Tests\Unit; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; class ExampleTest extends TestCase{ /** * 一个基本的测试示例。 * * @return void */ public function testBasicTest() { $this->assertTrue(true); } }
{note} If you want to define your ownLearnKu.comsetUp## in your test class # /
This article was first published on thetearDown
method, please make sure to call theparent::setUp()
/parent::tearDown()
method in the parent class.