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://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://blog.youkuaiyun.com/chinabluexfw/article/details/7484742
参考文档:
参数介绍:
http://blog.youkuaiyun.com/chinabluexfw/article/details/7484742
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:后缀
phpVersion:php 版本
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>