配置说明
全局安装PHPunit代码
composer global require PHPunit/PHPunit
该代码会自动保存在 /User/你的用户名/.composer/vendor/PHPunit
全局安装PHPunit命令脚本
从上一步安装结果可以得知当前环境PHP版本可兼容的PHPunit的版本,我这里的PHP是5.6的,最大可兼容PHPunit5.7
wget https://phar.PHPunit.de/PHPunit-5.7.phar chmod +x PHPunit-5.7.phar sudo mv PHPunit-5.7.phar /usr/local/bin/PHPunit PHPunit --version
创建 PHPunit.xml
放在你的项目根目录,这个文件是 PHPunit 会默认读取的一个配置文件
<PHPunit bootstrap="vendor/autoload.PHP"> <testsuites> <testsuite name="service"> <directory>tests</directory> </testsuite> </testsuites> </PHPunit>
配置 PHPStorm 的 PHP CLi
Languages & Frameworks > PHP
点击 + 新增一个 PHP 解释器
- 配置 PHP 执行程序
- 点击那个 同步的小图标,如果看到 successfully 就说明配置有效
配置 PHPunit.phar 路径和 PHPunit.xml 路径
Languages & Frameworks > PHP > Test Frameworks
点击 + 新增一个 PHPUnit Local
例如我的PHPunit本地的路径为/usr/local/bin/PHPunit
配置单元测试类提示
Languages & Frameworks > PHP
如我的PHPunit包本地的路径为/Users/maritni/.composer/vendor/PHPunit
单元测试编写
<?PHP /** * Description:数组压入和弹出测试用例 * Created by Martini * DateTime: 2019-06-29 16:09 */ use PHPUnit\Framework\TestCase; class DemoTest extends TestCase { public function testPushAndPop() { $stack = []; $this->assertEquals(0,count($stack)); array_push($stack,‘foo‘); $this->assertEquals(‘foo‘,$stack[count($stack)-1]); $this->assertEquals(1,count($stack)); $this->assertEquals(‘foo‘,array_pop($stack)); $this->assertEquals(0,count($stack)); } }
执行单元测试
执行单个文件单元测试
方式1: PHPstorm方式,当前测试类右键Run即可
方式2:命令行方式,进入项目目录执行
PHPunit Creational/SimpleFactory/Tests/DemoTest.PHP
执行全局单元测试
全局单元测试,实际上PHPunit会根据xml配置文件进行测试。
PHPstorm方式
命令行方式
命令行下进入当前项目执行
PHPunit
XML 文件用法
PHPUnit 的目标之一是测试应当可组合:我们希望能将任意数量的测试以任意组合方式运行,例如,整个项目的所有测试,或者项目中的某个组件内的所有类的测试,又或者仅仅某单个类的测试。
PHPUnit 支持好几种不同的方式来组织测试以及将它们编排组合成测试套件。
PHPUnit的 XML 配置文件可以用于编排测试套件。Example1,“用 XML 配置来编排测试套件”展示了一个最小化的 PHPunit.xml 例子,它将在递归遍历 tests 时添加所有在 *Test.PHP 文件中找到的 *Test 类。
Example1.最小化xml文件
<PHPunit bootstrap="vendor/autoload.PHP"> <testsuites> <testsuite name="money"> <directory>tests</directory> </testsuite> </testsuites> </PHPunit>
如果 PHPunit.xml 或 PHPunit.xml.dist (按此顺序)存在于当前工作目录并且未使用 --configuration,将自动从此文件中读取配置。
可以明确指定测试的执行顺序:
Example 2. 用 XML 配置来编排测试套件
<PHPunit bootstrap="vendor/autoload.PHP"> <testsuites> <testsuite name="money"> <file>tests/IntlFormatterTest.PHP</file> <file>tests/MoneyTest.PHP</file> <file>tests/CurrencyTest.PHP</file> </testsuite> </testsuites> </PHPunit>
xml实例1
<?xml version="1.0" encoding="UTF-8"?> <PHPunit bootstrap="./vendor/autoload.PHP" colors="true"> <testsuites> <testsuite name="Design Patterns"> <directory suffix="Test.PHP">Behavioral/*/Tests</directory> <directory suffix="Test.PHP">Creational/*/Tests</directory> <directory suffix="Test.PHP">More/*/Tests</directory> <directory suffix="Test.PHP">Structural/*/Tests</directory> </testsuite> </testsuites> <filter> <blacklist> <directory>./vendor</directory> </blacklist> </filter> </PHPunit>
xml实例2
<PHPunit bootstrap="./booten.PHP"> <testsuite name="actionsuitetest"> <directory suffix=".PHP">action</directory> <file>HuiyuanZhanghuOrder.PHP</file> <exclude>/action/HuiyuanJifenTest.PHP</exclude> </testsuite> <testsuite name="modelsuitetest"> <directory suffix=".PHP">model</directory> </testsuite> <testsuite name="htmlsuitetest"> <directory suffix=".PHP">html</directory> </testsuite> <!-- 代码覆盖率 --> <!-- 覆盖率的测试文件,blacklist 黑名单(不需要统计覆盖率的文件),whitelist 白名单(统计覆盖率的测试文件) 当黑名单与白名单文件重复时,白名单起作用 --> <filter> <blacklist> <directory suffix=".PHP">action</directory> <file>ArrayTest.PHP</file> </blacklist> <whitelist addUncoveredFilesFromWhitelist="true"> <directory suffix=".PHP">action</directory> <directory suffix=".PHP">model</directory> <directory suffix=".PHP">html</directory> <file>ArrayTest.PHP</file> <exclude> <directory suffix=".PHP">action/lib</directory> <directory suffix=".PHP">model</directory> <file>action/lib/Loginxxx.PHP</file> </exclude> </whitelist> </filter> <!--代码覆盖率报告,可以生成很多类型报告,有html(coverage-html),xml(coverage-clover),txt,json 等等 <log type="coverage-PHP" target="/tmp/coverage.serialized"/> <log type="coverage-text" target="PHP://stdout" showUncoveredFiles="false"/> <log type="json" target="/tmp/logfile.json"/> <log type="tap" target="/tmp/logfile.tap"/> <log type="junit" target="/tmp/logfile.xml" logIncompleteSkipped="false"/> <log type="testdox-html" target="/tmp/testdox.html"/> <log type="testdox-text" target="/tmp/testdox.txt"/> --> <logging> <!-- target(report/html) 生成html 文件的目录--> <log type="coverage-html" target="report/html" charset="UTF-8" yui="true" highlight="false" lowUpperBound="35" highLowerBound="70"/> <!-- target(report/coverage/coverage.xml) 生成xml的文件名--> <log type="coverage-clover" target="report/coverage/coverage.xml"/> </logging> <!-- 代码覆盖率 --> <PHP> <includePath>.</includePath> <ini name="foo" value="bar"/> <const name="foo" value="bar"/> <var name="foo" value="bar"/> <env name="foo" value="bar"/> <post name="foo" value="bar"/> <get name="foo" value="bar"/> <cookie name="foo" value="bar"/> <server name="foo" value="bar"/> <files name="foo" value="bar"/> <request name="foo" value="bar"/> </PHP> </PHPunit>
xml 解释
xml 解释 bootstrap="./vendor/autoload.PHP" 在测试之前加载的的PHP 文件,一般可以做一个初始化工作 <testsuite name="actionsuitetest"> <directory suffix=".PHP">action</directory> <file>Order.PHP</file> </testsuite> 测试套件,如果想测试页面,action,model 可以多加几个测试套件 name: 套件名称 directory :套件测试的目录,目录下一般放测试文件的用例 suffix :测试文件后缀,如果不填写,则默认后缀为*Test.PHP,即PHPunit 默认会执行*Test.PHP 的文件 action:测试目录名 file:可以单独设置测试文件 exclude:排除不需要测试的文件 <filter> 元素及其子元素用于配置代码覆盖率报告所使用的白名单。 blacklist 黑名单(不需要统计覆盖率的文件),whitelist 白名单(统计覆盖率的测试文件) 当黑名单与白名单文件重复时,白名单起作用 <logging> 元素及其 <log> 子元素用于配置测试执行期间的日志记录。 <PHP> <includePath>.</includePath> <ini name="foo" value="bar"/> <const name="foo" value="bar"/> <var name="foo" value="bar"/> <env name="foo" value="bar"/> <post name="foo" value="bar"/> <get name="foo" value="bar"/> <cookie name="foo" value="bar"/> <server name="foo" value="bar"/> <files name="foo" value="bar"/> <request name="foo" value="bar"/> </PHP> 这段xml 可以对应以下PHP 代码 includePath ini_set(‘foo‘,‘bar‘); define(‘foo‘,‘bar‘); $GLOBALS[‘foo‘] = ‘bar‘; $_ENV[‘foo‘] = ‘bar‘; $_POST[‘foo‘] = ‘bar‘; $_GET[‘foo‘] = ‘bar‘; $_COOKIE[‘foo‘] = ‘bar‘; $_SERVER[‘foo‘] = ‘bar‘; $_FILES[‘foo‘] = ‘bar‘; $_REQUEST[‘foo‘] = ‘bar‘;
Reference
PHPUnit 手册
在phpstorm中安装、配置和运行phpunit详细教程
PHP单元测试使用
phpunit5.0中文手册
phpunit XML 配置文件