Quick start


Create and Running Tests

tests

directory contains two subdirectories: Feature and

Unit
. Unit testing is testing of a very small and relatively independent part of your code. In fact, most unit tests are done against a single method. Functional testing is testing of large areas of code, including interactions between multiple objects and even full HTTP requests to JSON endpoints. An
ExampleTest.php

test 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.

Environment

When using 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.

You can create other necessary test environment configurations at will. 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 !

In addition, you can also create a .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.

Define and run a test

You can create a test using the Artisan command

make:test Use case:

// 在 Feature 目录下创建一个测试类...php artisan make:test UserTest// 在 Unit 目录下创建一个测试类...php artisan make:test UserTest --unit

After the test class is generated, you can define the test method as usual using PHPUnit. To run the test just run the

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 own

setUp## in your test class # / tearDown method, please make sure to call the parent::setUp() / parent::tearDown() method in the parent class.

This article was first published on the
LearnKu.com
website.