A minimal Lua testing library. Register tests, run them individually or all at once, and get clean pass/fail output. No dependencies.
Install with lux:
{
"cyb3rkun/lua-test",
opts = { scope = "local" },
}Note: lux and the LuaLuxa ecosystem are not yet publicly released. Manual installation is available in the meantime — just drop
init.luainto your project.
local t = require("test")
t.register("my test", {
func = function()
t.assert_eq(1 + 1, 2)
t.assert_true(type("hello") == "string")
end,
cleanup = function()
-- optional: teardown logic here
end,
})
t.test_all()Output:
[PASS] my test
Results: 1/1 passed
The entry point is left to the user. A typical pattern:
-- tests.lua
local t = require("test")
-- register your tests...
if arg and arg[1] == "--all" then
t.test_all()
elseif arg and arg[1] then
t.run_test(arg[1])
endlua tests.lua --all
lua tests.lua "my test"Registers a test. test.func is required and must be a function. test.cleanup is optional — if provided, it will be called after the test regardless of whether it passed or failed, useful for teardown and restoring state.
If cleanup itself fails, test_all will halt execution to prevent corrupted state from contaminating subsequent tests.
t.register("example", {
func = function() ... end,
cleanup = function() ... end, -- optional
})Runs all registered tests and prints a summary. Halts if a cleanup function fails.
Runs a single test by name.
| Function | Description |
|---|---|
t.assert_eq(actual, expected, msg?) |
Fails if actual ~= expected |
t.assert_neq(actual, expected, msg?) |
Fails if actual == expected |
t.assert_true(val, msg?) |
Fails if val is falsy |
t.assert_false(val, msg?) |
Fails if val is truthy |
t.assert_is_true(val, msg?) |
Fails if val ~= true (strict boolean) |
t.assert_is_false(val, msg?) |
Fails if val ~= false (strict boolean) |
t.assert_dpeq(actual, expected, msg?) |
Fails if tables are not deeply equal |
assert_true/assert_false check truthiness. Use assert_is_true/assert_is_false when you need strict boolean equality.
assert_dpeq recursively compares tables by value, and pretty-prints both sides on failure.
MIT