TDD 只是一种思想、设计方法论,需要很多工具支持以达到敏捷的效果,基本的测试工具有,比如PHPunit
1、PHPunit 3.7 安装
清除缓存
pear clear-cache
#(更新pear)
pearupgrade-all
#安装
pearchannel-discover pear.PHPunit.de
pearchannel-discover components.ez.no
pearchannel-discover pear.symfony-project.com
pearinstall --alldeps PHPunit/PHPUnit
查看版本,确定是否安装正确
PHPunit --version
安装其他依赖工具
curl:
打开PHP.ini配置,开启curl
extension=PHP_curl.dll
HTTP_Request2:
安装命令
pear install HTTP_Request2
PHPUnit_Selenium:
安装命令
pear install PHPunit/PHPUnit_Selenium
#操作数据库必备扩展 PHPUnit_Extensions_Database_TestCase
安装命令
pear install PHPunit/DbUnit
安装xdebug
如何安装请参考此文章
http://www.phpddt.com/php/xdebug.html
参考文档:
官网:http://phpunit.de/manual/current/en/
PHPunit安装:
http://phpunit.de/manual/current/en/installation.html
PHPunit 依赖包:
http://blog.sina.com.cn/s/blog_46d39f4801014jsn.html
2、执行PHPunit 命令
<?PHP
require_once'PHPUnit/Autoload.PHP';
class ArrayTest extends PHPUnit_Framework_TestCase
{
public function testArrayContainsAnElement()
{
// Create the Arrayfixture.
$fixture = array();
// Add an element to theArray fixture.
$fixture[] = 'Element';
// Assert that the sizeof the Array fixture is 1.
$this->assertEquals(1,sizeof($fixture));
}
}
?>
http://www.jb51.cc/article/p-oadsdeqm-my.html
参考文档:
参数介绍:
http://www.jb51.cc/article/p-oadsdeqm-my.html
PHPunit 官网:
http://phpunit.de/manual/current/en/
3、自动化
下面通过.xml 文件来介绍一下PHPunit.xml标签的用法与含义
<?xml version="1.0" encoding="UTF-8"?>
<!-- 自动化单元测试,版本PHPunit3.7 此文件只支持serivce 测试 -->
<PHPunit bootstrap="/test/Web/ServiceInit.PHP">
<!-- 套件测试
suffix:后缀
PHPVersionOperator:大于或小于符号 >= 或<=
<testsuites>
<testsuitename="ServiceSuite">
<directorysuffix="Test.PHP">/path/to/*Test.PHP files</directory>
<file PHPVersion="5.3.0"PHPVersionOperator=">=">/path/to/MyTest.PHP</file>
<exclude>/path/to/exclude</exclude>
</testsuite>
</testsuites>
-->
<!-- 测试 文件case -->
<testsuites>
<testsuite name="ServiceSuite">
<directory suffix="Test.PHP">test/Web/Service</directory>
</testsuite>
</testsuites>
<!--
覆盖率的测试文件,blacklist 黑名单(不需要统计覆盖率的文件),
whitelist 白名单(统计覆盖率的测试文件) 当黑名单与白名单文件重复时,白名单起作用
-->
<filter>
<!--
<blacklist>
<directorysuffix=".PHP">action</directory>
<file>ArrayTest.PHP</file>
</blacklist>
-->
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix="Service.class.PHP">src/Service</directory>
<!--
<file>ArrayTest.PHP</file>
//排除文件
<exclude>
<directorysuffix=".PHP">action/lib</directory>
<directorysuffix=".PHP">model</directory>
<file>action/lib/Loginxxx.PHP</file>
</exclude>
-->
</whitelist>
</filter>
<!-- 测试结果:代码覆盖率,测试结果
<logging>
<logtype="coverage-html" target="/tmp/report"charset="UTF-8"
highlight="false" lowUpperBound="35"highLowerBound="70"/>
<logtype="coverage-clover" target="/tmp/coverage.xml"/>
<logtype="coverage-PHP" target="/tmp/coverage.serialized"/>
<logtype="coverage-text" target="PHP://stdout"showUncoveredFiles="false"/>
<logtype="json" target="/tmp/logfile.json"/>
<logtype="tap" target="/tmp/logfile.tap"/>
<logtype="junit" target="/tmp/logfile.xml"logIncompleteSkipped="false"/>
<logtype="testdox-html" target="/tmp/testdox.html"/>
<logtype="testdox-text" target="/tmp/testdox.txt"/>
</logging>
-->
<logging>
<!-- target(report/html)生成html 文件的目录-->
<log type="coverage-html"target="test/Log/html"charset="UTF-8"yui="true"highlight="false"lowUpperBound="35"highLowerBound="70"/>
<!-- target(report/coverage/coverage.xml) 生成xml的文件名,生成的xml 用图标插件解析xml-->
<log type="coverage-clover"target="test/Log/coverage/coverage.xml"/>
</logging>
</PHPunit>