phpunit 笔记

本文分享了作者从忽视到重视单元测试的心路历程,并深入介绍了 PHPUnit 的使用技巧,包括数据提供者、异常处理、代码覆盖率分析及自动化测试配置。

很久之前就想写单元测试了,但一直懒,但后来项目文件越来越大

发现当初真不该偷懒,现在开个新项目,这会可不在偷这个懒了,真可谓后患无穷...

久了没写phpunit,发现生疏了,看过了一遍手册,http://www.phpunit.de/manual/3.7/en/index.html 
写了点备份的笔记:

 

 

 数据提供者

为所有方法提供参数数据,循环提供
public static function data_return
使用在测试方法上,添加注释
/**
 * @dataProvider data_return
 */
直接抛出异常 (注释)
/**
 * @expectedException myexception
 */
或者使用
$this->setExpectedException();抛出异常
或直接抛出
测试返回
. 成功
F 失败
E 出错(异常,参数错误等等)
S 跳过
I 测试未实现
markTestIncomplete() 未完成测试
markTestSkipped()  跳过一个测试
PHPUnit_Framework_TestCase
每次运行测试都会调用
测试运行前调用
setUp()
测试结束后调用
tearDown() //一般做垃圾回收用
PHPUnit_Framework_TestSuite (用来组织testcase)
一个测试均会调用一次
测试运行前调用
setUp()
测试结束后调用
tearDown() //一般做垃圾回收用
可以添加测试方法(PHPUnit_Framework_TestCase)
addTestSuite();
模拟对象
========
//取得一个为kk的实例化对象,有s方法
$ob= $this->getMock("kk",array("s"));
//设置s方法的返回值
$ob->expects($this->any())
->method("s")
->will($this->returnValue(1));
//设置s方法会触发异常
$ob->expects($this->any())
->method("s")
->will($this->throwException(new Exception("hi")));
//设置s方法每次调用返回不同值
$ob->expects($this->any())
->method("s")
->will($this->onConsecutiveCalls());
代码覆盖率
=========
制定测试代码作用的方法
@covers class::method
测试时忽略分析的代码,写在代码中
// @codeCoverageIgnoreStart
$this->doSomethingElse();
// @codeCoverageIgnoreEnd
代码中的测试用例
===============
可以通过在代码注释中添加assert来该方法测试
/**
* @assert (0, 0) == 0
*/

 

运行时观察者接口

phpunit_Framework_TestListener

实现该接口,用于运行时回调,可在配置中配置或直接实例添加

 

使用实例

/Files/liushannet/code.rar  

 

命令参数说明:

 --log-*  日志文件格式及保存到

 --coverage-* 测试覆盖率及保存到

 --testdox-* 测试dox及保存到

 --filter 去除某些目录或文件

 --testsuite 指定suite

 --group 只执行指定组测试

--exclude-group 执行包该改组测试

--list-groups 列出所有组

--test-suffix 测试文件后缀

--bootstrap 测试前先执行的文件 (部署环境独立)

--c 配置文件 (详细介绍:http://www.phpunit.de/manual/current/en/appendixes.configuration.html)

--include-path 运行前先引入文件

 

配置 phpunit.xml 实现自动化测试

<!--
    This is an example phpunit.xml file to get you started
    Copy it to a directory, update the relative paths and rename to phpunit.xml
    Then to run tests cd into it's directory and just run
        phpunit
    (it'll automatically use any phpunit.xml file in the current directory)

    Any options you specify when calling phpunit will override the ones in here
-->
< phpunit  colors ="false"  bootstrap ="modules/unittest/bootstrap.php" >
     <!-- 测试列表 -->
     < testsuites >
         < testsuite  name ="Kohana Tests" >
             <!-- directory suffix="Test.php">/path/to/files</directory -->
             < file >modules/unittest/tests.php </ file >
         </ testsuite >
     </ testsuites >
     <!-- 执行的组 -->
     < groups >
       < include >
         < group >kohana.image </ group >
       </ include >
       <!-- exclude>
        <group>name</group>
      </exclude
-->
     </ groups >
     <!-- 日志记录 -->
     < logging >
       < log  type ="coverage-html"  target ="./report"  charset ="UTF-8" />
       < log  type ="testdox-html"  target ="report.html" />
     </ logging >
     <!-- 环境变量设置 -->
     <!-- php>
      <includePath>.</includePath>
      <ini name="foo" value="bar"/>
      <const name="foo" value="bar"/>
      <var name="foo" value="bar"/>
      <env name="foo" value="bar"/>
      <post name="foo" value="bar"/>
      <get name="foo" value="bar"/>
      <cookie name="foo" value="bar"/>
      <server name="foo" value="bar"/>
      <files name="foo" value="bar"/>
      <request name="foo" value="bar"/>
    </php
-->
     <!-- 代码覆盖的黑白名单 -->
     <!-- filter>
      <blacklist>
        <directory suffix=".php">/path/to/files</directory>
        <file>/path/to/file</file>
        <exclude>
          <directory suffix=".php">/path/to/files</directory>
          <file>/path/to/file</file>
        </exclude>
      </blacklist>
      <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">/path/to/files</directory>
        <file>/path/to/file</file>
        <exclude>
          <directory suffix=".php">/path/to/files</directory>
          <file>/path/to/file</file>
        </exclude>
      </whitelist>
    </filter
-->
     <!-- 添加运行时观察者实例,实现:PHPUnit_Framework_TestListener -->
     <!-- listeners>
      <listener class="MyListener" file="/optional/path/to/MyListener.php">
        <arguments>
          <array>
            <element key="0">
              <string>Sebastian</string>
            </element>
          </array>
          <integer>22</integer>
          <string>April</string>
          <double>19.78</double>
          <null/>
          <object class="stdClass"/>
        </arguments>
      </listener>
    </listeners
-->
</ phpunit >

 

加上自动部署:

http://www.cnblogs.com/liushannet/archive/2011/07/20/2111434.html

完美了.

转载于:https://www.cnblogs.com/liushannet/archive/2012/11/29/2794331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值