PHPUnit学习04---PHPUnit实用技巧收录

简介:

本文目的

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

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

 

函数依赖测试

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

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

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

clip_image002

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

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

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

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

clip_image002[5]

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

 

数据提供函数

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

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

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

clip_image002[9]

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

 

异常测试

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

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

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

 

跳过忽略测试

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

方法

意义

void markTestSkipped()

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

void markTestSkipped(string $message)

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

void markTestIncomplete

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

void markTestIncomplete(string $message)

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

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

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

输出结果如下

clip_image002[11]

 

自动生成测试框架

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

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

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

使用如下命令:

clip_image002[13]

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

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

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

参考文档

本文转自bourneli博客园博客,原文链接:http://www.cnblogs.com/bourneli/archive/2012/09/08/2676978.html ,如需转载请自行联系原作者
相关文章
|
10月前
|
程序员 PHP 数据安全/隐私保护
【实用教程】掌握这款神器,PHP中的goto源码解密轻松搞定!
PHP中的goto代码一直是程序员们头疼的难题,但是现在有一款神奇的解密工具可以帮助你轻松解决这个问题。
|
11月前
|
SQL 前端开发 JavaScript
[从0开始]PHP+phpstudy留言板项目搭建教程及报错详析
PHP是在服务器端执行的脚本语言,适用于Web开发并可嵌入HTML中。 学习网站:PHP教程 | 菜鸟教程
162 0
|
11月前
|
JSON 监控 Ubuntu
开心档-软件开发入门之Python uWSGI 安装配置
本文主要介绍如何部署简单的 WSGI 应用和常见的 Web 框架。 以 Ubuntu/Debian 为例,先安装依赖包
|
开发工具 git
gitbook docs 序言
gitbook docs 序言
|
SEO
easyswoole项目示例
easyswoole项目示例
90 0
Hexo、Jekyll、Sphinx、mkdocs、docsify等静态博文档汇总
Hexo、Jekyll、Sphinx、mkdocs、docsify等静态博文档汇总
176 0
Hexo、Jekyll、Sphinx、mkdocs、docsify等静态博文档汇总
|
PHP
ThinkPHP源码阅读最佳工具debug_backtrace(1)
ThinkPHP源码阅读最佳工具debug_backtrace
138 0
ThinkPHP源码阅读最佳工具debug_backtrace(1)
|
Web App开发 编解码 PHP
PHP大法——实验吧
今天有点时间就多写几篇解题思路吧, 希望能够帮助到那些需要帮助的人, 所有的wp都是以一题一篇的形式写出 主要是为了能够让读者更好的阅读以及查找, 希望你们不要责怪!!共勉!!! 这一题做的时间比较久,但是收获颇多!!!! 永远爱你们的————新宝宝 PHP大法分值:20 ...
1424 0
|
Web App开发 编解码 PHP
CTF---Web入门第十一题 PHP大法
PHP大法分值:20 来源: DUTCTF 难度:中 参与人数:8205人 Get Flag:2923人 答题人数:3042人 解题通过率:96% 注意备份文件 解题链接: http://ctf5.
1588 0