最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP單元測試利器 PHPUNIT深入用法(二)第1/2頁

 更新時間:2011年03月06日 21:46:50   作者:  
在上一篇PHP單元測試利器:PHPUNIT初探文章中,我們對phpunit有了一個初步的認識,在本文中將繼續(xù)深入講解下phpunit中的一些用法。
1、markTestSkipped和markTestIncomplete

  在phpunit中,有兩個有用的方法markTestSkipped和markTestIncomplete。它們能允許你編寫的單元測試中不單是只有通過和失敗兩種結(jié)果。markTestSkipped能讓PHPUNIT不去執(zhí)行某個已經(jīng)編寫好的測試方法。舉個例子說明,比如下面的程序:

<?php
public function testThisMightHaveADb()
{
  
$myObject->createObject();
  
try {
    
$db = new Database();
    
$this->assertTrue($db->rowExists());
  }
catch (DatabseException $e) {
    
$this->markTestSkipped('This test was skipped because there was a database problem');
  }
}
?>

   在上面的程序中,是一個連接數(shù)據(jù)庫后,判斷數(shù)據(jù)是否存在的測試方法,但如果考慮數(shù)據(jù)庫的連接異常的話,則應(yīng)該在拋出異常時,使用markTestSkipped指出該測試方法應(yīng)該是被忽略的,因為出現(xiàn)了異常,而注意的時,此時有可能你寫的代碼是正確的,只不過是出現(xiàn)了異常而已,這樣phpunit在輸出時就不會只是簡單的輸出fail。

  而markTestIncomplete也有點類似,但有點不同的是,它是當開發(fā)者在編寫一個未完成的測試方法時使用的,標記出某個測試方法還沒編寫完成,同樣測試結(jié)果也不會是fail,只是告訴phpunit這個測試方法還沒編寫完成而已,例子如下:

<?php
public function testAreNotEnoughHours()
{
  
$this->markTestIncomplete("There aren't enough hours in the day to have my tests go green");
  
$trueVariable = true;
  
$this->assertTrue($trueVariable);
}
?>

   2、更深入了解phpunit中的斷言

  在上一篇文章中,已經(jīng)基本講解了一些基本的phpunit中的斷言的使用,這里以一個例子,下面是一個類的代碼:

<?php
class Testable
{
  
public $trueProperty = true;
  
public $resetMe = true;
  
public $testArray = array(
    
'first key' => 1,
    
'second key' => 2
  );
  
private $testString = "I do love me some strings";
  
public function __construct()
  {
  }
  
public function addValues($valueOne,$valueTwo) {
    
return $valueOne+$valueTwo;
  }
  
public function getTestString()
  {
    
return $this->testString;
  }
}
?>

   我們編寫的單元測試代碼初步的框架如下:

<?php
class TestableTest extends PHPUnit_Framework_TestCase
{
  
private $_testable = null;
  
public function setUp()
  {
    
$this->_testable = new Testable();
  }
  
public function tearDown()
  {
    
$this->_testable = null;
  }
  
/** test methods will go here */
}
?>

   在上一篇文章中,已經(jīng)介紹了setUp方法和tearDown方法,這里的setUp方法中,建立了Testable()實例并保存在變量$_testable中,而在tearDown方法中,銷毀了該對象。

  接下來,開始編寫一些斷言去測試,首先看assertTrue和assertFalase:

<?php
public function testTruePropertyIsTrue()
{
  
$this->assertTrue($this->_testable->trueProperty,"trueProperty isn't true");
}
public function testTruePropertyIsFalse()
{
  
$this->assertFalse($this->_testable->trueProperty, "trueProperty isn't false");
}
?>

相關(guān)文章

最新評論

瑞丽市| 玉树县| 庄河市| 安岳县| 北海市| 利川市| 临安市| 申扎县| 孟连| 平武县| 米林县| 蒙城县| 留坝县| 萨嘎县| 沂南县| 宜昌市| 屏山县| 兰州市| 磴口县| 昌平区| 长乐市| 甘谷县| 石首市| 阿巴嘎旗| 乌什县| 泊头市| 玉龙| 霍城县| 潮安县| 祁阳县| 云安县| 时尚| 大英县| 镇安县| 牡丹江市| 常德市| 集安市| 吴江市| 洛隆县| 平原县| 邳州市|