这是模块:UnitTests的文档页面
内容协议 本文采取的内容协议为CC BY-SA 4.0
|
来自EN Wikipedia
|
UnitTests 为其他 Lua 脚本提供单元测试功能。参见 Wikipedia:Lua#Unit_testing。以下是来自 Module:Bananas/testcases 的示例:
-- Module:Bananas 的单元测试
local p = require('Module:UnitTests')
function p:test_hello()
self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!')
end
return p
在测试模块的讨论页使用 {{#invoke:Bananas/testcases | run_tests}} 来运行。以 "test" 开头的函数自动识别为测试。
方法
- run_tests(differs_at)
- 运行所有测试。传入
differs_at=1会额外显示期望值和实际值的首个差异字符位置。 {{#invoke:Bananas/testcases|run_tests}}
- preprocess_equals(text, expected)
- 将 wikitext 送解析器展开后与期望值比对。可在 wikitext 中调用模板和模块。
self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!')
- preprocess_equals_many(prefix, suffix, cases)
- 批量执行 preprocess_equals。自动在每个测试文本前后添加 prefix/suffix。
self:preprocess_equals_many('{{#invoke:BananasArgs | add |', '}}', { {'2|3', '5'}, {'-2|2', '0'}, })
- preprocess_equals_preprocess(text1, text2)
- 将两段 wikitext 分别展开后比对。适用于比较模块与模板的输出。
self:preprocess_equals_preprocess('{{#invoke:Bananas | hello}}', '{{Hello}}')
- preprocess_equals_preprocess_many(prefix1, suffix1, prefix2, suffix2, cases)
- 批量执行 preprocess_equals_preprocess。
- equals(name, actual, expected)
- 纯值比对。适用于测试供其他模块调用的函数(而非通过 #invoke 调用的)。
self:equals('简单加法', 2 + 2, 4)
- equals_deep(name, actual, expected)
- 深层递归比较表结构。
