PHP單元測試?yán)鳎篜HPUnit深入用法
在上一篇初探PHP單元測試?yán)鳎篜HPUnit文章中,我們對PHPUnit有了一個初步的認(rèn)識,在本文中將繼續(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)該是被忽略的,因?yàn)槌霈F(xiàn)了異常,而注意的時,此時有可能你寫的代碼是正確的,只不過是出現(xiàn)了異常而已,這樣PHPUnit在輸出時就不會只是簡單的輸出fail。
而markTestIncomplete也有點(diǎn)類似,但有點(diǎn)不同的是,它是當(dāng)開發(fā)者在編寫一個未完成的測試方法時使用的,標(biāo)記出某個測試方法還沒編寫完成,同樣測試結(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()實(shí)例并保存在變量$_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");
- }
- ?>
在上一篇文章中已經(jīng)介紹過assertTrue和assertFalse了,這里留意一下其中的第二個參數(shù),其含義是,當(dāng)該斷言的測試不通過時,自定義的顯示信息。比如在這個測試方法中,當(dāng)trueProperty不為真值時,將顯示“trueProperty isn't true”的信息。
接下來再看下在數(shù)值方面上PHPUnit的斷言使用實(shí)例:
- <?php
- public function testValueEquals()
- {
- $valueOne = 4;
- $valueTwo = 2;
- $this->assertEquals($this->_testable->addValues($valueOne,$valueTwo),6);
- }
- public function testValueGreaterThan()
- {
- $valueOne = 4;
- $valueTwo = 2;
- $this->assertGreaterThan($valueTwo,$valueOne);
- }
- public function testLessThanOrEqual()
- {
- $valueOne = 4;
- $valueTwo = 2;
- $this->assertLessThanOrEqual($valueTwo,$valueOne);
- }
- public function testAreObjectsEqual()
- {
- $testTwo = new Testable();
- $this->_testable->resetMe = false;
- $this->assertEquals($this->_testable,$testTwo);
- }
- ?>
其中,assertEquals為判斷是否相等,assertGreaterThan為判斷是否大于,assertLessThanOrEqual判斷是否小于或等于,而assertEquals這里要注意一下,它還可以用來判斷兩個對象是否相等,比如這里就判斷了$testTwo這個Testable類的實(shí)例是否和新設(shè)置的resetMe這個對象相等。
除了在數(shù)值方面的斷言外,在字符方面還有一些很多斷言的功能,看下面的代碼:
- <?php
- public function testStringEnding()
- {
- $testString = $this->_testable->getTestString();
- $this->assertStringEndsWith('frood',$testString);
- }
- public function testStringStarts()
- {
- $testString = $this->_testable->getTestString();
- $this->assertStringStartsWith('hoopy',$testString);
- }
- public function testEqualFileContents()
- {
- $this->assertStringEqualsFile('/path/to/textfile.txt','foo');
- }
- public function testDoesStringMatchFormat()
- {
- $testString = $this->_testable->getTestString();
- $this->assertStringMatchesFormat('%s',$testString);
- }
- ?>
其中, assertStringStartsWith斷言是判斷字符串是否以指定的字符串開頭,assertStringEndsWith斷言判斷字符串是否以指定的字符串結(jié)尾。assertStringEqualsFile斷言判斷給定的文件中是否含有指定的字符,比如這里就判斷textfile.txt這個文件中是否包含字符串foo。
而assertStringMatchesFormat可以讓用戶指定匹配的模式去判斷一個字符串是否符合要求,如 $this->assertStringMatchesFormat('%s',$testString);
這里則判斷$testString是否是字符串類型,具體的可以參考PHPUnit手冊。
再來看如下的代碼:
- <?php
- public function testStringIsNotNull()
- {
- $notANull = “i'm not a null!”;
- $this->assertNull($notANull);
- }
- public function testStringIsSame()
- {
- $numberAsString = '1234';
- $this->assertSame(1234,$numberAsString);
- }
- ?>
其中assertNull判斷某個變量是否為null,而assertSame則嚴(yán)格判斷兩個變量是否同一個類型,盡管在PHP中是弱類型語言,但這里通過assertSame還是能判斷出$numberAsString為字符串類型,跟期望的1234數(shù)字類型不匹配,所以測試不能通過。
***我們來看一下平??赡懿淮蟪S玫臄嘌裕挚赡軐δ愕膯卧獪y試工作十分有幫助的,先看代碼如下:
- <?php
- public function testArrayKeyExists()
- {
- $this->assertArrayHasKey('first key',$this->_testable->testArray);
- }
- public function testAttributeExists()
- {
- $this->assertClassHasAttribute('resetMe',get_class($this->_testable));
- }
- public function testFileIsReal()
- {
- $this->assertFileExists('/path/to/file.txt');
- }
- public function testIsInstance()
- {
- $this->assertInstanceOf('OtherClass',$this->_testable);
- }
- <?php
- public function testDoesMatchRegex()
- {
- $testString = $this->_testable->getTestString();
- $this->assertRegExp('/[a-z]+/',$testString);
- }
- ?>
代碼中***個斷言assertArrayHasKey,是用來檢查一個數(shù)組中是否每個鍵值都是存在的,比如我們的數(shù)組中,“firstkey”這個值是有鍵1與其對應(yīng)的,所以測試能通過。而assertClassHasAttribute則能判斷某個類是否有相應(yīng)的屬性,這個例子中測試也能通過;
而assertFileExists則判斷在本地文件系統(tǒng)中是否存在指定的文件。而assertInstanceOf則判斷某個你正在創(chuàng)建的對象是否為某個類的實(shí)例。assertRegExp相信大家都知道,這個是判斷某個字符串中是否與給定的正則表達(dá)式相匹配。
原文鏈接:http://tech.it168.com/a2011/0215/1157/000001157580_all.shtml
【編輯推薦】