Thursday, July 7. 2005

在PHP中使用SimpleTest进行单元测试

编程开发

SimpleTest是一个使用十分简单的单元测试工具。

一下内容是我自己设计的利用SimpleTest对一个项目进行单元测试的代码

一、文件结构

一个项目有多个文件夹,文件夹中还包含文件夹。我们要进行测试的代码即包含在这些文件夹中。要进行单元测试的情况,我认为有以下三种:

l         程序员进行某个函数、方法等的开发的时候,在开发完成时甚至开发过程中,边调试边进行测试。测试案例可能在进行开发的同时撰写,或者在项目的详细设计阶段即已经写好;

l         对一个模块(包含多个功能点)中的所有功能点进行一些集中的测试以检查是不是每一个功能点都能通过测试;

l         对于整个项目的统一单元测试。通常与每日构造结合。

针对以上三种情况,我设计的测试代码中包含以下文件夹和文件:

1)项目文件夹的根目录中,放置一个unit_test文件夹,其中包含setup.inc.phpindex.php

setup.inc.php

测试系统的初始化文件,每一个直接执行的测试程序都要包含它

index.php

进行系统整体测试的程序

2) 每一个需要进行单元测试的子文件夹中,建立一个unit_test文件夹,其中包含一个index.php和若干与单元测试功能点所在的文件同名的文件。例如对于以下一个fruits模块(位于项目的fruits文件夹中),其结构如下:

文件夹结构

文件描述

fruits

fruits模块的文件夹

CFruits.class.php

水果的基类

CApple.class.php

苹果类

CBanana.class.php

香蕉类

unit_test

单元测试文件夹

index.php

fruits模块集中测试的程序

CFruits.class.php

fruits模块中的CFruits.class.php进行单元测试的代码

CApple.class.php

fruits模块中的CApple.class.php进行单元测试的代码

CBanana.class.php

fruits模块中的CBanana.class.php进行单元测试的代码

二、实现方法

通过上文,可以看出本设计中包含了三种测试和四种文件,四种文件分别为:

l         单元测试初始化文件(每个项目一个)。

l         单元测试文件(每个代码文件一个);

l         模块集中测试文件(项目中每个模块一个);

l         项目集中测试文件(一个项目一个);

1、单元测试初始化文件

<?php
if (!defined('SIMPLETEST_SETUP'))
{
    /* the following four constant defines the run types of TEST */
    /** don't know what to run */
    define('SIMPLETEST_RUN_UNKNOWN',    0);
    /** run the current unit of test */
    define('SIMPLETEST_RUN_UNIT',            1);
    /** run the group of test units */
    define('SIMPLETEST_RUN_GROUP',        2);
    /** run all tests */
    define('SIMPLETEST_RUN_GLOBAL',        3);
    $simletest_run = SIMPLETEST_RUN_UNKNOWN;
    /* Install path SimpleTest */
    define('SIMPLETEST_DIR', 'd:/web_sites/simpletest');
    /* 项目根文件夹 */
    define('SYS_ROOT', dirname(__FILE__) . '/..');
    define('SIMPLETEST_SETUP', true);
    require SIMPLETEST_DIR . '/unit_tester.php';
    require SIMPLETEST_DIR . '/reporter.php';
}
?>

其中声明了四个常量以表示当前运行的测试为哪一种测试(单元/模块/项目/未知);定义了SimpleTest所在的文件夹;定义了当前进行单元测试的项目所在的文件夹;包含了基本的文件。

2、单元测试文件

以下为一个典型的单元测试文件的模板(功能直接用注释说明)

<?php
/* 声明当前测试运行类型的全局变量,这是由于SimpleTest在进行集中测试时会在某个方法中使用require函数来包含各个单元测试文件,而在PHP中,当在函数/方法使用require/include时,被包含进来的代码与函数处于相同的上下文(变量作用域) */
global $simpletest_run;
/* 如果$simpletest_run还没有设置,则说明setup.inc.php肯定没有被包含进来,在进行模块/项目这两种测试时,setup.inc.php会被首先在index.php中包含进来,然后各个单元测试文件被index.php包含进去,所以如果$simpletest_run未设置,则说明这个文件被直接执行,也就是进行本单元的测试 */
if (!isset($simpletest_run))
{
    require '../../../unit_test/setup.inc.php';
    $simpletest_run = SIMPLETEST_RUN_UNIT;
}
/* 包含本单元测试所要测试的代码 */
require_once(SYS_ROOT . '/sys/user_object/customize.inc.php');
/* 单元测试的类,参考SimpleTest文档 */
class TestOfValueCheck extends UnitTestCase
{
    /* Test cases for function check_format() */
    function testCheckFormat()
    {
        // UO_FMT_LETTER
        $this->assertTrue(check_format('hello', UO_FMT_LETTER));
        $this->assertTrue(check_format('HelloWorld', UO_FMT_LETTER));
        $this->assertTrue(check_format('', UO_FMT_LETTER));
        $this->assertFalse(check_format('a3', UO_FMT_LETTER));
        $this->assertFalse(check_format('34', UO_FMT_LETTER));
    }
}
/* 如果当前测试类型为本单元测试,则直接实例化一个对象并运行 */
if ($simpletest_run == SIMPLETEST_RUN_UNIT)
{
    $test = &new TestOfValueCheck();
    $test->run(new TextReporter());
}
?>

3、模块集中测试文件

<?php
global $simpletest_run;
if (!isset($simpletest_run))
{
    require '../../../unit_test/setup.inc.php';
    $simpletest_run = SIMPLETEST_RUN_GROUP;
}
class UserObjectGroupTest extends GroupTest
{
    function UserObjectGroupTest()
    {
        $current_dir = dirname(__FILE__);
        /* 包含各个单元测试文件 */
        $this->addTestFile($current_dir . '/customize.inc.php');
        $this->addTestFile($current_dir . '/check.php');
    }
}
if ($simpletest_run == SIMPLETEST_RUN_GROUP)
{
    $test = &new UserObjectGroupTest();
    $test->run(new HtmlReporter());
}
?>

Trackbacks
No Trackbacks
Comments
Display comments as (Linear | Threaded)
No comments
Add Comment


有人发送垃圾信息不遗余力,本人不胜其烦,所以只好委屈您填写一下如下代码
CAPTCHA

 
 

Categories

All categories

Calendar

« 2010 年 March
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Archives

Quicksearch

Syndicate This Blog

XML RSS 1.0 feed
XML RSS 2.0 feed

Blog Administration

Open login screen

访问统计

Locations of visitors to this page
The articles in this sites are copyrighted, except those marked as reshipped
MII Record: 05029638