Skip to content


测试驱动开发之_simpleTest

这几天在看<<重构>>,这是一本与<<设计模式>>齐名的好书.主张多修改,多测试.很遗憾写了这么长时间的代码.unit test一直都是自己编写.很少用单元测试工具.这回也来学习一下别人的测试框架的优秀之处。

问了一下google老师,好像目前流行的php测试工具有以下两个

1. phpunit
2. simpleTest

大概看了一下两者的介绍. 本人对simpleTest比较感兴趣,主要是它很快的让我上手了(或许他的网站介绍做得好,呵呵).

直奔主题:下载 simpleTest的源代码 (当前最新的版本).

我比较显示偷看源代码,所以我通常会把代码解压后为代码建一个项目来管理,这样不仅便于观察其实现原理,而且eclipse的自动联想功能会让你提高开发效率。

如果你的IDE是eclipse,恭喜你simpletest有此IDE的插件,用起来十分方便,安装也超级简单.这插件下载地址 eclipse plugin. 用eclipse的自动扩展插件的功能更是方便到家.

url是 http://simpletest.org/eclipse/.

下面是安装截图:

(1)填写更新源,并给插件命名

simpletest安装1

(2)设置simpleTest的属性:包括php以及php配置文件的位置,simpletest源代码的位置.只要填好这几个就可以了.

simpletest安装2

(3)新建一个项目命名这test.写测试代码.

require_once(’simpletest/autorun.php’);
require_once(’simpletest/web_tester.php’); //用于网页测试

class MySiteTest extends WebTestCase {

function testHomePage() {
$this->get(’http://re.xianguo.com’);
$this->assertTitle(’鲜果热文’,'isright’); //判断title
$this->clickLink(’Contact’);
$this->assertTitle(’Contact me’);
$this->assertPattern(’/Email me at/’);
}

}

class TestOfSessionLogging extends UnitTestCase {
function setUp() { //调用测试代码前
echo ‘test before’;
}

function tearDown() { //调用测试代码后
echo ‘test after’;
}

function testEqual() { //测试实体
$this->assertEqual(1,2, “is equal”);
}

function testTrue(){
$this->assertTrue(0,’is true’);
}
}
?>

(4) 用eclipse查看测试结果
simpletest安装3
(5) 查看测试结果
simpletest安装4
结果中描述了测试错误的代码函数的位置.

哈哈!是不是十分的方便快捷,还不快点去试一下.
除了上面两大常用单元测试类外,还有组测试等类.用起超爽呀.官方详细介绍

Posted in 重构.

Tagged with , , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. Heartshare » Blog Archive » Introduction to PHP Coding Standards linked to this post on 2008年09月28日

    [...] 如果你的IDE是Eclipse,那么你可以很方便的进行代码测试,使用SimpleTest参考http://www.guogoul.com/2008/05/19/simpletest_1/ [...]

  2. PHP编程规范 « 爱维尼 linked to this post on 2009年06月8日

    [...] 如果你的IDE是Eclipse,那么你可以很方便的进行代码测试,使用SimpleTest参考http://www.guogoul.com/2008/05/19/simpletest_1/ 说明: 本规范由EasyChen [...]



Some HTML is OK

or, reply to this post via trackback.