PHP单元测试利器:PHPUnit深入用法
创始人
2024-07-21 01:30:30
0

在上一篇初探PHP单元测试利器:PHPUnit文章中,我们对PHPUnit有了一个初步的认识,在本文中将继续深入讲解下PHPUnit中的一些用法。

1、markTestSkipped和markTestIncomplete

在PHPUnit中,有两个有用的方法markTestSkipped和markTestIncomplete。它们能允许你编写的单元测试中不单是只有通过和失败两种结果。markTestSkipped能让PHPUnit不去执行某个已经编写好的测试方法。举个例子说明,比如下面的程序:

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

在上面的程序中,是一个连接数据库后,判断数据是否存在的测试方法,但如果考虑数据库的连接异常的话,则应该在抛出异常时,使用markTestSkipped指出该测试方法应该是被忽略的,因为出现了异常,而注意的时,此时有可能你写的代码是正确的,只不过是出现了异常而已,这样PHPUnit在输出时就不会只是简单的输出fail。

而markTestIncomplete也有点类似,但有点不同的是,它是当开发者在编写一个未完成的测试方法时使用的,标记出某个测试方法还没编写完成,同样测试结果也不会是fail,只是告诉PHPUnit这个测试方法还没编写完成而已,例子如下:

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

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

在上一篇文章中,已经基本讲解了一些基本的PHPUnit中的断言的使用,这里以一个例子,下面是一个类的代码:

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

我们编写的单元测试代码初步的框架如下:

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

在上一篇文章中,已经介绍了setUp方法和tearDown方法,这里的setUp方法中,建立了Testable()实例并保存在变量$_testable中,而在tearDown方法中,销毁了该对象。

接下来,开始编写一些断言去测试,首先看assertTrue和assertFalase:

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

在上一篇文章中已经介绍过assertTrue和assertFalse了,这里留意一下其中的第二个参数,其含义是,当该断言的测试不通过时,自定义的显示信息。比如在这个测试方法中,当trueProperty不为真值时,将显示“trueProperty isn't true”的信息。

接下来再看下在数值方面上PHPUnit的断言使用实例:

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

其中,assertEquals为判断是否相等,assertGreaterThan为判断是否大于,assertLessThanOrEqual判断是否小于或等于,而assertEquals这里要注意一下,它还可以用来判断两个对象是否相等,比如这里就判断了$testTwo这个Testable类的实例是否和新设置的resetMe这个对象相等。

除了在数值方面的断言外,在字符方面还有一些很多断言的功能,看下面的代码:

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

其中, assertStringStartsWith断言是判断字符串是否以指定的字符串开头,assertStringEndsWith断言判断字符串是否以指定的字符串结尾。assertStringEqualsFile断言判断给定的文件中是否含有指定的字符,比如这里就判断textfile.txt这个文件中是否包含字符串foo。

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

这里则判断$testString是否是字符串类型,具体的可以参考PHPUnit手册。

再来看如下的代码:

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

其中assertNull判断某个变量是否为null,而assertSame则严格判断两个变量是否同一个类型,尽管在PHP中是弱类型语言,但这里通过assertSame还是能判断出$numberAsString为字符串类型,跟期望的1234数字类型不匹配,所以测试不能通过。

***我们来看一下平常可能不大常用的断言,但又可能对你的单元测试工作十分有帮助的,先看代码如下:

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

代码中***个断言assertArrayHasKey,是用来检查一个数组中是否每个键值都是存在的,比如我们的数组中,“firstkey”这个值是有键1与其对应的,所以测试能通过。而assertClassHasAttribute则能判断某个类是否有相应的属性,这个例子中测试也能通过;

而assertFileExists则判断在本地文件系统中是否存在指定的文件。而assertInstanceOf则判断某个你正在创建的对象是否为某个类的实例。assertRegExp相信大家都知道,这个是判断某个字符串中是否与给定的正则表达式相匹配。

原文链接:http://tech.it168.com/a2011/0215/1157/000001157580_all.shtml

【编辑推荐】

  1. 初探PHP单元测试利器:PHPUnit
  2. PHP开发者工资翻倍需做到的5件事
  3. PHP企业级应用之常见缓存技术深入解读
  4. PHP与Java在Web开发方面的比较
  5. Web开发者必备:21个超实用PHP代码

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...