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");

}

?>