PhpStorm 配置 PHPUnit
配置說明
全域性安裝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 配置檔案
&n