simpletest for 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)
內容爲:
<?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類爲例先看代碼:
<?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:<FONT  COLOR=#FF0000> " . $testAction->getViewTemplateType() . "</FONT>\n";
        echo "<br>";
        echo "view mime type: <FONT  COLOR=#FF0000>" . $testAction->getMimeName() . "</FONT>\n <br>";
 
  }

  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)
內容爲:
<?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
<?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:<FONT  COLOR=#FF0000> " . $testAction-

>getViewTemplateType() . "</FONT>\n";
        echo "<br>";
        echo "view mime type: <FONT  COLOR=#FF0000>" . $testAction->getMimeName() .

"</FONT>\n <br>";
 
  }

  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進行測試並與大家分享!




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章