目录 搜索
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
文字

CodeIgniter 用户指南 版本 2.1.0

编辑文档、查看近期更改请 登录 或 注册  找回密码
查看原文

单元测试类

单元测试是一种用于测试应用程序中每个函数的软件开发方法。如果你对此概念不熟悉,或许需要google一下相关概念。

CodeIgniter的单元测试类非常简单,由一个评估函数和两个结果函数组成。它没打算成为一个十全的测试集,只提供一个简单的能够评测你的代码是否可以产生正确的数据类型及结果的解决方案。

初始化单元类

和大多数其它的类一样,在CodeIgniter中,单元测试类一样要在控制器中用函数$this->load->library 来初始化:

$this->load->library('unit_test');

测试类一旦加载,单元测试对象可以这样使用:$this->unit

运行测试:

运行一个测试应提供一个测试及期望的结果给以下函数:

$this->unit->run( test, expected result, 'test name', 'notes');

Where test is the result of the code you wish to test, expected result is the data type you expect, test name is an optional name you can give your test, and notes are optional notes. Example:

$test = 1 + 1;

$expected_result = 2;

$test_name = 'Adds one plus one';

$this->unit->run($test, $expected_result, $test_name);

你所给出的期望结果($expected_result)可以是字面上匹配(literal match)的也可以是数据类型上匹配的。下例为字面匹配(literal match):

$this->unit->run('Foo', 'Foo');

下例为类型匹配(data type match):

$this->unit->run('Foo', 'is_string');

注意第二个参数"is_string"的使用,它告诉函数测试你的测试用例把产生的将是一个字符串。下面是合法的类型列表:

  • is_object
  • is_string
  • is_bool
  • is_true
  • is_false
  • is_int
  • is_numeric
  • is_float
  • is_double
  • is_array
  • is_null

生成报告

你可以在每个测试后立刻显示结果,也可以运行若干测试后再产生报告。直接显示一个报告,可以用echo或者返回run函数(的执行结果):

echo $this->unit->run($test, $expected_result);

显示所有测试的完整的报告,可以这样:

echo $this->unit->report();

报告会以格式化了的HTML表格的形式呈现。如果你更喜欢看原始的数据,可以用下面代码获取一个数组:

echo $this->unit->result();

严格模式

默认情况下,单元测试类评测字面匹配(literal match)时比较宽松,考虑以下例子:

$this->unit->run(1, TRUE);

此测试评测的是一个整数,但是期望结果却是布尔值。不过,由于PHP是弱类型语言,使用正常的相等测试,以上代码返回的将是TRUE:

if (1 == TRUE) echo 'This evaluates as true';

喜欢的话,你可以让单元测试类在严格模式下执行,这样它会同时比较两数据的类型及值:

if (1 === TRUE) echo 'This evaluates as FALSE';

启用严格模式:

$this->unit->use_strict(TRUE);

启用/禁用单元测试

如果你想放一些测试在代码中,而又希望让它只在需要的时候运行,你可以禁用单元测试:

$this->unit->active(FALSE)

Unit Test Display

When your unit test results display, the following items show by default:

试翻译:当你显示单元测试结果的时候,以下信息(原以为条目)将被默认显示:

  • Test Name (test_name)
  • 试翻译:测试名称(test_name)
  • Test Datatype (test_datatype)
  • 试翻译:测试数据类型(test_datatype)
  • Expected Datatype (res_datatype)
  • 试翻译:期望数据类型(res_datatype)
  • Result (result)
  • 试翻译:结果(result)
  • File Name (file)
  • 试翻译:文件名称(file)
  • Line Number (line)
  • 试翻译:行数(line)
  • Any notes you entered for the test (notes)
  • 试翻译:手动添加的注释(notes)

You can customize which of these items get displayed by using $this->unit->set_items(). For example, if you only wanted the test name and the result displayed:

试翻译:使用$this->unit->set_items() ,你可以自定义显示信息(条目). 比如,如果你只期望显示测试名称和测试结果:

Customizing displayed tests

试翻译:自定义显示测试结果

$this->unit->set_test_items(array('test_name', 'result'));

Creating a Template

试翻译:创建显示模板

如果你希望测试结果以自定义的格式显示,这里提供一个简单的模板示例。注意必需的伪变量(seudo-variables):

$str = '
<table border="0" cellpadding="4" cellspacing="1">
    {rows}
        <tr>
        <td>{item}</td>
        <td>{result}</td>
        </tr>
    {/rows}
</table>';

$this->unit->set_template($str);

注意: 你的模板必须在单元测试运行之前声明。

 

翻译贡献者: Hex, iptton, zhou78620051
最后修改: 2011-11-24 15:46:57
上一篇: 下一篇: