PHP單元測試配置與使用方法詳解
本文實例講述了PHP單元測試配置與使用方法。分享給大家供大家參考,具體如下:
php與其他語言不太一樣,單元測試需要自己安裝和配置,相對麻煩一點,不過單元測試對于提高庫的穩(wěn)定性和健壯性還是非常給力的,下面教大家怎么配置PHP單元測試
注意:php需升級到7.1版本以上
配置說明
1.全局安裝phpunit命令腳本
$ wget https://phar.phpunit.de/phpunit-7.0.phar $ chmod +x phpunit-7.0.phar $ sudo mv phpunit-7.0.phar /usr/local/bin/phpunit $ phpunit --version PHPUnit x.y.z by Sebastian Bergmann and contributors.
2.全局安裝安裝phpunit代碼
composer global require phpunit/phpunit
3.創(chuàng)建 phpunit.xml放在你的項目根目錄, 這個文件是 phpunit 會默認讀取的一個配置文件:
<phpunit bootstrap="vendor/autoload.php">
  <testsuites>
    <testsuite name="service">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>
4.配置phpstorm單元phpunit.phar路徑,Languages & Frameworks > PHP > PHPUinit
如我的phpunit本地的路徑為/usr/local/bin/phpunit
5.配置單元測試類提示,Languages & Frameworks > PHP > include path
如我的phpunit包本地的路徑為/Users/chenqionghe/.composer/vendor/phpunit
6.單元測試編寫
1.Class為Demo的測試類為DemoTest
2.測試類繼承于 PHPUnit\Framework\TestCase
3.測試方法
- 必須為public權(quán)限,
 - 一般以test開頭,也可以給其加注釋@test來標識
 - 在測試方法內(nèi),類似于 assertEquals() 這樣的斷言方法用來對實際值與預期值的匹配做出斷言。
 
<?php
use Eoffcn\Utils\Arrays;
use PHPUnit\Framework\TestCase;
/**
 * Array測試用例
 * Class ArraysTest
 */
class ArraysTest extends TestCase
{
  public function testGet()
  {
    $array = [
      1 => [
        'b' => [
          'c' => 'cqh'
        ]
      ],
      2 => [
        'b' => [
          'c' => 'cqh'
        ] ]
    ];
    $this->assertEquals('cqh', Arrays::get($array, '1.b.c'));
  }
}
執(zhí)行單元測試
1.執(zhí)行單個文件單元測試
Phpstorm方式,當前測試類右鍵Run即可
命令行的方式,進行項目目錄執(zhí)行
phpunit tests/ArraysTest.php
2.執(zhí)行全局單元測試
phpstorm方式
命令行方式,命令行下進入當前項目執(zhí)行
phpunit
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP錯誤與異常處理方法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》及《php優(yōu)秀開發(fā)框架總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
上一篇:PHP的圖像處理實例小結(jié)【文字水印、圖片水印、壓縮圖像等】
欄 目:PHP編程
下一篇:Laravel 5.1 框架Blade模板引擎用法實例分析
本文標題:PHP單元測試配置與使用方法詳解
本文地址:http://www.jygsgssxh.com/a1/PHPbiancheng/10992.html
您可能感興趣的文章
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
 - 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
 - 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
 - 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
 - 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
 - 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂碼
 - 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)語句
 - 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)據(jù)函數(shù)
 - 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方案
 - 04-02php如何用導入數(shù)據(jù) php用來導入其他文件的語句
 


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
 - 04-02關(guān)于txt數(shù)據(jù)庫php的信息
 - 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
 - 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
 - 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
 - 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
 - 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
 - 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
 - 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
 - 04-02php如何用導入數(shù)據(jù) php用來導入其他文
 
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 01-10C#中split用法實例總結(jié)
 - 01-10delphi制作wav文件的方法
 


