PhpStorm 配置 PHPUnit
配置说明全局安装phpunit代码composer global require phpunit/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 CLiLanguages & Frameworks > PHP 点击 + 新增一个 PHP 解释器
配置 phpunit.phar 路径和 phpunit.xml 路径Languages & Frameworks > PHP > Test Frameworks 例如我的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 PHPUnitFrameworkTestCase; 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会根据xml配置文件进行测试。 phpstorm方式命令行方式命令行下进入当前项目执行
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‘; ReferencePHPUnit 手册 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |