跳到主要内容

gtest

简介

加载 pkg 单元测试, 确保 pkg 的基本接口功能正常

与 gs 的包管理 gip 一起使用, 本地可以通过 gip test 来进行 pkg 的单元测试

pkg 提交到 gitlab 仓库之后, 会通过 cicd 自动加载 gip test 来进行 pkg 测试, 确保发布的 pkg 的基本功能正常

在单元测试运行完之后, 会输出一个表格, 说明当前 pkg 的单元测试情况, 对于异常的测试, 会输出相应的说明, 以及调用堆栈

在运行单元测试的同时, 会统计每一个测试的耗时, 最终与表格一起输出到控制台. 在一定程度上, 可以判断 pkg 的性能是否存在严重问题,

测试流程

遍历 ./test 目录下的 test 开头的 gs 文件, 然后通过运行该对象下, 全部以 test 开头的public 方法

编写测试

可以参看 cjson 的测试 .

编写测试函数后, 在 create / entry 中调用

// 测试读取 json 文件
// 从pkg中读取 name, 判断器是是 @g-bits/cjson
void create()
{
read_file();
}

public string read_file()
{
string path = "package.json";
map p = cjson.load(path);
return p.name;
}

本地测试

gip test

组件接口

gtest.gs

函数原型函数作用
string stat()获取测试结果, 可以直接打印在控制台上
bool is_success()获取测试是否通过
bool test_func(function func, mixed expected)对方法进行单元测试
bool test_func_with_arg(function func, mixed expected, ...)对方法进行单元测试
void test_equal(mixed val, mixed expected, string msg = nil)测试两个值是否相等
void test_assert(mixed cond, string msg = nil)测试 cond 是否为 true
bool test_func_despite_return(function func)测试方法是否存在异常, 无法测试返回值
bool test_object(mixed path_ob, string pattern = "test.*")对 object 中, 满足 pattern 要求的方法名, 进行测试
bool test_dir(string path, bool recursive = false, string file_pattern = "test.", string mehtod_pattern = "test.")对 dir 中, 所有 满足 file_pattern 的对象进行测试, 其中 object 为 .gs 或者 .o 结尾文件
bool test_object_funcs(mixed path_ob, map funcs)对 object 中 funcs 包含 的方法进行测试

equal.gs

函数原型函数作用
int equal(mixed data1, mixed data2)测试两个值是否相等

result.gs

函数原型函数作用
void add_exception(mixed func, int timems, mixed e)add exception
void add_fail(mixed func, int timems, mixed res, mixed expecteds)add fail
void add_success(mixed func, int timems, mixed info)add success
bool is_success()is success
string stat()stat