0x01. 下载两个 Phar 包:
1wget https://phar.phpunit.de/phpunit.phar 2wget https://phar.phpunit.de/phpunit-skelgen.phar
0x02. 让这两个包可执行
1chmod +x phpunit.phar 2chmod +x phpunit-skelgen.phar
0x03. 移动(或建立软连接)到你的环境变量的目录下
1mv phpunit.phar /usr/local/bin/phpunit 2mv phpunit-skelgen.phar /usr/local/bin/phpunit-skelgen
至此,PHPUnit 以及生成测试的框架,已经部署完毕。简单吧!
0x04. Netbeans 全局配置
1菜单 --> 工具[Tools] --> 选项[Options] --> PHP --> Frameworks & Tools 2选中左侧的 PHPUnit,设置 [PHPUnit Script] 与 [Skeleton Generator Script] 3如果在你的 PATH 环境变量下,点击 [Search] 即可到,如果不在,自己填路径。 4本例中,这两个路径分别为: 5 /usr/local/bin/phpunit 6 /usr/local/bin/phpunit-skelgen 点击确定,退出配置
0x05. NetBeans 项目配置
1在项目名称上右击 --> 属性[Properties] --> 测试[Testing] 2添加测试目录。 3展开左侧 测试[Testing] 树,点击 [PHPUnit] 4勾选 [Use Bootstrap],然后选择启动脚本或点击[Generate]自动生成一个。 5勾选 [Use Bootstrap for Creating New Unit Tests]
如果不勾选 [Use Bootstrap for Creating New Unit Tests],在生成有继承或实现接口的类时,会提示
1Final Error: Interface ... not found in ... 2Final Error: Class ... not found in ...
0x06. 修改 bootstrap.php
自动生成的 bootstrap.php 可能不大符合自己的实际要求,修改一下即可,它的作用一般是自动加载类库。比如你使用一些框架,加载自己的类,则把框架的 autoload.php 文件包含进来,把自己的 autoload.php 包含进来即可。比如:
1<?php 2 3/* 4 * To change this license header, choose License Headers in Project Properties. 5 * To change this template file, choose Tools | Templates 6 * and open the template in the editor. 7 */ 8 9/** 10 * @author 11 */ 12// TODO: check include path 13//ini_set('include_path', ini_get('include_path')); 14 15// put your code here 16require __DIR__ . '../../../my/autoload.php';
0x07. 使用方法
1在代码窗口选中要测试的文件,菜单 --> 工具[Tools] --> 生成测试[Create Tests] 2或在项目窗口,选中要测试的文件,右键 --> 工具[Tools] --> 生成测试[Create Tests]
0x08. 附命令行使用方法
phpunit-skelgen
"/usr/bin/php" "/usr/local/bin/phpunit-skelgen" "--ansi" "generate-test" "--bootstrap=/path/to/bootstrap.php" "Namespace\MyClass" "/path/to/Namespace/MyClass.php" "Namespace\MyClassTest" "/path/to/Namespace/MyClassTest.php"
phpunit
"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-junit" "/tmp/nb-phpunit-log.xml" "--bootstrap" "/path/to/bootstrap.php" "/path/to/netbeans-8.0/php/phpunit/NetBeansSuite.php" "--run=/path/to/test/Namespace/MyClassTest.php"