Windows10 + IntelliJ IDEA 2017.3.2 + wamp2e + Yii + PHPunit 搭建测试环境

一、环境

系统: windows10

WampServer:  wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-32b.exe

IDE:  IntelliJ IDEA 2017.3.2

PHP: 7.1.15

Intellij IDEA 如何支持php请参考,IntellIJ IDEA 配置 Vue 支持

 

二、PHPunit配置

1.下载PHPunit.phar:

Linux:

wget -O phpunit https://phar.phpunit.de/phpunit-7.phar
chmod +x phpunit

➜ ./phpunit --version

 

Window:

下载phpunit-7.phar到你所在的php.exe目录下, 并新建外包覆批处理脚本 phpunit.cmd 文件,内容如下:

@ECHO OFF
php %~dp0phpunit-7.phar %*

对于 Cygwin 或 MingW32 (例如 TortoiseGit) shell 环境, 取而代之的是,把文件保存为 phpunit (没有 .phar 扩展名),然后用 chmod 775 phpunit 将其设为可执行。参考:安装 PHPUnit

 

设置环境变量:

 

执行: phpunit --version

 

 PS: https://phpunit.de/getting-started/phpunit-7.html

 

三、新建测试项目

1.以Yii 项目为例,在protected目录下创建一个测试目录tests。在tests目录下创建bootstrap.php文件。

<?php
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);
require_once(__DIR__ . '/../../../yiisoft/yii2/Yii.php');
$config = require '../config/web.php';
(new Application($config));

 

bootstrap文件是用来加载测试用例的运行时用的,各个项目有各个项目的加载配置,按具体项目而定。

 

2.编写测试用例

<?php

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase{

    public function testEmpty(){
        $stack = [];

        $this->assertEmpty($stack);

        return $stack;
    }
}

 

在测试用例中需要注意的是命名空间还是需要指定的。参考:编写 PHPUnit 测试

 

 

三、配置PHPunit

 

1. 勾选 Default bootstrap file ,并选择你上文新建的 bootstrap.php 文件。

2. 在你所在的 MyTest.php 文件 testEmpty 方法体中, 点击右键选择执行 。

 

 

如果你想手动执行:

D:\IdeaProjects>phpunit --bootstrap D:\IdeaProjects\test\protected\tests\bootstrap.php MyTest D:\IdeaProjects\test\protected\tests\examples\MyTest.php
PHPUnit 7.1.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 367 ms, Memory: 10.00MB

OK (1 test, 1 assertion)

 

 

 

PS:

http://www.yii-china.com/post/detail/460.html

http://tangl163.iteye.com/blog/2288538

https://phpunit.readthedocs.io/zh_CN/latest/installation.html

https://phpunit.readthedocs.io/zh_CN/latest/index.html

https://phpunit.de/getting-started/phpunit-7.html

 

posted @ 2018-05-08 11:57  phpdragon  阅读(720)  评论(0编辑  收藏  举报