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

PHP單元測試?yán)鳎篜HPUnit深入用法

開發(fā) 后端
本文進(jìn)一步探討了PHP單元測試?yán)鳎篜HPUnit中一些重要的方法和斷言,PHPUnit中還有大量豐富的斷言,對提高單元測試十分有幫助,具體的請參考PHPUnit用戶手冊。

在上一篇初探PHP單元測試?yán)鳎篜HPUnit文章中,我們對PHPUnit有了一個初步的認(rèn)識,在本文中將繼續(xù)深入講解下PHPUnit中的一些用法。

1、markTestSkipped和markTestIncomplete

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

  1. <?php  
  2. public function testThisMightHaveADb()  
  3. {  
  4.   $myObject->createObject();  
  5.   try {  
  6.     $db = new Database();  
  7.     $this->assertTrue($db->rowExists());  
  8.   } catch (DatabseException $e) {  
  9.     $this->markTestSkipped('This test was skipped because there was a database problem');  
  10.   }  
  11. }  
  12. ?> 

在上面的程序中,是一個連接數(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這個測試方法還沒編寫完成而已,例子如下:

  1. <?php  
  2. public function testAreNotEnoughHours()  
  3. {  
  4.   $this->markTestIncomplete("There aren't enough hours in the day to have my tests go green");  
  5.   $trueVariable = true;  
  6.   $this->assertTrue($trueVariable);  
  7. }  
  8. ?> 

2、更深入了解PHPUnit中的斷言

在上一篇文章中,已經(jīng)基本講解了一些基本的PHPUnit中的斷言的使用,這里以一個例子,下面是一個類的代碼:

  1. <?php  
  2. class Testable  
  3. {  
  4.   public $trueProperty = true;  
  5.   public $resetMe = true;  
  6.   public $testArray = array(  
  7.     'first key' => 1,  
  8.     'second key' => 2  
  9.   );  
  10.   private $testString = "I do love me some strings";  
  11.   public function __construct()  
  12.   {  
  13.   }  
  14.   public function addValues($valueOne,$valueTwo) {  
  15.     return $valueOne+$valueTwo;  
  16.   }  
  17.   public function getTestString()  
  18.   {  
  19.     return $this->testString;  
  20.   }  
  21. }  
  22. ?> 

我們編寫的單元測試代碼初步的框架如下:

  1. <?php  
  2. class TestableTest extends PHPUnit_Framework_TestCase  
  3. {  
  4.   private $_testable = null;  
  5.   public function setUp()  
  6.   {  
  7.     $this->_testable = new Testable();  
  8.   }  
  9.   public function tearDown()  
  10.   {  
  11.     $this->_testable = null;  
  12.   }  
  13.   /** test methods will go here */ 
  14. }  
  15. ?> 

在上一篇文章中,已經(jīng)介紹了setUp方法和tearDown方法,這里的setUp方法中,建立了Testable()實(shí)例并保存在變量$_testable中,而在tearDown方法中,銷毀了該對象。

接下來,開始編寫一些斷言去測試,首先看assertTrue和assertFalase:

  1. <?php 
  2. public function testTruePropertyIsTrue()  
  3. {  
  4.   $this->assertTrue($this->_testable->trueProperty,"trueProperty isn't true");  
  5. }  
  6. public function testTruePropertyIsFalse()  
  7. {  
  8.   $this->assertFalse($this->_testable->trueProperty, "trueProperty isn't false");  
  9. }  
  10. ?> 

在上一篇文章中已經(jīng)介紹過assertTrue和assertFalse了,這里留意一下其中的第二個參數(shù),其含義是,當(dāng)該斷言的測試不通過時,自定義的顯示信息。比如在這個測試方法中,當(dāng)trueProperty不為真值時,將顯示“trueProperty isn't true”的信息。

接下來再看下在數(shù)值方面上PHPUnit的斷言使用實(shí)例:

  1. <?php  
  2. public function testValueEquals()  
  3. {  
  4.   $valueOne = 4;  
  5.   $valueTwo = 2;  
  6.   $this->assertEquals($this->_testable->addValues($valueOne,$valueTwo),6);  
  7. }  
  8. public function testValueGreaterThan()  
  9. {  
  10.   $valueOne = 4;  
  11.   $valueTwo = 2;  
  12.   $this->assertGreaterThan($valueTwo,$valueOne);  
  13. }  
  14. public function testLessThanOrEqual()  
  15. {  
  16.   $valueOne = 4;  
  17.   $valueTwo = 2;  
  18.   $this->assertLessThanOrEqual($valueTwo,$valueOne);  
  19. }  
  20. public function testAreObjectsEqual()  
  21. {  
  22.   $testTwo = new Testable();  
  23.   $this->_testable->resetMe = false;  
  24.   $this->assertEquals($this->_testable,$testTwo);  
  25. }  
  26. ?> 

其中,assertEquals為判斷是否相等,assertGreaterThan為判斷是否大于,assertLessThanOrEqual判斷是否小于或等于,而assertEquals這里要注意一下,它還可以用來判斷兩個對象是否相等,比如這里就判斷了$testTwo這個Testable類的實(shí)例是否和新設(shè)置的resetMe這個對象相等。

除了在數(shù)值方面的斷言外,在字符方面還有一些很多斷言的功能,看下面的代碼:

  1. <?php 
  2. public function testStringEnding()  
  3. {  
  4.   $testString = $this->_testable->getTestString();  
  5.   $this->assertStringEndsWith('frood',$testString);  
  6. }  
  7. public function testStringStarts()  
  8. {  
  9.   $testString = $this->_testable->getTestString();  
  10.   $this->assertStringStartsWith('hoopy',$testString);  
  11. }  
  12. public function testEqualFileContents()  
  13. {  
  14.   $this->assertStringEqualsFile('/path/to/textfile.txt','foo');  
  15. }  
  16. public function testDoesStringMatchFormat()  
  17. {  
  18.   $testString = $this->_testable->getTestString();  
  19.   $this->assertStringMatchesFormat('%s',$testString);  
  20. }  
  21. ?> 

其中, assertStringStartsWith斷言是判斷字符串是否以指定的字符串開頭,assertStringEndsWith斷言判斷字符串是否以指定的字符串結(jié)尾。assertStringEqualsFile斷言判斷給定的文件中是否含有指定的字符,比如這里就判斷textfile.txt這個文件中是否包含字符串foo。

而assertStringMatchesFormat可以讓用戶指定匹配的模式去判斷一個字符串是否符合要求,如 $this->assertStringMatchesFormat('%s',$testString);

這里則判斷$testString是否是字符串類型,具體的可以參考PHPUnit手冊。

再來看如下的代碼:

  1. <?php 
  2. public function testStringIsNotNull()  
  3. {  
  4.   $notANull = “i'm not a null!”;  
  5.   $this->assertNull($notANull);  
  6. }  
  7. public function testStringIsSame()  
  8. {  
  9.   $numberAsString = '1234';  
  10.   $this->assertSame(1234,$numberAsString);  
  11. }  
  12. ?> 

其中assertNull判斷某個變量是否為null,而assertSame則嚴(yán)格判斷兩個變量是否同一個類型,盡管在PHP中是弱類型語言,但這里通過assertSame還是能判斷出$numberAsString為字符串類型,跟期望的1234數(shù)字類型不匹配,所以測試不能通過。

***我們來看一下平??赡懿淮蟪S玫臄嘌裕挚赡軐δ愕膯卧獪y試工作十分有幫助的,先看代碼如下:

  1. <?php 
  2. public function testArrayKeyExists()  
  3. {  
  4.     $this->assertArrayHasKey('first key',$this->_testable->testArray);  
  5. }  
  6. public function testAttributeExists()  
  7. {  
  8.     $this->assertClassHasAttribute('resetMe',get_class($this->_testable));  
  9. }  
  10. public function testFileIsReal()  
  11. {  
  12.     $this->assertFileExists('/path/to/file.txt');  
  13. }  
  14. public function testIsInstance()  
  15. {  
  16.     $this->assertInstanceOf('OtherClass',$this->_testable);  
  17. }  
  18. <?php 
  19. public function testDoesMatchRegex()  
  20. {  
  21.   $testString = $this->_testable->getTestString();  
  22.   $this->assertRegExp('/[a-z]+/',$testString);  
  23. }  
  24. ?> 

代碼中***個斷言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

【編輯推薦】

  1. 初探PHP單元測試?yán)鳎篜HPUnit
  2. PHP開發(fā)者工資翻倍需做到的5件事
  3. PHP企業(yè)級應(yīng)用之常見緩存技術(shù)深入解讀
  4. PHP與Java在Web開發(fā)方面的比較
  5. Web開發(fā)者必備:21個超實(shí)用PHP代碼
責(zé)任編輯:陳貽新 來源: it168
相關(guān)推薦

2011-02-21 09:54:14

PHPPHPUnit

2011-02-15 10:05:48

PHPPHPUnit

2017-01-14 23:42:49

單元測試框架軟件測試

2011-11-18 15:18:41

Junit單元測試Java

2017-04-07 13:45:02

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

2024-04-26 11:14:34

C#單元測試框架

2024-08-15 08:11:10

2017-01-14 23:26:17

單元測試JUnit測試

2017-01-16 12:12:29

單元測試JUnit

2015-12-02 10:52:11

PHPUnitWindows配置

2020-08-18 08:10:02

單元測試Java

2017-03-23 16:02:10

Mock技術(shù)單元測試

2021-05-05 11:38:40

TestNGPowerMock單元測試

2023-07-26 08:58:45

Golang單元測試

2011-07-04 18:16:42

單元測試

2020-05-07 17:30:49

開發(fā)iOS技術(shù)

2023-10-07 09:04:31

FastAPI單元測試

2011-05-16 16:52:09

單元測試徹底測試

2011-04-18 13:20:40

單元測試軟件測試

2017-02-23 15:59:53

測試MockSetup
點(diǎn)贊
收藏

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