安装 wget https://phar.phpunit.de/phpunit.phar chomd +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit phpunit -version <?php use PHPUnit\Framework\TestCase; class StackTest 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)); } } 运行测试 phpunit StackTest.php config.xml: <?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="./booten.php"> <testsuites> <testsuite name="Library"> <directory>library</directory> </testsuite> <testsuite name="XXX_Form"> <file>library/XXX/FormTest.php</file> <directory>library/XXX/Form</directory> </testsuite> <testsuite name="Both"> <directory>library</directory> <file>library/XXX/FormTest.php</file> <directory>library/XXX/Form</directory> </testsuite> </testsuites> </phpunit> 运行测试 phpunit --configuration config.xml --testsuite Library phpunit --configuration config.xml --testsuite XXX_Form phpunit --configuration config.xml --testsuite Both <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"/> --> <!-- 生成log文件--> <logging> <log type="coverage-html" target="report/html" charset="UTF-8" yui="true" highlight="false" lowUpperBound="35" highLowerBound="70"/> <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 解释 bootstrap="./booten.php" 在测试之前加载的的PHP 文件,一般可以做一个初始化工作 <testsuite name="actionsuitetest"> <directory suffix=".php">action</directory> <file>HuiyuanZhanghuOrder.php</file> </testsuite> 测试套件,如果想测试页面,action,model 可以多加几个测试套件 name: 套件名称 directory :套件测试的目录,目录下一般放测试文件的用例 suffix :测试文件后缀,如果不填写,则默认后缀为*Test.php,即phpunit 默认会执行*Test.php 的文件 action:测试目录名 file:可以单独设置测试文件 exclude:排除不需要测试的文件 <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';