码迷,mamicode.com
首页 > Web开发 > 详细

PHPUnit-函数依赖-数据提供-异常-忽略-自动生成

时间:2017-03-28 13:52:00      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:cond   输入   编写   remove   标记   testcase   表示   lidar   unittest   

本文目的

本文目的是收录一些PHPUnit的有用技巧,这些技巧能够为给PHPUnit单元测试带来很多便利。本文将要介绍的技巧如下:

  • 函数依赖测试
  • 数据提供函数
  • 异常测试
  • 跳过忽略测试
  • 自动生成测试框架

 

函数依赖测试

有时候,类中的函数有依赖,而且你的逻辑需要被依赖函数正确执行,此时,你可以通过phpunit的依赖标签显示的标明这种依赖关系,如果任意被依赖的函数执行失败,那么依赖函数将会被自动跳过。如下所示代码(dependenceDemo.cpp):

 1 <?php
 2 class DependanceDemo extends PHPUnit_Framework_TestCase
 3 {
 4     public function testOne()
 5     {
 6         echo "testOne\n";
 7         $this->assertTrue(TRUE);
 8     }
 9      
10     public function testTwo()
11     {
12         echo "testTwo\n";
13         $this->assertTrue(FALSE);
14     }
15      
16     /**
17      * @depends testOne
18      * @depends testTwo
19      */
20     public function testThree()
21     {
22     }
23 }
24 ?>

 

上面的代码执行结果如下图:

技术分享

可以看到,testThree依赖testOne和testTwo,但是testTwo失败,所以testThree被跳过,使用S表示。

@depends标签还可以依赖返回值。如下例子所示(paramDependence.php),

 1 <?php
 2 class DependanceDemo extends PHPUnit_Framework_TestCase
 3 {
 4     public function testOne()
 5     {
 6         $this->assertTrue(TRUE);
 7         return "testOne";
 8     }
 9      
10     public function testTwo()
11     {
12         $this->assertTrue(TRUE);
13         return "testTwo";
14     }
15      
16     /**
17      * @depends testOne
18      * @depends testTwo
19      */
20     public function testThree($param1, $param2)
21     {
22         echo ‘First param:  ‘.$param1."\n";
23         echo ‘Second param: ‘.$param2."\n";
24     }
25 }
26 ?>

 

上面代码的执行结果如下:

技术分享

值得注意的是,函数的顺序与依赖标签的数序一致。

 

数据提供函数

函数一般会有多组不同的输入参数,如果每一组参数都写一个测试函数,那么写测试比较麻烦,如果能提供一种批量的参数输入方法,那么测试代码将会简洁许多。好在,phpunit提供@dataProvider标签,支持这种特性,看如下代码(dataProviderDemo.php):

 1 <?php
 2 class DataTest extends PHPUnit_Framework_TestCase
 3 {   
 4     /**
 5      * @dataProvider provider    
 6      */   
 7      public function testAdd($a, $b, $c)   
 8      {
 9         $this->assertEquals($c, $a + $b);
10     }    
11     public function provider()
12     {
13         return array(
14             array(0, 0, 0),
15             array(0, 1, 1),
16             array(1, 1, 1),
17             array(1, 2, 3)
18         );  
19     }
20 }?>

 

上面的代码输出如下所示:

技术分享

可以看到,函数testAdd遍历了函数provider的返回的结果,并将他们作为参数,被@dataProvider标记的函数的唯一要求就是返回数组。

 

异常测试

PHPUnit提供三种方法测试异常,如下面代码所示(exceptionsDemo.php):

 1 <?php
 2 class ExceptionsDemo extends PHPUnit_Framework_TestCase
 3 {
 4     /**
 5      * @expectedException InvalidArgumentException
 6      */
 7     public function testTagException()
 8     {  
 9         throw new InvalidArgumentException;
10     }
11      
12     public function testApiException()
13     {
14         $this->setExpectedException(‘InvalidArgumentException‘);
15         throw new InvalidArgumentException;
16     }
17      
18     public function testTryException()
19     {
20         try
21         {
22             throw new InvalidArgumentException;
23         }       
24         catch (InvalidArgumentException $expected)
25         {           
26             return;       
27         }        
28         $this->fail(‘An expected exception has not been raised.‘);
29     }
30 }
31 ?>

 

当然,这三种方法各有用处,效果等同,使用时看需要而定。

 

跳过忽略测试

在编写单元测试过程中,有时候只写出了测试方法名称,没有写具体的测试内容。这样,PHPUnit框架默认的认为此测试通过,这样,我们很可能忘记了该测试方法还没有实现,如果使用$this->fail(),只能表明该测试失败,但是该测试并没有失败,令人误解。所以,我们需要PHPUnit提供一组方法,使得可以跳过没有实现的测试,并且给与正确的提示。PHPUnit提供下面这四个方法,帮助我们办到这一点:

方法

意义

void markTestSkipped()

标记当前的测试被跳过,用“S”标记

void markTestSkipped(string $message)

标记当前的测试被跳过,用“S”标记,并且输出一段示消息

void markTestIncomplete

标记当前的测试不完全,用“I”标记

void markTestIncomplete(string $message)

标记当前的测试不完全,用“I”标记,并且输出一段提示消息

 

下面的代码演示了上面四个方法的使用(SIMarkDemo.php):

 1 <?php
 2 class SkipIncompleteMarkDemo extends PHPUnit_Framework_TestCase
 3 {
 4     public function testSkipped()
 5     {
 6         $this->markTestSkipped();
 7     }
 8      
 9     public function testSkippedWithMessage()
10     {
11         $this->markTestSkipped("this is a skipped test.");
12     }
13      
14     public function testIncomplete()
15     {
16         $this->markTestIncomplete();
17     }
18      
19     public function testIncompleteWithMessage()
20     {
21         $this->markTestIncomplete("this is a incomplete test.");
22     }
23 }
24 ?>

 

输出结果如下

技术分享

 

自动生成测试框架

在编写单元测试的时候,你会发现有些代码都是千篇一律的,比如testXXXX(){…..},所以基于这种考虑,PHPUnit提供了生成测试框架的命令。该命令可以给为被测试的类中的每一个方法生成一个默认的测试方法,该方法使用markTestIncomplete标记。

如下图面的代码表示的类,

 1 <?php
 2 class Calculator
 3 {   
 4     public function add($a, $b)
 5     {       
 6         return $a + $b;  
 7     }
 8      
 9     public function minus($a, $b) 
10     {       
11         return $a - $b;  
12     }
13 }
14 ?>

 

使用如下命令:

技术分享

将会生成一个类CalculatorTest.php,内容如下:

 1 <?php
 2 require_once ‘PHPUnit/Framework.php‘;
 3  
 4 require_once ‘/home/bourneli/test/UnitTestDemo/PHPUnitFeatures/Calculator.php‘;
 5  
 6 /**
 7  * Test class for Calculator.
 8  * Generated by PHPUnit on 2011-05-24 at 20:54:59.
 9  */
10 class CalculatorTest extends PHPUnit_Framework_TestCase
11 {
12     /**
13      * @var Calculator
14      */
15     protected $object;
16  
17     /**
18      * Sets up the fixture, for example, opens a network connection.
19      * This method is called before a test is executed.
20      */
21     protected function setUp()
22     {
23         $this->object = new Calculator;
24     }
25  
26     /**
27      * Tears down the fixture, for example, closes a network connection.
28      * This method is called after a test is executed.
29      */
30     protected function tearDown()
31     {
32     }
33  
34     /**
35      * @todo Implement testAdd().
36      */
37     public function testAdd()
38     {
39         // Remove the following lines when you implement this test.
40         $this->markTestIncomplete(
41           ‘This test has not been implemented yet.‘
42         );
43     }
44  
45     /**
46      * @todo Implement testMinus().
47      */
48     public function testMinus()
49     {
50         // Remove the following lines when you implement this test.
51         $this->markTestIncomplete(
52           ‘This test has not been implemented yet.‘
53         );
54     }
55 }
56 ?>

 

可以看到,该框架还是比较完整的,生成了setUp,tearDown函数,还为每一个函数生成了一个测试方法。当然,phpunit还提供替他框架函数,如果想要了解更多,可以参见参考文档中的链接。

 

 

参考文档


文章来源:http://www.cnblogs.com/bourneli/archive/2012/09/08/2676978.html

 

PHPUnit-函数依赖-数据提供-异常-忽略-自动生成

标签:cond   输入   编写   remove   标记   testcase   表示   lidar   unittest   

原文地址:http://www.cnblogs.com/bndong/p/6633720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!