Thinkphp5 and unit test phpunit


Thinkphp5.0 is the first to support composer in TP development. TP has its own loading mechanism, and composer has its own loading mechanism.

The official think testing component can be installed through composer require topthink / think testing 1 *. But I am not very satisfied with this test component because each test is simulated as an HTTP request. This means that for some specific functions, they have to be encapsulated or routed in the controller, and the controller has to control that its production environment cannot be opened to the outside world. Thinkphp5.0 is the first to support composer in TP development. TP has its own loading mechanism, and composer has its own loading mechanism.

PHPUnit   Is an open source software developed with PHP programming language, is a unit testing framework. Phpunit was created by Sebastian Bergmann and originated from Sunit of Kent Beck. It is one of the frameworks of xUnit family.

Unit testing is the process of testing individual code objects, such as functions, classes and methods. Unit testing can use any written test code, or use some existing testing frameworks, such as JUnit, phpunit or cantata + +. Unit testing framework provides a series of common and useful functions to help people write automatic testing units, such as checking whether an actual value conforms to our expected value assertion. Unit testing frameworks often include reports on each test, as well as the code coverage you have covered.

In a word, using   phpunit   Automatic testing will make your code more robust and reduce the cost of later maintenance. It is also a relatively standard specification. Now the popular PHP framework has unit testing, such as laraval, symfony, yii2, etc. unit testing has become a standard configuration.

In addition, the unit test case controls the test script through the command, rather than accessing the URL through the browser.

Installation of phpunit in the project

use   composer   Method to install phpunit project root directory to execute the following code

composer require phpunit/phpunit 

Auto download adaptation version   I downloaded 4.8.36

Change f to: Wamp, Wamp, WWW, TP5, vendor, bin   Add to the path environment variable (so phpunit takes effect globally)

Phpunit simple usage

1. Single file test

Create the directory tests under the root directory

new file   First test.php, edit as follows:

 *1. Import autoload.php file
 *2. Test cases
namespace App\tests;
require_once __DIR__ . '/../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class FirstTest extends TestCase
  public function testTure()
    $stack = [];
    $this->assertEquals(0, count($stack));

Code explanation:

Firsttest is a test class. Firsttest inherits from   Phpunit / framework / testcase test method testture(), the test method must have the public permission, which usually starts with test, or you can choose to annotate it with @ test to table in the test method, similar to   assertEquals()   This assertion method is used to match the actual value with the expected value, so as to judge whether the method is correct

Command line execution:
Test directory   > phpunit FirstTest       Command test file naming

In project approach

The controller in TP5 project   F: A simple method is written in the folder of the following folder: Wamp, Wamp, WWW, TP5, application, index and controller

Write an indextest. PHP in tests

namespace App\tests;
require_once __DIR__ . '/../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use app\index\controller\Index;
class IndexTest extends TestCase
  public function testSum()
    $obj = new Index;
    $this->assertEquals(6, $obj->index(2,3));

Results after implementation   success!

If I make a mistake in index. PHP and indextest. PHP

Return results   There will be wrong locations

Other uses

For other usage, please refer to the official website of phpunit China