关键词: simpletest phpsa 单元测试
author: hszhl
simpletest 是一套非常实用的测试工具,其中的webTest支持可以对web程序界面的测试,simpletest更多介绍请参看它的官方网站http://simpletest.org ;
在这里我以simpletest这个单元测试框架来对phpsa 进行单元测试,在php领域中,还有PHPUNIT,PHPUNIT2 等单元测试工具;详情可参看它们的官方网站
simpletest提供unitTestCase 、webTestCase 等测试工能,本例中我们采用unitTestCase来讲解;
我们在phpsa的根目录建立一个tests目录,把simpletest解压到这个目录里,同时新建一个phpsa_unit_test.inc.php(来源于原phpsa/test里录里的phpsa_unit_test.inc.php)
内容为:
$_SERVER['DOCUMENT_ROOT'] = '..';
//require_once('../PEAR/PHPUnit/Framework/TestCase.php');
require_once ('../comm/config/config.class.php');
require_once ('../comm/config/constants.class.php');
require_once ('../comm/utils/classLoader.class.php');
require_once ('../comm/object.class.php');
?>
进行测试的一个好习惯,就是把所有的测试用例都直接放在项目根目录之下的“tests/”子目录中。(这种习惯有一个问题,就是在测试环境下,可能破坏任何依赖于从这个项目的根目录相对包含的代码。不过,这个问题可以通过给包含路径增加父目录“../”来解决。)
现在来介绍如何用simpletest对phpsa进行单元测试,以测试phpsa中的action类为例先看代码:
//定义simpletest包含路径
if (!defined('SIMPLE_TEST')) define('SIMPLE_TEST', 'simpletest/');
/**
* SimpleTest includes
*/
require_once SIMPLE_TEST.'unit_tester.php';
require_once SIMPLE_TEST.'reporter.php';
require_once SIMPLE_TEST.'mock_objects.php';
require_once SIMPLE_TEST.'web_tester.php';
/**/
//action运行的一些必需文件
require_once('phpsa_unit_test.inc.php');
//加载待测试的文件
require_once ('../comm/action/action.class.php');
//编写测试类
class TestUnit extends UnitTestCase{
//继承unittestcas父类
function TestUnit($name=''){
$this->UnitTestCase($name);
}
function setUp(){}
function TestAction(){
$form = null;
$moduleName = 'news';
$actionName = 'add';
$testAction = new TestAction($form, $moduleName, $actionName);
$this->assertEqual('news', $testAction->getModuleName());
$this->assertEqual($testAction->getActionName(), 'add');
$this->assertEqual($testAction->getDoActionMethodName(), 'doAddAction');
$this->assertEqual($testAction->getDoViewMethodName(), 'doAddView');
//$this->assertEqual($testAction->getDoViewMethodName(), 'doDelView');
$this->assertNull($testAction->getForm());
echo "view template type: " . $testAction->getViewTemplateType() . "\n";
echo "
";
echo "view mime type: " . $testAction->getMimeName() . "\n
";
}
function tearDown(){}
}
//由于action是一个abstract类,所以我们以继承方式对它进行测试
/**
* test action
*/
class TestAction extends Action {
public function TestAction($form = null, $moduleName = '', $actionName = '') {
parent::Action($form, $moduleName, $actionName);
}
public function doTestView() {
echo "get test";
}
public function doTestAction() {
echo "post test";
}
}
//测试开始
$test = new TestUnit('test Action start');
//测试输出
if (TextReporter::inCli()) {
exit ($test->run(new TextReporter()) ? 0 : 1);
}
$test->run(new HtmlReporter());
?>
来让我们来运行一下,效果如下:
我们的目标就是看到绿色 ,如果条是绿色的,代码就是正确的;
如果我们把这行(//$this->assertEqual($testAction->getDoViewMethodName(), 'doDelView'); )的注释去掉, 在看下效果:
看到了吧,出错了,那就说明,action是按moduleName 和actionName来执行程序的;并不执行没有分发到的action;在来看下类TestUnit 中用到一些方法,simpletest 是以'test'为默认字符开头的方法执行,也就是说凡是testX 的格式的方法都会在测试类内默认执行; 其它的方法:setUp():每一个test之前先运行;tearDown():每个test之后在运行; 在这里只介绍了两种,更具体的方法请参看http://simpletest.org/en/unit_test_documentation.html
simpletest的例子就先介绍到这里,关于测试的介绍也可以参看PHP设计模式指南的 第一章 编程习惯和附录 B 测试实践 这两节, 也希望更多的网友对phpsa进行测试并与大家分享!
用simpletest对phpsa进行测试
关键词: simpletest phpsa 单元测试
author: hszhl
simpletest 是一套非常实用的测试工具,其中的webTest支持可以对web程序界面的测试,simpletest更多
介绍请参看它的官方网站http://simpletest.org ;
在这里我以simpletest这个单元测试框架来对phpsa 进行单元测试,在php领域中,还有PHPUNIT,
PHPUNIT2 等单元测试工具;详情可参看它们的官方网站
simpletest提供unitTestCase 、webTestCase 等测试工能,本例中我们采用unitTestCase来讲解;
我们在phpsa的根目录建立一个tests目录,把simpletest解压到这个目录里,同时新建一个
phpsa_unit_test.inc.php(来源于原phpsa/test里录里的phpsa_unit_test.inc.php)
内容为:
$_SERVER['DOCUMENT_ROOT'] = '..';
//require_once('../PEAR/PHPUnit/Framework/TestCase.php');
require_once ('../comm/config/config.class.php');
require_once ('../comm/config/constants.class.php');
require_once ('../comm/utils/classLoader.class.php');
require_once ('../comm/object.class.php');
?>
进行测试的一个好习惯,就是把所有的测试用例都直接放在项目根目录之下的“tests/”子目录中。(这
种习惯有一个问题,就是在测试环境下,可能破坏任何依赖于从这个项目的根目录相对包含的代码。不
过,这个问题可以通过给包含路径增加父目录“../”来解决。)
现在来介绍如何用simpletest对phpsa进行单元测试,以测试phpsa中的action类为例先看代码:
文件名: test.php
//定义simpletest包含路径
if (!defined('SIMPLE_TEST')) define('SIMPLE_TEST', 'simpletest/');
/**
* SimpleTest includes
*/
require_once SIMPLE_TEST.'unit_tester.php';
require_once SIMPLE_TEST.'reporter.php';
require_once SIMPLE_TEST.'mock_objects.php';
require_once SIMPLE_TEST.'web_tester.php';
/**/
//action运行的一些必需文件
require_once('phpsa_unit_test.inc.php');
//加载待测试的文件
require_once ('../comm/action/action.class.php');
//编写测试类
class TestUnit extends UnitTestCase{
//继承unittestcas父类
function TestUnit($name=''){
$this->UnitTestCase($name);
}
function setUp(){}
function TestAction(){
$form = null;
$moduleName = 'news';
$actionName = 'add';
$testAction = new TestAction($form, $moduleName, $actionName);
$this->assertEqual('news', $testAction->getModuleName());
$this->assertEqual($testAction->getActionName(), 'add');
$this->assertEqual($testAction->getDoActionMethodName(), 'doAddAction');
$this->assertEqual($testAction->getDoViewMethodName(), 'doAddView');
//$this->assertEqual($testAction->getDoViewMethodName(), 'doDelView');
$this->assertNull($testAction->getForm());
echo "view template type: " . $testAction-
>getViewTemplateType() . "\n";
echo "
";
echo "view mime type: " . $testAction->getMimeName() .
"\n
";
}
function tearDown(){}
}
//由于action是一个abstract类,所以我们以继承方式对它进行测试
/**
* test action
*/
class TestAction extends Action {
public function TestAction($form = null, $moduleName = '', $actionName = '') {
parent::Action($form, $moduleName, $actionName);
}
public function doTestView() {
echo "get test";
}
public function doTestAction() {
echo "post test";
}
}
//测试开始
$test = new TestUnit('test Action start');
//测试输出
if (TextReporter::inCli()) {
exit ($test->run(new TextReporter()) ? 0 : 1);
}
$test->run(new HtmlReporter());
?>
来让我们来运行一下,效果如下:
我们的目标就是看到绿色 ,如果条是绿色的,代码就是正确的;
如果我们把这行(//$this->assertEqual($testAction->getDoViewMethodName(), 'doDelView'); )的注
释去掉, 在看下效果:
看到了吧,出错了,那就说明,action是按moduleName 和actionName来执行程序的;并不执行没有分发
到的action;在来看下类TestUnit 中用到一些方法,simpletest 是以'test'为默认字符开头的方法执行
,也就是说凡是testX 的格式的方法都会在测试类内默认执行; 其它的方法:setUp():每一个test之前先
运行;tearDown():每个test之后在运行; 在这里只介绍了两种,更具体的方法请参看
http://simpletest.org/en/unit_test_documentation.html
simpletest的例子就先介绍到这里,关于测试的介绍也可以参看PHP设计模式指南的 第一章 编程习惯和
附录 B 测试实践 这两节, 也希望更多的网友对phpsa进行测试并与大家分享!