好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 使用PHPUnit進行單元測試并生成代碼覆蓋率報告的方法

使用PHPUnit進行單元測試并生成代碼覆蓋率報告的方法

熱門標簽:銀川ai電話機器人 浙江外呼電話系統(tǒng)軟件 電梯外呼線路板維修視頻 上海公司外呼系統(tǒng)線路 十堰ai電話機器人效果怎么樣 地圖標注風向標 芒果電銷機器人 安陽自動外呼系統(tǒng)價格是多少 臨沂智能電銷機器人軟件

安裝PHPUnit

使用 Composer 安裝 PHPUnit

#查看composer的全局bin目錄 將其加入系統(tǒng) path 路徑 方便后續(xù)直接運行安裝的命令
composer global config bin-dir --absolute
#全局安裝 phpunit
composer global require --dev phpunit/phpunit
#查看版本
phpunit --version

使用Composer構建你的項目

我們將新建一個unit項目用于演示單元測試的基本工作流

創(chuàng)建項目結構

mkdir unit  cd unit  mkdir app tests reports
#結構如下
./
├── app #存放業(yè)務代碼
├── reports #存放覆蓋率報告
└── tests #存放單元測試

使用Composer構建工程

#一路回車即可
composer init

#注冊命名空間
vi composer.json
...
  "autoload": {
    "psr-4": {
      "App\\": "app/",
      "Tests\\": "tests/"
    }
  }
...
#更新命名空間
composer dump-autoload

#安裝 phpunit 組件庫
composer require --dev phpunit/phpunit

到此我們就完成項目框架的構建,下面開始寫業(yè)務和測試用例。

編寫測試用例

創(chuàng)建文件app/Example.php 這里我為節(jié)省排版就不寫注釋了

?php
namespace App;

class Example
{
  private $msg = "hello world";

  public function getTrue()
  {
    return true;
  }

  public function getFalse()
  {
    return false;
  }

  public function setMsg($value)
  {
    $this->msg = $value;
  }

  public function getMsg()
  {
    return $this->msg;
  }
}

創(chuàng)建相應的測試文件tests/ExampleTest.php

?php
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use App\Example;

class ExampleTest extends BaseTestCase
{
  public function testGetTrue()
  {
    $example = new Example();
    $result = $example->getTrue();
    $this->assertTrue($result);
  }
  
  public function testGetFalse()
  {
    $example = new Example();
    $result = $example->getFalse();
    $this->assertFalse($result);
  }
  
  public function testGetMsg()
  {
    $example = new Example();
    $result = $example->getTrue();
    // $result is world not big_cat
    $this->assertEquals($result, "hello big_cat");
  }
}

執(zhí)行單元測試

[root@localhost unit]# phpunit --bootstrap=vendor/autoload.php \

tests/

PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

..F                                 3 / 3 (100%)

Time: 61 ms, Memory: 4.00MB

There was 1 failure:

1) Tests\ExampleTest::testGetMsg
Failed asserting that 'hello big_cat' matches expected true.

/opt/unit/tests/ExampleTest.php:27
/root/.config/composer/vendor/phpunit/phpunit/src/TextUI/Command.php:195
/root/.config/composer/vendor/phpunit/phpunit/src/TextUI/Command.php:148

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

這是一個非常簡單的測試用例類,可以看到,執(zhí)行了共3個測試用例,共3個斷言,共1個失敗,可以參照PHPUnit手冊學習更多高級用法。

代碼覆蓋率

代碼覆蓋率反應的是測試用例測試對象行,函數(shù)/方法,類/特質的訪問率是多少(PHP_CodeCoverage 尚不支持 Opcode覆蓋率、分支覆蓋率 及 路徑覆蓋率),雖然有很多人認為過分看重覆蓋率是不對的,但我們初入測試還是俗氣的追求一下吧。

測試覆蓋率的檢測對象是我們的業(yè)務代碼,PHPUnit通過檢測我們編寫的測試用例調用了哪些函數(shù),哪些類,哪些方法,每一個控制流程是否都執(zhí)行了一遍來計算覆蓋率。

PHPUnit 的覆蓋率依賴 Xdebug,可以生成多種格式:

--coverage-clover file>  Generate code coverage report in Clover XML format.
--coverage-crap4j file>  Generate code coverage report in Crap4J XML format.
--coverage-html dir>    Generate code coverage report in HTML format.
--coverage-php file>    Export PHP_CodeCoverage object to file.
--coverage-text=file>   Generate code coverage report in text format.
--coverage-xml dir>    Generate code coverage report in PHPUnit XML format.

同時需要使用 --whitelist dir參數(shù)來設定我們需要檢測覆蓋率的業(yè)務代碼路徑,下面演示一下具體操作:

phpunit \

--bootstrap vendor/autoload.php \

--coverage-html=reports/ \

--whitelist app/ \

tests/
#查看覆蓋率報告
cd reports/  php -S 0.0.0.0:8899

這樣我們就對業(yè)務代碼App\Example做單元測試,并且獲得我們單元測試的代碼覆蓋率,現(xiàn)在自然是百分之百,因為我的測試用例已經訪問了App\Example的所有方法,沒有遺漏的,開發(fā)中則能體現(xiàn)出你的測試時用力對業(yè)務代碼測試度的完善性。

基境共享測試數(shù)據(jù)

可能你會發(fā)現(xiàn)我們在每個測試方法中都創(chuàng)建了App\Example對象,在一些場景下是重復勞動,為什么不能只創(chuàng)建一次然后供其他測試方法訪問呢?這需要理解 PHPUnit 執(zhí)行測試用例的工作流程。

我們沒有辦法在不同的測試方法中通過某成員屬性來傳遞數(shù)據(jù),因為每個測試方法的執(zhí)行都是新建一個測試類對象,然后調用相應的測試方法

即測試的執(zhí)行模式并不是

testObj = new ExampleTest();
testObj->testMethod1();
testObj->testMethod2();

而是

testObj1 = new ExampleTest();
testObj1->testMethod1();

testObj2 = new ExampleTest();
testObj2->testMethod2();

所以testMethod1()修改的屬性狀態(tài)無法傳遞給 testMethod2()使用。

PHPUnit則為我們提供了全面的hook接口:

public static function setUpBeforeClass()/tearDownAfterClass()//測試類構建/解構時調用
protected function setUp()/tearDown()//測試方法執(zhí)行前/后調用
protected function assertPreConditions()/assertPostConditions()//斷言前/后調用

當運行測試時,每個測試類大致就是如下的執(zhí)行步驟

#測試類基境構建
setUpBeforeClass

#new一個測試類對象
#第一個測試用例
setUp
assertPreConditions
assertPostConditions
tearDown

#new一個測試類對象
#第二個測試用例
setUp
assertPreConditions
assertPostConditions
tearDown
...

#測試類基境解構
tearDownAfterClass

所以我們可以在測試類構建時使用setUpBeforeClass創(chuàng)建一個 App\Example 對象作為測試類的靜態(tài)成員變量(tearDownAfterClass主要用于一些資源清理,比如關閉文件,數(shù)據(jù)庫連接),然后讓每一個測試方法用例使用它:

?php
namespace Tests;

use App\Example;
use PHPUnit\Framework\TestCase as BaseTestCase;

class ExampleTest extends BaseTestCase
{
  // 類靜態(tài)屬性
  private static $example;

  public static function setUpBeforeClass()
  {
    self::$example = new Example();
  }

  public function testGetTrue()
  {
    // 類的靜態(tài)屬性更新
    self::$example->setMsg("hello big_cat");
    $result = self::$example->getTrue();
    $this->assertTrue($result);
  }

  public function testGetFalse()
  {
    $result = self::$example->getFalse();
    $this->assertFalse($result);
  }

  /**
   * 依賴 testGetTrue 執(zhí)行完畢
   * @depends testGetTrue
   * @return [type] [description]
   */
  public function testGetMsg()
  {
    $result = self::$example->getMsg();
    $this->assertEquals($result, "hello big_cat");
  }
}

或者使用@depends注解來聲明二者的執(zhí)行順序,并使用傳遞參數(shù)的方式來滿足需求。

public function testMethod1()
{
  $this->assertTrue(true);
  return "hello";
}

/**
 * @depends testMethod1
 */
public function testMethod2($str)
{
  $this->assertEquals("hello", $str);
}
#執(zhí)行模式大概如下
testObj1 = new Test;
$str = testObj1->testMethod1();

testObj2 = new Test;
testObj2->testMethod2($str);

理解測試執(zhí)行的模式還是很有幫助的,其他高級特性請瀏覽官方文檔。

使用phpunit.xml編排測試套件

使用測試套件來管理測試,vi phpunit.xml

?xml version="1.0" encoding="UTF-8"?>
phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="./vendor/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false">
  testsuites>
    !--可以定義多個 suffix 用于指定待執(zhí)行的測試類文件后綴-->
    testsuite name="Tests">
      directory suffix="Test.php">./test/directory>
    /testsuite>
  /testsuites>
  filter>
    whitelist processUncoveredFilesFromWhitelist="true">
      !--可以定義多個 對./app下的業(yè)務代碼做覆蓋率統(tǒng)計-->
      directory suffix=".php">./app/directory>
    /whitelist>
  /filter>
  logging>
    !--覆蓋率報告生成類型和輸出目錄 lowUpperBound低覆蓋率閾值 highLowerBound高覆蓋率閾值-->
    log type="coverage-html" target="./reports" lowUpperBound="35" highLowerBound="70"/>
  /logging>
/phpunit>

然后直接運phpunit行即可:

[root@localhost unit]# phpunit 
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Time: 81 ms, Memory: 4.00MB

No tests executed!

Generating code coverage report in HTML format ... done

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHPUnit 單元測試安裝與使用入門教程
  • ThinkPHP5與單元測試PHPUnit使用詳解
  • PHP單元測試配置與使用方法詳解
  • PHPUnit + Laravel單元測試常用技能
  • PHP使用phpunit進行單元測試示例
  • PHPStorm中如何對nodejs項目進行單元測試詳解
  • PHP單元測試框架PHPUnit用法詳解
  • thinkPHP框架單元測試庫tpunit用法示例
  • 淺談如何提高PHP代碼質量之單元測試

標簽:吐魯番 常州 荊門 遵義 徐州 遂寧 武威 寧夏

巨人網絡通訊聲明:本文標題《使用PHPUnit進行單元測試并生成代碼覆蓋率報告的方法》,本文關鍵詞  使用,PHPUnit,進行,單元,測試,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用PHPUnit進行單元測試并生成代碼覆蓋率報告的方法》相關的同類信息!
  • 本頁收集關于使用PHPUnit進行單元測試并生成代碼覆蓋率報告的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 国产国际精品福利色噜噜| 日韩美女在线视频网站免费观看| 欧美一线二线三显区别| 嘿咻视频在线观看| 电影天堂大片欧美 - 百度 | 粉嫩被两个粗黑疯狂进出| 国产做受91???高潮| 骚宝spa| 精品国产高清自在线一区二区三区 | 被迫撑开颤抖高潮求饶bl| AⅤ亚洲AV天堂波多野吉衣| 日本动漫免费网站| 人人澡人人添人人玩| 又粗又大又黄又硬又爽毛片| 深夜影院在线视频观看| 秀婷程仪公欲息肉婷在线观看| 亚欧色视频在线观看免费| 国产高清免费视频| 91超薄丝袜肉丝一区二区| 乳欲人妻1~5集动漫无删减| 借种娇妻半夜卧室里呻吟声| 亚洲成a人片77777在线播放| 御宅屋高辣| 国产三级国产精品国产普男人| 动漫美女被揉胸| 搡老熟女XXXX搡老女人图片| 国产精品久久久久久中文字| 人人草人人舔| 一级A片在线观看| 好爽?好紧?人妻老师别夹H| 国产一级婬片AAAAA片口述| 超污视频在线看| 肥丑攻np糟蹋美人受| 台湾50部三级真做落三风| 婚礼上的乱啪| 国产?高潮?白浆?喷| 中文无码完熟50妇AV在线| 亚洲图区欧美| 91女神反差婊在线观看| 英语课代表的胸软软的感觉| 人与性动交ⅩXXXB女在|