自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP單元測(cè)試?yán)鳎篜HPUnit深入理解

開發(fā) 后端
在本文中,筆者將為大家介紹PHPUnit中的兩個(gè)高級(jí)概念和用法,盡管它不一定在你的日常單元測(cè)試中都用到,但理解和學(xué)會(huì)它們的用法對(duì)學(xué)習(xí)PHPUnit還是十分重要的。

在本系列文章的前兩篇中初探PHP單元測(cè)試?yán)鳎篜HPUnitPHP單元測(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:

  1. <?php  
  2. class MyTestClass extends PHPUnit_Framework_TestCase  
  3. {  
  4. /**  
  5. * Testing the answer to “do you love unit tests?”  
  6. */ 
  7. public function testDoYouLoveUnitTests()  
  8. {  
  9. $love = true;  
  10. $this->assertTrue($love);  
  11. }  
  12. }  
  13. ?> 

可以看到,其實(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去提供了這樣的功能,我們以下面的代碼為例:

  1. <?php  
  2. class MyMathClass  
  3. {  
  4. /**  
  5. * Add two given values together and return sum  
  6. */ 
  7. public function addValues($a,$b)  
  8. {  
  9. return $a+$b;  
  10. }  
  11. }  
  12. ?> 

上面的只是一個(gè)簡單的加法的例子,為此,我們使用Annotations去編寫一個(gè)單元測(cè)試,在上兩篇文章中,我們采用的是手工編寫單元測(cè)試的方法,而本文中,將介紹使用PHPUnit命令行的方法,自動(dòng)生成單元測(cè)試的框架,方法如下:

首先把上面的類保存為MyMathClass.PHP,然后在命令行下運(yùn)行如下命令:

  1. phpunit –skeleton-test MyMathClass 

這時(shí)PHPUnit會(huì)自動(dòng)生成如下的框架單元測(cè)試代碼:

  1. <?php  
  2. require_once '/path/to/MyMathClass.php';  
  3. /**  
  4. * Test class for MyMathClass.  
  5. * Generated by PHPUnit on 2011-02-07 at 12:22:07.  
  6. */ 
  7. class MyMathClassTest extends PHPUnit_Framework_TestCase  
  8. {  
  9. /**  
  10. * @var MyMathClass  
  11. */ 
  12. protected $object;  
  13. /**  
  14. * Sets up the fixture, for example, opens a network connection.  
  15. * This method is called before a test is executed.  
  16. */ 
  17. protected function setUp()  
  18. {  
  19. $this->object = new MyMathClass;  
  20. }  
  21. /**  
  22. * Tears down the fixture, for example, closes a network connection.  
  23. * This method is called after a test is executed.  
  24. */ 
  25. protected function tearDown()  
  26. {  
  27. }  
  28. /**  
  29. * @todo Implement testAddValues().  
  30. */ 
  31. public function testAddValues()  
  32. {  
  33. // Remove the following lines when you implement this test.  
  34. $this->markTestIncomplete(  
  35. 'This test has not been implemented yet.' 
  36. );  
  37. }  
  38. }  
  39. ?> 

可以看到,PHPUnit為我們生成的單元測(cè)試代碼自動(dòng)引入了原來的MyMathClass.PHP,同時(shí)也生成了setUp和tearDown方法,但唯一的核心單元測(cè)試代碼是留給了我們編寫。如果想在這個(gè)基礎(chǔ)上更快速的生成我們想要的單元測(cè)試代碼,要如何實(shí)現(xiàn)呢?沒錯(cuò),就是使用annotations!我們可以在原來的MyMathClass.PHP中加入如下的annotations。

  1. <?php  
  2. class MyMathClass  
  3. {  
  4. /**  
  5. * Add two given values together and return sum  
  6. * @assert (1,2) == 3  
  7. */ 
  8. public function addValues($a,$b)  
  9. {  
  10. return $a+$b;  
  11. }  
  12. }  
  13. ?>  

然后再象上述一樣在命令行運(yùn)行:

  1. phpunit –skeleton-test MyMathClass 

這個(gè)時(shí)候會(huì)為我們生成如下的單元測(cè)試代碼:

  1. <?php  
  2. /**  
  3. * Generated from @assert (1,2) == 3.  
  4. */ 
  5. public function testAddValues()  
  6. {  
  7. $this->assertEquals(  
  8. 3,  
  9. $this->object->addValues(1,2)  
  10. );  
  11. }  
  12. ?> 

看到了么?我們?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如下:

  1. <?php  
  2. /**  
  3. * Data provider for test methods below  
  4. */ 
  5. public static function provider()  
  6. {  
  7. return array(  
  8. array(1,2,3),  
  9. array(4,2,6),  
  10. array(1,5,7)  
  11. );  
  12. }  
  13. /**  
  14. * Testing addValues returns sum of two values  
  15. * @dataProvider provider  
  16. */ 
  17. public function testAddValues($a,$b,$sum)  
  18. {  
  19. $this->assertEquals(  
  20. $sum,  
  21. $this->object->addValues($a,$b)  
  22. );  
  23. }  
  24. ?> 

可以看到,這里使用了注解@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è)試代碼中是否正確拋出了異常,比如:

  1. <?phprequire_once 'PHPUnit/Framework.php';   
  2. class ExceptionTest extends PHPUnit_Framework_TestCase{      
  3. /**    
  4.    * @expectedException InvalidArgumentException     */      
  5. public function testException()    {    
  6.   }  
  7. }  
  8.  
  9. ?>  

這里就用注解的方法表示testException中必須拋出的異常類型為InvalidArgumentException。

另外一個(gè)是@cover注解。它的作用是標(biāo)識(shí)PHPUnit只為類中的哪些方法或作用域生成測(cè)試代碼,比如:

  1. /**  
  2.      * @covers SampleClass::publicMethod  
  3.      * @covers SampleClass::<!public>  
  4.      * @covers HelperClass<extended>  
  5.      */ 
  6.     public function testMethod()  
  7.     {  
  8.         $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ù)。舉例說明如下:

  1. <?php  
  2. class Database  
  3. {  
  4. public function reallyLongTime()  
  5. {  
  6. $results = array(  
  7. array(1,'test','foo value')  
  8. );  
  9. sleep(100);  
  10. return $results;  
  11. }  
  12. }  
  13. ?> 

在上面這個(gè)例子中,我們模擬了一個(gè)數(shù)據(jù)庫的操作,認(rèn)為它需要運(yùn)行很長時(shí)間。接下來我們編寫其單元測(cè)試代碼如下:

  1. <?php  
  2. require_once '/path/to/Database.php';  
  3. class DatabaseTest extends PHPUnit_Framework_TestCase  
  4. {  
  5. private $db = null;  
  6. public function setUp()  
  7. {  
  8. $this->db = new Database();  
  9. }  
  10. public function tearDown()  
  11. {  
  12. unset($this->db);  
  13. }  
  14. /**  
  15. * Test that the "really long query" always returns values  
  16. */ 
  17. public function testReallyLongReturn()  
  18. {  
  19. $mock = $this->getMock('Database');  
  20. $result = array(  
  21. array(1,'foo','bar test')  
  22. );  
  23. $mock->expects($this->any())  
  24. ->method('reallyLongTime')  
  25. ->will($this->returnValue($result));  
  26. $return = $mock->reallyLongTime();  
  27. $this->assertGreaterThan(0,count($return));  
  28. }  
  29. }  
  30. ?>  

注意看這段代碼中有趣的地方,這里,使用了PHPUnit中的getMock對(duì)象方法,這里實(shí)際上是模擬生成一個(gè)Database類的“偽實(shí)例”了,這里生成了$mock這個(gè)mock對(duì)象實(shí)例,以方便接著的單元測(cè)試中用到。接下來的這三行代碼:

  1.  $mock->expects($this->any())  
  2. ->method('reallyLongTime')  
  3. ->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。依然以上面的例子說明:

  1. <?php  
  2. public function testReallyLongRunBuilder()  
  3. {  
  4. $stub = $this->getMockBuilder('Database')  
  5. ->setMethods(array(  
  6. 'reallyLongTime' 
  7. ))  
  8. ->disableAutoload()  
  9. ->disableOriginalConstructor()  
  10. ->getMock();  
  11. $result = array(array(1,'foo','bar test'));  
  12. $stub->expects($this->any())  
  13. ->method('reallyLongTime')  
  14. ->will($this->returnValue($result));  
  15. $this->assertGreaterThan(0,count($return));  
  16. }  
  17. ?> 

通過使用Mockbuilder,我們可以不用通過構(gòu)造函數(shù)的方法去初始化一個(gè)mock對(duì)象。這段代碼跟上一段代碼的功能其實(shí)是一樣的,只不過留意一下新的兩個(gè)方法: disableAutoload和disableOriginalConstructor,其功能分別是禁止使用PHP的內(nèi)置的autoload初始構(gòu)造方法和禁止調(diào)用該類原有的構(gòu)造函數(shù)。最后再看一個(gè)例子:

  1. <?php  
  2. /**  
  3. * Testing enforcing the type to "array" like the "enforceTypes"  
  4. * method does via type hinting  
  5. */ 
  6. public function ttestReallyLongRunBuilderConstraint()  
  7. {  
  8. $stub = $this->getMock('Database',array('reallyLongTime'));  
  9. $stub->expects($this->any())  
  10. ->method('reallyLongTime')  
  11. ->with($this->isType('array'));  
  12. $arr = array('test');  
  13. $this->assertTrue($stub-> reallyLongTime ($arr));  
  14. }  
  15. ?> 

在這里,我們使用了with方法,其中這個(gè)方法中指定了要傳入的參數(shù)類型為array數(shù)組類型,最后這個(gè)斷言是通過了,因?yàn)榉祷氐牡拇_是數(shù)組類型。
 

【編輯推薦】

  1. PHP單元測(cè)試?yán)鳎篜HPUnit深入用法
  2. 初探PHP單元測(cè)試?yán)鳎篜HPUnit
  3. PHP開發(fā)者工資翻倍需做到的5件事
  4. PHP企業(yè)級(jí)應(yīng)用之常見緩存技術(shù)深入解讀
  5. PHP 5.3.6 RC1發(fā)布 修復(fù)多處BUG(附下載)
責(zé)任編輯:陳貽新 來源: it168
相關(guān)推薦

2011-02-16 09:45:13

PHPPHPUnit

2011-02-15 10:05:48

PHPPHPUnit

2024-08-15 08:11:10

2011-08-22 13:57:55

gtest

2017-01-14 23:42:49

單元測(cè)試框架軟件測(cè)試

2011-11-18 15:18:41

Junit單元測(cè)試Java

2017-04-07 13:45:02

PHP單元測(cè)試數(shù)據(jù)庫測(cè)試

2009-11-16 17:20:04

PHP多維數(shù)組排序

2010-06-28 10:12:01

PHP匿名函數(shù)

2009-11-18 12:38:04

PHP字符串函數(shù)

2024-04-26 11:14:34

C#單元測(cè)試框架

2010-06-01 15:25:27

JavaCLASSPATH

2016-12-08 15:36:59

HashMap數(shù)據(jù)結(jié)構(gòu)hash函數(shù)

2020-07-21 08:26:08

SpringSecurity過濾器

2017-01-14 23:26:17

單元測(cè)試JUnit測(cè)試

2017-01-16 12:12:29

單元測(cè)試JUnit

2015-12-02 10:52:11

PHPUnitWindows配置

2020-08-18 08:10:02

單元測(cè)試Java

2017-03-23 16:02:10

Mock技術(shù)單元測(cè)試

2021-05-05 11:38:40

TestNGPowerMock單元測(cè)試
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)