与“Codeigniter里的单元测试”相关的原创示意图
测试驱动开发已经提出好久了。在web领域估计国内采用的不多。
其实单元测试也是很好用的东西,确实可以提高开发人员的效率。
清风用codeigniter举例说明一下。
load->library('unit_test');
$this->unit->active(TRUE);//true 启用;false 停用
}
function blog_list(){
$result = array('one','tow','three');
return $result;
}
function commend_save(){
//保存成功返回true,失败false;假设失败
return false;
}
function test()
{
$this->unit->run($this->blog_list(),'is_array','Test Blog_list.');
$this->unit->run($this->commend_save(),'is_true','Test commend_save.','返回true才是添加成功');
echo $this->unit->report();
}
}
在控制器里,加载单元测试类,并添加一个测试方法,里面调用所有需要测试的本控制器里的方法。访问一下这个方法,就知道接受测试的方法中哪个方法有问题了。结果如下:
红色的Failed说明,这个方法没有通过测试。可以去找bug了。
这样就避免了大量的在浏览器里不停刷新等操作的尴尬。当然codeigniter内置的单元测试比较简单,可以尝试simple test。