TDD 测试工具 (原创)

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

调试PHP代码工具,此处用于生成代码覆盖率

如何安装请参考此文章

http://www.phpddt.com/php/xdebug.html

参考文档:

官网:http://phpunit.de/manual/current/en/

PHPunit安装:

http://phpunit.de/manual/current/en/installation.html

PHPunit 依赖包:

http://pear.phpunit.de/

安装错误解决:

http://blog.sina.com.cn/s/blog_46d39f4801014jsn.html

2、执行PHPunit 命令

创建一个简单测试文件:LoginTest.PHP内容如下

<?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));

}

}

?>

在命令行输入: PHPunit LoginTest.PHP

PHPunit 命令参数介绍请参考其他文章

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、自动

PHPunit 配置文件为:PHPunit.xml.

下面通过.xml 文件来介绍一下PHPunit.xml标签用法与含义

<?xml version="1.0" encoding="UTF-8"?>

<!-- 自动化单元测试,版本PHPunit3.7 此文件支持serivce 测试 -->

<PHPunit bootstrap="/test/Web/ServiceInit.PHP">

<!-- 套件测试

suffix:后缀

PHPVersionPHP 版本

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>

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...