测试文件的名字使用 *_SUITE.erl 形式,模块中包含 ct.hrl,测试文件需要导出 all/0 函数,其返回待执行的测试用例组和测试用例的列表。
对于每个测试用例函数,可以存在与其同名,但不带参数的函数。这就是测试用例信息函数。它返回带标记的元组的列表,每个元组指定关于测试用例的各种属性。比如:
下面是一个测试用例信息函数示例:
xxxxxxxxxx reboot_node() -> [ {timetrap,{seconds,60}}, {require,interfaces}, {userdata, [{description,"System Upgrade: RpuAddition Normal RebootNode"}, {fts,"http://someserver.ericsson.se/test_doc4711.pdf"}]} ].在测试套件模块中,函数 suite/0 可以用于设置默认的 timetrap 值,以及 require 外部的配置数据。如果测试用例或组信息函数也指定了信息标签,那么其重写由 suite/0 指定的默认值。
下面是一个套件信息函数示例:
xxxxxxxxxx suite() -> [ {timetrap,{minutes,10}}, {require,global_names}, {userdata,[{info,"This suite tests database transactions."}]}, {silent_connections,[telnet]}, {stylesheet,"db_testing.css"} ].测试用例组是共享配置函数和执行属性的一组测试用例。通过函数 groups/0,定义测试用例组。groups/0 应该返回具有如下语法的 term:
groups() -> GroupDefs
Types:
GroupDefs = [GroupDef] GroupDef = {GroupName,Properties,GroupsAndTestCases} GroupName = atom() GroupsAndTestCases = [GroupDef | {group,GroupName} | TestCase | {testcase,TestCase,TCRepeatProps}] TestCase = atom() TCRepeatProps = [{repeat,N} | {repeat_until_ok,N} | {repeat_until_fail,N}]GroupName 是组名,在测试套件模块中,必须唯一。通过在其它组的 GroupsAndTestCases 列表中包含组定义的方式,可以嵌套组。Properties 是用于组的执行属性列表。可选值如下:
xxxxxxxxxx Properties = [parallel | sequence | Shuffle | {GroupRepeatType,N}] Shuffle = shuffle | {shuffle,Seed} Seed = {integer(),integer(),integer()} GroupRepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail | repeat_until_any_ok | repeat_until_any_fail N = integer() | forever解释:
Common Test 并行地执行组中的测试用例Common Test 重复执行组中的所有用例给定的次数,或直到任何或所有用例失败或成功示例:
xxxxxxxxxxgroups() -> [{group1, [parallel], [test1a,test1b]}, {group2, [shuffle,sequence], [test2a,test2b,test2c]}].为指定组的执行顺序(也涉及到不属于任何组的测试用例),将 {group,GroupName} 形式的元组添加到 all/0 列表。
示例:
xxxxxxxxxxall() -> [testcase1, {group,group1}, {testcase,testcase2,[{repeat,10}]}, {group,group2}].x.├── demo│ └── demo_SUITE.erl├── logs/└── spec.spec2 directories, 2 files
spec.spec:
{alias, demo, "./demo/"}.{logdir, "./logs/"}.{suites, demo, all}.{skip_cases, demo, demo_SUITE, test_skipped, "this test fails on purpose"}.demo/demo_SUITE.erl:
x
-module(demo_SUITE).-include_lib("common_test/include/ct.hrl").-include_lib("eunit/include/eunit.hrl").-compile(export_all).
all() -> [test1, test_skipped].
init_per_suite(Config) -> [{arg, 1} | Config].
end_per_suite(_) -> ok.
init_per_testcase(Case, Config) -> [{caseName, Case} | Config].
end_per_testcase(_, _) -> ok.
test1(Config) -> Arg = ?config(arg, Config), ?assert(Arg =:= 1), ok.
test_skipped(_Config) -> throw("this test fails on purpose").xxxxxxxxxx$ ct_run -spec spec.spec -suite demo/demo_SUITE.erl