CodeIgniter 中国开发者社区诚征热爱 CI 的版主

查看完整版本: 单元测试分享

lxylxy888666 2008-8-15 08:12

单元测试分享

我仅仅是做测试,随便写的
首先在libraries里写你自己的类
[code=PHP]<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Image {
var $url = "";
    function My_image($url)
    {
     echo "<hr/>";
     echo "<font color = 'red'>The url is:$url"."</font>";
     echo "<hr/>";
    }
   
    function Re_image($name)
    {
     echo "The image name is $name";
    }
   
    function A_image()
    {
     return "AA";
    }
}
?>[/code]

然后在controller里任一function里进行测试

[code=PHP]function image_unit()
        {
         echo "test...";
         $this->load->library('unit_test');
      $this->load->library('Image');
      $test = $this->image->a_image();
      $expected_result = 'AA';
      $test_name = 'Image Unit';
   echo $this->unit->run($test, $expected_result, $test_name);
        }[/code]

有点注意:调用时的类名,函数名必须小写,搞了我半天,汗。
你还可以进行类型测试,
这是对结果进行测试。


[table=98%][tr][td]Test Name[/td][td]Image Unit[/td][/tr][tr][td]Test Datatype[/td][td]String[/td][/tr][tr][td]Expected Datatype[/td][td]String[/td][/tr][tr][td]Result[/td][td][color=#0c0]Passed[/color][/td][/tr][tr][td]File Name[/td][td]D:\webroot\x.cn\CodeIgniter_1.6.3\system\application\controllers\welcome.php[/td][/tr][tr][td]Line Number[/td][td]549[/td][/tr][/table]

若有什么错误,请指正。谢谢

kissmumu 2008-8-15 14:22

支持一把。
虽然我从不测试:victory:

Hex 2008-8-15 14:26

很棒,我给你转移并把语法加亮了。
加分~

lxylxy888666 2008-8-15 16:01

[quote]原帖由 [i]kissmumu[/i] 于 2008-8-15 14:22 发表 [url=http://codeigniter.org.cn/forums/redirect.php?goto=findpost&pid=4942&ptid=941][img]http://codeigniter.org.cn/forums/images/common/back.gif[/img][/url]
支持一把。
虽然我从不测试:victory: [/quote]

如果是中小项目,作用不是很明显。
但是面对中大型项目,是非常重要的。
谢谢HEX:lol
页: [1]
查看完整版本: 单元测试分享