色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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

1、markTestSkipped和markTestIncomplete

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

<?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');
  }
}
?>

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

  而markTestIncomplete也有點類似,但有點不同的是,它是當開發者在編寫一個未完成的測試方法時使用的,標記出某個測試方法還沒編寫完成,同樣測試結果也不會是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中的斷言

  在上一篇文章中,已經基本講解了一些基本的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 */
}
?>

   在上一篇文章中,已經介紹了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");
}
?>

php技術PHP單元測試利器 PHPUNIT深入用法(二)第1/2頁,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 来宾市| 云和县| 舞钢市| 弋阳县| 慈利县| 邯郸市| 彰化市| 廊坊市| 东乌| 京山县| 阜城县| 黄龙县| 溆浦县| 怀安县| 治县。| 潞城市| 松阳县| 蒙阴县| 蓬莱市| 济南市| 高阳县| 红安县| 阿拉善盟| 平湖市| 开化县| 宁波市| 阿拉善盟| 古交市| 山阳县| 无为县| 金塔县| 浦东新区| 温泉县| 邵阳市| 曲麻莱县| 扎鲁特旗| 兴安盟| 正蓝旗| 且末县| 六安市| 葫芦岛市|