跳到主要内容

unittest

简介

gs的单元测试框架

组件接口

tester.gs

单元测试组件

每个组件都有对应名称的目标object

如果一个目标object的文件名为 file.gs

则其单元测试文件名称应该为 test_file.gs 且位于 ./unit_test 文件夹

函数原型函数作用
void run_tests()运行当前测试文件的所有测试函数
map get_result()获取测试结果
void set_stateless_tests(map tests)Set stateless tests map
void test_assure(mixed cond, string msg = "")测试入参是否为true
void test_equal(mixed val, mixed expect_val, string msg = "")测试两个值是否相等
void test_same(mixed val, mixed expect_val, string msg = "")测试两个数据是否相同(array/map/function)
mixed CALL_OB(...)找到 test_ob() 的测试函数并调用
mixed CALL_STATIC(...)找到 test_daemon() 的测试函数并调用
object new_test_ob(map para = nil, domain d = nil)新建一个 test object
object test_ob(map para = nil, domain d = nil)获取 test object,不存在则新建一个
object test_daemon(map para = nil, domain d = nil)新建一个 static test object
function find_test_function(object ob, int depth)获取测试函数

unittest.gs

函数原型函数作用
map run(string dir, bool recursive = true, function test_callback = nil)运行所有单元测试
array run_tests_in_dir(string test_dir, bool recursive, function test_callback)运行目标目录下所有以 test_ 开头的测试文件
map run_test_file(string test_file)运行一个测试脚本文件

样例

import pkg.unittest;

string testdir = "./test_scripts"

unittest.run(testdir);