配置說明
全局安裝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

單元測試編寫
- Class為Demo的測試類為DemoTest
- 測試類繼承於 PHPUnit\Framework\TestCase
- 測試方法
- 必須為public權限,
- 一般以test開頭,也可以給其加注釋@test來標識
- 在測試方法內,類似於 assertEquals() 這樣的斷言方法用來對實際值與預期值的匹配做出斷言。
<?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 配置文件