PHP單元測(cè)試?yán)鳎篜HPUnit深入理解
在本系列文章的前兩篇中初探PHP單元測(cè)試?yán)鳎篜HPUnit和PHP單元測(cè)試?yán)鳎篜HPUnit深入用法中,分別介紹了PHPUnit的基本用法和PHPUnit中的一些重要用法。在本文中,筆者將為大家介紹PHPUnit中的兩個(gè)高級(jí)概念和用法,盡管它不一定在你的日常單元測(cè)試中都用到,但理解和學(xué)會(huì)它們的用法對(duì)學(xué)習(xí)PHPUnit還是十分重要的。
PHPUnit中的Annotations
如果有其他編程語言經(jīng)驗(yàn)的開發(fā)者,應(yīng)該對(duì)Annotations(注解)不陌生,其實(shí)在PHPUnit中,一個(gè)簡單的如下面的一段注釋也可以認(rèn)為是Annotations:
- <?php
- class MyTestClass extends PHPUnit_Framework_TestCase
- {
- /**
- * Testing the answer to “do you love unit tests?”
- */
- public function testDoYouLoveUnitTests()
- {
- $love = true;
- $this->assertTrue($love);
- }
- }
- ?>
可以看到,其實(shí)一段以/** **/為標(biāo)記的文字,就可以認(rèn)為是一種Annotations,但Annotations其實(shí)不單單是簡單的注釋,它是與一個(gè)程序元素相關(guān)聯(lián)信息或者元數(shù)據(jù)的標(biāo)注,它不影響程序的運(yùn)行,但相關(guān)的軟件工具或框架能夠?qū)⑵滢D(zhuǎn)換成特殊的元數(shù)據(jù)標(biāo)記,以方便開發(fā)者以更少的代碼去提高效率(比如通過。如果你熟悉Java,則會(huì)發(fā)現(xiàn)在Java SE 5中及象Spring等框架中,都大量使用了Annotations。
然而,由于PHP并不象Java那樣是編譯性語言,因此本身缺乏去解析Annotations的機(jī)制,但幸好PHPUnit去提供了這樣的功能,我們以下面的代碼為例:
- <?php
- class MyMathClass
- {
- /**
- * Add two given values together and return sum
- */
- public function addValues($a,$b)
- {
- return $a+$b;
- }
- }
- ?>
上面的只是一個(gè)簡單的加法的例子,為此,我們使用Annotations去編寫一個(gè)單元測(cè)試,在上兩篇文章中,我們采用的是手工編寫單元測(cè)試的方法,而本文中,將介紹使用PHPUnit命令行的方法,自動(dòng)生成單元測(cè)試的框架,方法如下:
首先把上面的類保存為MyMathClass.PHP,然后在命令行下運(yùn)行如下命令:
- phpunit –skeleton-test MyMathClass
這時(shí)PHPUnit會(huì)自動(dòng)生成如下的框架單元測(cè)試代碼:
- <?php
- require_once '/path/to/MyMathClass.php';
- /**
- * Test class for MyMathClass.
- * Generated by PHPUnit on 2011-02-07 at 12:22:07.
- */
- class MyMathClassTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var MyMathClass
- */
- protected $object;
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp()
- {
- $this->object = new MyMathClass;
- }
- /**
- * Tears down the fixture, for example, closes a network connection.
- * This method is called after a test is executed.
- */
- protected function tearDown()
- {
- }
- /**
- * @todo Implement testAddValues().
- */
- public function testAddValues()
- {
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
- }
- }
- ?>
可以看到,PHPUnit為我們生成的單元測(cè)試代碼自動(dòng)引入了原來的MyMathClass.PHP,同時(shí)也生成了setUp和tearDown方法,但唯一的核心單元測(cè)試代碼是留給了我們編寫。如果想在這個(gè)基礎(chǔ)上更快速的生成我們想要的單元測(cè)試代碼,要如何實(shí)現(xiàn)呢?沒錯(cuò),就是使用annotations!我們可以在原來的MyMathClass.PHP中加入如下的annotations。
- <?php
- class MyMathClass
- {
- /**
- * Add two given values together and return sum
- * @assert (1,2) == 3
- */
- public function addValues($a,$b)
- {
- return $a+$b;
- }
- }
- ?>
然后再象上述一樣在命令行運(yùn)行:
- phpunit –skeleton-test MyMathClass
這個(gè)時(shí)候會(huì)為我們生成如下的單元測(cè)試代碼:
- <?php
- /**
- * Generated from @assert (1,2) == 3.
- */
- public function testAddValues()
- {
- $this->assertEquals(
- 3,
- $this->object->addValues(1,2)
- );
- }
- ?>
看到了么?我們?cè)谠械念愔屑尤肓俗⒔釦assert(1,2)==3,則PHPUnit自動(dòng)為我們生成了正確的單元測(cè)試代碼。當(dāng)然,可以參考PHPUnit手冊(cè),學(xué)習(xí)到更多的關(guān)于@assert注解使用的規(guī)則。
下面再舉一個(gè)例子來講解annotations。假設(shè)我們的程序中的一個(gè)方法,只是僅需要數(shù)據(jù)的輸入,并且不依賴XML或者數(shù)據(jù)庫提供數(shù)據(jù)源,則為了測(cè)試這個(gè)方法,我們可能想到的一個(gè)方法是在程序中設(shè)置一個(gè)測(cè)試數(shù)據(jù)集去測(cè)試,但這里介紹一個(gè)比較簡單的方法,就是使用注解@dataProvider,修改MyMathClass.PHP如下:
- <?php
- /**
- * Data provider for test methods below
- */
- public static function provider()
- {
- return array(
- array(1,2,3),
- array(4,2,6),
- array(1,5,7)
- );
- }
- /**
- * Testing addValues returns sum of two values
- * @dataProvider provider
- */
- public function testAddValues($a,$b,$sum)
- {
- $this->assertEquals(
- $sum,
- $this->object->addValues($a,$b)
- );
- }
- ?>
可以看到,這里使用了注解@dataProvider,指明了測(cè)試用例的數(shù)據(jù)提供者是由provider方法返回的一個(gè)數(shù)組。所以在單元測(cè)試時(shí),數(shù)組中的第0個(gè)元素則會(huì)賦值給$a,第1個(gè)元素則會(huì)賦值給b,第3個(gè)元素則會(huì)賦值給sum,可以看到,上面的第3個(gè)數(shù)組提供的數(shù)據(jù)是不能通過單元測(cè)試的,因?yàn)?+5不等于7。
此外,這里還簡單介紹兩個(gè)常用的annotations,比如@expectedException注解可以測(cè)試代碼中是否正確拋出了異常,比如:
- <?phprequire_once 'PHPUnit/Framework.php';
- class ExceptionTest extends PHPUnit_Framework_TestCase{
- /**
- * @expectedException InvalidArgumentException */
- public function testException() {
- }
- }
- ?>
這里就用注解的方法表示testException中必須拋出的異常類型為InvalidArgumentException。
另外一個(gè)是@cover注解。它的作用是標(biāo)識(shí)PHPUnit只為類中的哪些方法或作用域生成測(cè)試代碼,比如:
- /**
- * @covers SampleClass::publicMethod
- * @covers SampleClass::<!public>
- * @covers HelperClass<extended>
- */
- public function testMethod()
- {
- $result = SampleClass::method();
- }
則PHPUnit只為SampleClass類中的publicMethod方法、SampleClass類中的所有非public聲明的方法和HelperClass類或者它的其中一個(gè)父類產(chǎn)生單元測(cè)試代碼。
#p#
PHPUnit中的Mocking
在介紹Mocking前,先來看下為什么要使用Mocking。舉一個(gè)數(shù)據(jù)庫查詢的例子,比如在某個(gè)應(yīng)用中,如果要測(cè)試一個(gè)數(shù)據(jù)庫的應(yīng)用,但假如這個(gè)數(shù)據(jù)庫的測(cè)試要耗費(fèi)很多資源以及編寫很復(fù)雜的單元測(cè)試的代碼的話,可以嘗試使用Mocking技術(shù)。舉例說明如下:
- <?php
- class Database
- {
- public function reallyLongTime()
- {
- $results = array(
- array(1,'test','foo value')
- );
- sleep(100);
- return $results;
- }
- }
- ?>
在上面這個(gè)例子中,我們模擬了一個(gè)數(shù)據(jù)庫的操作,認(rèn)為它需要運(yùn)行很長時(shí)間。接下來我們編寫其單元測(cè)試代碼如下:
- <?php
- require_once '/path/to/Database.php';
- class DatabaseTest extends PHPUnit_Framework_TestCase
- {
- private $db = null;
- public function setUp()
- {
- $this->db = new Database();
- }
- public function tearDown()
- {
- unset($this->db);
- }
- /**
- * Test that the "really long query" always returns values
- */
- public function testReallyLongReturn()
- {
- $mock = $this->getMock('Database');
- $result = array(
- array(1,'foo','bar test')
- );
- $mock->expects($this->any())
- ->method('reallyLongTime')
- ->will($this->returnValue($result));
- $return = $mock->reallyLongTime();
- $this->assertGreaterThan(0,count($return));
- }
- }
- ?>
注意看這段代碼中有趣的地方,這里,使用了PHPUnit中的getMock對(duì)象方法,這里實(shí)際上是模擬生成一個(gè)Database類的“偽實(shí)例”了,這里生成了$mock這個(gè)mock對(duì)象實(shí)例,以方便接著的單元測(cè)試中用到。接下來的這三行代碼:
- $mock->expects($this->any())
- ->method('reallyLongTime')
- ->will($this->returnValue($result));
它們的含義為:無論方法reallyLongtime執(zhí)行了多長時(shí)間,始終最后會(huì)直接返回$result這個(gè)數(shù)組的結(jié)果。這樣,你就可以通過mocking技術(shù)很輕易地去實(shí)現(xiàn)在單元測(cè)試中,繞過某些復(fù)雜的邏輯部分,而節(jié)省大量的寶貴時(shí)間提高測(cè)試效率。
下面的這個(gè)例子,講解的是Mocking技術(shù)中的更高級(jí)用法Mockbuilder。依然以上面的例子說明:
- <?php
- public function testReallyLongRunBuilder()
- {
- $stub = $this->getMockBuilder('Database')
- ->setMethods(array(
- 'reallyLongTime'
- ))
- ->disableAutoload()
- ->disableOriginalConstructor()
- ->getMock();
- $result = array(array(1,'foo','bar test'));
- $stub->expects($this->any())
- ->method('reallyLongTime')
- ->will($this->returnValue($result));
- $this->assertGreaterThan(0,count($return));
- }
- ?>
通過使用Mockbuilder,我們可以不用通過構(gòu)造函數(shù)的方法去初始化一個(gè)mock對(duì)象。這段代碼跟上一段代碼的功能其實(shí)是一樣的,只不過留意一下新的兩個(gè)方法: disableAutoload和disableOriginalConstructor,其功能分別是禁止使用PHP的內(nèi)置的autoload初始構(gòu)造方法和禁止調(diào)用該類原有的構(gòu)造函數(shù)。最后再看一個(gè)例子:
- <?php
- /**
- * Testing enforcing the type to "array" like the "enforceTypes"
- * method does via type hinting
- */
- public function ttestReallyLongRunBuilderConstraint()
- {
- $stub = $this->getMock('Database',array('reallyLongTime'));
- $stub->expects($this->any())
- ->method('reallyLongTime')
- ->with($this->isType('array'));
- $arr = array('test');
- $this->assertTrue($stub-> reallyLongTime ($arr));
- }
- ?>
在這里,我們使用了with方法,其中這個(gè)方法中指定了要傳入的參數(shù)類型為array數(shù)組類型,最后這個(gè)斷言是通過了,因?yàn)榉祷氐牡拇_是數(shù)組類型。
【編輯推薦】