亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP測試框架PHPUnit組織測試操作示例

 更新時(shí)間:2018年05月28日 11:21:24   作者:我是天才啊  
這篇文章主要介紹了PHP測試框架PHPUnit組織測試,結(jié)合實(shí)例形式分析了PHPUnit組織測試具體步驟、相關(guān)命令與操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP測試框架PHPUnit組織測試操作。分享給大家供大家參考,具體如下:

首先是目錄結(jié)構(gòu)

源文件夾為 src/
測試文件夾為 tests/

User.php

<?php
class Errorcode
{
  const NAME_IS_NULL = 0;
}
class User
{
  public $name;
  public function __construct($name)
  {
    $this->name=$name;
  }
  public function Isempty()
  {
    try{
      if(empty($this->name))
      {
        throw new Exception('its null',Errorcode::NAME_IS_NULL);
      }
    }catch(Exception $e){
      return $e->getMessage();
    }
    return 'welcome '.$this->name;
  }
}

對應(yīng)的單元測試文件  UserTest.php

<?php
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
  protected $user;
  public function setUp()
  {
    $this->user = new User('');
  }
  public function testIsempty()
  {
    $this->user->name='mark';
    $result =$this->user->Isempty();
    $this->assertEquals('welcome mark',$result);
    $this->user->name='';
    $results =$this->user->Isempty();
    $this->assertEquals('its null',$results);
  }
}

第二個(gè)單元測試代碼因?yàn)橐?要測試的類  這里可以用 自動載入 避免文件多的話 太多include

所以在src/ 文件夾里寫 autoload.php

<?php
function __autoload($class){
  include $class.'.php';
}
spl_autoload_register('__autoload');

當(dāng)需要User類時(shí),就去include User.php。寫完__autoload()函數(shù)之后要用spl_autoload_register()注冊上。

雖然可以自動載入,但是要執(zhí)行的命令變得更長了。

打開cmd命令如下

phpunit --bootstrap src/autoload.php tests/UserTest

所以我們還可以在根目錄寫一個(gè)配置文件phpunit.xml來為項(xiàng)目指定bootstrap,這樣就不用每次都寫在命令里了。

phpunit.xml

<phpunit bootstrap="src/autoload.php">
</phpunit>

然后

打開cmd命令 執(zhí)行MoneyTest 命令如下

phpunit tests/UserTest

打開cmd命令 執(zhí)行tests下面所有的文件 命令如下

phpunit tests

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP錯(cuò)誤與異常處理方法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《php優(yōu)秀開發(fā)框架總結(jié)

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論