ScribuntoUnit is an assert-style unit tester for Scribunto modules. The syntax for test assertions is markedly similar to the LuaUnit framework, NodeJS assert module and the NodeJS Chai assertion API.
Usage
Test suite syntax
- The test suite is initialised by calling the
ScribuntoUnit:newconstructor:local module = require('Module:<MODULENAME>') local suite = require('Module:ScribuntoUnit'):new()
- The test suite registers methods prefixed with
testas tests.function suite:test_someCall() self:assertEquals('expected value', module._someCall(123)) self:assertEquals('other expected value', module._someCall(456)) end function suite:test_someOtherCall() self:assertEquals('expected value', module._someOtherCall(123)) self:assertEquals('other expected value', module._someOtherCall(456)) end
- Assertions for methods are called in each test. Tests can target a module method's functionality or any specific feature, at will.
- Tests will generate an error message if they fail validation, resulting in the failure of the test suite. This information will appear in the third column of the test report table, as necessary.
- Note that functions not prefixed by
testwill not appear in the test result table. These can be called as dependencies in the test suite.
- The test suite instance is returned at the end of the module:
return suite
Test suite execution
Validation failure messages
The last parameter of all `ScribuntoUnit` assertion methods is a message that is displayed in the third report column if validation fails.
function suite:test_SomeCall()
self:assertEquals(
"expected value",
module._someCall(123),
"validation failed for module._someCall(123)"
)
end
Documentation
Package items
ScribuntoUnit:run(displayMode)(function)- Invocation entry point for running the tests. This method can be called without a frame, in which case it will use
mw.logfor output. However, the following assertion methods require template or wikitext expansions and will be skipped:ScribuntoUnit:assertResultEquals- requiresframe:preprocessScribuntoUnit:assertSameResult- requiresframe:preprocessScribuntoUnit:assertTemplateEquals- requiresframe:expandTemplate
- Parameter:
displayModeAccepts:"table"- shows result report as a wikitext table that displays which test(s) failed and the failure message."log"- shows result as a console log for debugging purposes. Skips frame-dependent assertions."short"- displays result report as a short sentence."tableshort"- as "short", unless there is a failing test. Then, as "table".
- (string)
- Usage:
{{testunit|scrib}}{{#invoke:ScribuntoUnit|run|displayMode = <table/log>}}
ScribuntoUnit:new(o)(function • constructor)- Instantiates a new
ScribuntoUnitfor a test suite module. The developer can call this method with a table constructor/upvalue, or they can attach their methods to the returned instance's table. - Parameter:
oa table with test functions. (table; optional) - Returns: Self-executable instance of ScribuntoUnit. (ScribuntoUnit)
- Usage:
local suite = require('Module:ScribuntoUnit'):new()return require('Module:ScribuntoUnit'):new(suite)
ScribuntoUnit:markTestSkipped(message)(function)- When used in a test, that test gets ignored, and the skipped count increases by one.
- Parameter:
messageOptional description. (string; optional) - Error: ScribuntoUnit error with field
skippedastrue. (table) ScribuntoUnit:assertTrue(actual, message)(function)- Assertion checking that the input evaluates as
trueas a Lua expression. In Lua, anything that isn't thefalseornilprimitives is "truthy". - Parameters:
actualA Lua expression - potentially an upvalue or condition evaluated by the test suite.messageOptional description of the test. (string; optional)
- Error: ScribuntoUnit error with assertion failure, if the
expressionvalue is falsy. (table; optional) - Usage:
suite:assertTrue(2 + 2 == 4)suite:assertTrue('foo')
ScribuntoUnit:assertFalse(actual, message)(function)- Assertion checking that the input evaluates as
falseas a Lua expression. In Lua, only thefalseandnilprimitives are "falsy". - Parameters:
actualA Lua expression - potentially a variable or condition evaluated by the test suite.messageOptional description of the test. (string; optional)
- Error: ScribuntoUnit error with assertion failure, if the
expressionvalue is truthy. (table; optional) - Usage:
suite:assertFalse(2 + 2 == 5)suite:assertFalse(false)
ScribuntoUnit:assertStringContains(pattern, str, plain, message)(function)- Assertion testing for the presence of an expected pattern or string in an input string. If the string is not found, the error message shows the values of
patternandstr. Ifstris more than 70 characters long, a truncated version is displayed in the error message. This method is useful for testing specific behaviours in complex wikitext. - Parameters:
- Usage:
suite:assertStringContains("foo", "foobar")suite:assertStringContains("foo[", "foo[bar", true)
ScribuntoUnit:assertNotStringContains(pattern, str, plain, message)(function)- Assertion testing for the absence of an expected pattern or string in an input string. If the string is not found, the error message shows the values of
patternandstr. This test method has similar validation and rendering behaviour toScribuntoUnit:assertStringContains. - Parameters:
- Usage:
suite:assertNotStringContains("baz", "foobar")suite:assertNotStringContains("[qux", "baz]qux", true)
ScribuntoUnit:assertEquals(expected, actual, message)(function)- Assertion for the strict equality of an input upvalue against an expected value. This method uses
==instead ofrawequal, thus supporting__eqmetamethods where applicable. Tables and functions are compared by reference in the assertion (i.e. by whether they reference the same object). - If both parameters are numbers, the values are instead compared using
ScribuntoUnit:assertWithinDeltawith delta1e-8(0.00000001) since Lua numbers are represented as floating-point numbers with limited precision. - Parameters:
expectedTarget Lua upvalue for comparison, generated by the test module.actualValue to compare against in the comparison, provided in the test suite.messageOptional description of the test. (string; optional)
- Usage:
suite:assertEquals(4, calculator._add(2, 2), "2 + 2 should be 4") ScribuntoUnit:assertWithinDelta(expected, actual, delta, message)(function)- Assertion for the numerical equivalence of a input upvalue against an expected numerical value, within a closed interval or "distance" (
delta). Lua non-integer numbers are represented as double-precision floating-point numbers with limited precision. Generally, Lua numerical comparisons involve a rounding step by when one of the values is irrational. The slight error between the compared numbers means that Lua does not consider them equal. This necessitates a ranged comparison using a delta when handling these numbers. - Parameters:
expectedNumerical value to compare against, provided in the test suite. (number)actualTarget Lua upvalue for comparison, generated by the test module. (number)deltaClosed interval range to permit value difference within. Should range between1e-8and1e-16. (number)messageOptional description of the test. (string; optional)
- Usage:
suite:assertWithinDelta(0.1, calculator._subtract(0.3, 0.2), 1e-10) ScribuntoUnit:assertDeepEquals(expected, actual, message)(function)- Assertion for the recursive equivalence of two tables. This function works by computing the path to every primitive in the tables, then comparing them by reference. Respects the
__eqmetamethod of theexpectedtable. - If a non-table value is provided for
expectedandactual, the values are assumed to be primitives and compared by reference. - Parameters:
- Usage:
suite:assertDeepEquals({ { 1, 3 }, { 2, 4 } }, calculator._partition("odd", { 1, 2, 3, 4 })) ScribuntoUnit:assertResultEquals(expected, text, message)(function)- Assertion for processing of unprocessed wikitext against an expected result.
- Parameters:
- Usage:
suite:assertResultEquals("4", "{{calculator|addition|2|2}}") ScribuntoUnit:assertSameResult(text1, text2, message)(function)- Assertion for comparison of two unprocessed wikitext strings.
- Parameters:
- Usage:
suite:assertSameResult("{{#expr: 2 + 2}}", "{{calculator|addition|2|2}}") ScribuntoUnit:assertTemplateEquals(expected, template, args, message)(function)- Assertion for a template call comparison against expected output.
- Parameters:
- Usage:
suite:assertTemplateEquals("4", "calculator", {"add", "2", "2"}) ScribuntoUnit:assertThrows(fn, expectedMessage, message)(function)- Assertion for an uncaught exception for a function call.
- Parameters:
- Usage:
suite:assertThrows(error, "unknown error")