PHP单元测试利器:PHPUnit深入用法

开发 后端
本文进一步探讨了PHP单元测试利器:PHPUnit中一些重要的方法和断言,PHPUnit中还有大量丰富的断言,对提高单元测试十分有帮助,具体的请参考PHPUnit用户手册。

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

1、markTestSkipped和markTestIncomplete

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

  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. ?> 

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

而markTestIncomplete也有点类似,但有点不同的是,它是当开发者在编写一个未完成的测试方法时使用的,标记出某个测试方法还没编写完成,同样测试结果也不会是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中的断言

在上一篇文章中,已经基本讲解了一些基本的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. ?> 

在上一篇文章中,已经介绍了setUp方法和tearDown方法,这里的setUp方法中,建立了Testable()实例并保存在变量$_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. ?> 

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

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

  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类的实例是否和新设置的resetMe这个对象相等。

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

  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断言判断字符串是否以指定的字符串结尾。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则严格判断两个变量是否同一个类型,尽管在PHP中是弱类型语言,但这里通过assertSame还是能判断出$numberAsString为字符串类型,跟期望的1234数字类型不匹配,所以测试不能通过。

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

  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,是用来检查一个数组中是否每个键值都是存在的,比如我们的数组中,“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代码
责任编辑:陈贻新 来源: it168
相关推荐

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单元测试数据库测试

2017-01-14 23:26:17

单元测试JUnit测试

2017-01-16 12:12:29

单元测试JUnit

2020-08-18 08:10:02

单元测试Java

2017-03-23 16:02:10

Mock技术单元测试

2023-07-26 08:58:45

Golang单元测试

2015-12-02 10:52:11

PHPUnitWindows配置

2021-05-05 11:38:40

TestNGPowerMock单元测试

2011-07-04 18:16:42

单元测试

2020-05-07 17:30:49

开发iOS技术

2023-10-07 09:04:31

FastAPI单元测试

2011-05-16 16:52:09

单元测试彻底测试

2012-05-17 09:09:05

Titanium单元测试

2009-09-25 10:33:25

Hibernate单元

2011-04-18 13:20:40

单元测试软件测试

2009-09-01 10:20:06

protected方法单元测试
点赞
收藏

51CTO技术栈公众号