Use PHPUnit under Yii2

One, install PHPUnit

1. Install under linux, install
globally

wget https://phar.phpunit.de/phpunit-6.2.4.phar
chmod +x phpunit-6.2.4.phar
sudo mv phpunit-6.2.4.phar /usr/local/bin/phpunit
phpunit --version

Or directly call the phar file to run

wget https://phar.phpunit.de/phpunit-6.2.4.phar
php phpunit-6.2.4.phar --version

2. Installation under Windows After
downloading the phpunit-6.2.4.phar file, place it in the directory you specify. Under the cmd command line, run the following command in the directory where the file is located

echo @php "%~dp0phpunit-6.2.4.pharr"  %* > phpunit.cmd

Then add the directory where phpuni.cmd is located to the environment variable, you can run phpunit --version to view the version

Two, execute PHPUnit

I created the test directory in the root directory of the Yii project, and I put all the phpunit files in this directory. The tets directory
Insert picture description here
is the unit test file under the following code. We can execute the unit test directly by running the following command in the root directory of the project

D:\www\wln>phpunit test\code UserTest

Three, configuration

If UserTest is just a simple test, we can follow the above steps. However, we need to use various classes of the Yii framework for our tests. Before running UserTest, we need to load the various plug-ins and classes of the Yii2 framework first. Pictured aboveautoload.phpThat's what it does. Let's look at the content in autoloa.php

<?php
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);

include_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../vendor/yiisoft/yii2/Yii.php';

//composer下的自动加载类声明自定义的命名空间对应的真是路径
/*$classLoader = new \Composer\Autoload\ClassLoader();
$classLoader->addPsr4("app\\", __DIR__.'/../appapi/', true);
$classLoader->addPsr4("common\\", __DIR__.'/../common/', true);
$classLoader->register();*/

//这段代码是声明自定义的命名空间所对应的真是路径,是Yii2框架下的声明。
//他的作用与上面注释掉的代码是相同的
require __DIR__ . '/../common/config/bootstrap.php';
require __DIR__ . '/../appapi/common/config/bootstrap.php';

$config = \common\library\ArrayHelper::merge(
    require(__DIR__ . '/../common/config/main.php'),
    require(__DIR__ . '/../appapi/common/config/main.php')
);
(new \yii\web\Application($config));

//Yii::setAlias('@test', __DIR__);

We see that the content of this file is basically the same as the entry file index.php of Yii2. The only difference is that the final new object is not executed. run()Method After
finishing the autoload.php file, we execute the following command

D:\www\wln>phpunit --bootstrap test\code\autoload.php  test\code UserTest

In UserTest.php, we can freely use Yii project data and other related components

But in this way, we need to specify bootstrap every time we run the command is very troublesome. At this time, we can create a configuration file for phpunit and specify the path of the bootstrap file in the configuration file, so there is no need to enter the path of the bootstrap file every time the command is executed. Configuration filephpunit.xmlThe content is as follows

<?xml version="1.0" encoding="UTF-8"?>
 <!-- 展示了一个最小化的 phpunit.xml 例子,它将在递归遍历 tests 时添加所有在 *Test.php 文件中找到的 *Test 类-->
<phpunit bootstrap="/test/autoload.php">
    <testsuites>
        <testsuite name="mytest">
            <directory>/test/code</directory>
        </testsuite>
    </testsuites>
</phpunit>
<!--#指定文件测试顺序-->
<phpunit bootstrap="/test/autoload.php">
    <testsuites>
        <testsuite name="mytest">
            <file>test/code/HelloTest.php</file>
            <file>test/code/MoneyTest.php</file>
            <file>test/code/SiteTest.php</file>
        </testsuite>
    </testsuites>
</phpunit>

We put the file in the root directory, so when we execute the phpunit command, it must be in the root directory so that PHPunit will automatically load the configuration file.
The above is the basic process and configuration of using phpunit under the Yii project.

Guess you like

Origin blog.csdn.net/u012830303/article/details/103661999