Modul:ScribuntoUnit/dok
Dette er dokumentasjonssiden for Modul:ScribuntoUnit
This module provides unit tests for other Lua modules. To test a module, you must create a separate test module, usually located at Module:Module name/testtilfeller
. The module is tested with the ScribuntoUnit module, which verifies that the operations defined in the test module produce the expected results.
Test module structure
[rediger kilde]To make a test module (test suite), start with the following code:
local myModule = require('Module:MyModule') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
After you have done this you can add individual test functions to the suite
object. Any function that begins with test
is treated as a test. (Other functions will be ignored by ScribuntoUnit, but can be used in the tests themselves.)
function suite:testSomeCall()
self:assertEquals('expected value', myModule.someCall(123))
self:assertEquals('other expected value', myModule.someCall(456))
end
function suite:testSomeOtherCall()
self:assertEquals('expected value', myModule.someOtherCall(123))
self:assertEquals('other expected value', myModule.someOtherCall(456))
end
The tests you write should make assertions, and ScribuntoUnit will check whether those assertions are true. For example, assertEquals
checks that both of the arguments it is given are equal. If ScribuntoUnit doesn't find an assertion to be true, then the test will fail and an error message will be generated. The error message will show which assertion failed verification (other checks on the assertions are not made at this time).
To finish the test module, you need to return the suite
object.
return suite
Running the tests
[rediger kilde]The tests can be run in two ways: through the Lua debug console, and from a wiki page using #invoke. If you are running the tests through the debug console, use the code require('Module:MyModule/tests').run()
. If you are running them from a wiki page, use the code {{#invoke:MyModule/tests|run}}
. This will generate a table containing the results. It is also possible to display a more compact table by using the code {{#invoke:MyModule/tests|run|displayMode=short}}
.
Tests
[rediger kilde]Error messages
[rediger kilde]The last parameter of all the test methods is an message that is displayed if validation fails.
self:assertEquals('expected value', myModule.someCall(123), 'This tests whether the function x does y.')
assertTrue, assertFalse
[rediger kilde]self:assertTrue(actual, message)
self:assertFalse(actual, message)
These test whether the assertion evaluates to true
or false
. Note that in Lua false
and nil
evaluate to false
, and everything else evaluates to true
.
self:assertTrue(2 + 2 == 4)
self:assertTrue('foo')
self:assertFalse(2 + 2 == 5)
self:assertFalse(nil)
assertStringContains
[rediger kilde]self:assertStringContains(pattern, s, plain, message)
This tests whether pattern
is found in the string s
. If plain
is true, then pattern
is interpreted as literal text; otherwise, pattern
is interpreted as a ustring pattern.
If the string is not found, the error message shows the values of pattern
and s
; if s
is more than 70 characters long then a truncated version is displayed. This method is useful for testing specific behaviours in complex wikitext.
self:assertStringContains("foo", "foobar") -- passes
self:assertStringContains("foo", "fobar") -- fails
self:assertStringContains(".oo", "foobar") -- passes: matches "foo"
self:assertStringContains(".oo", "foobar", true) -- fails: . is interpreted as a literal character
assertEquals
[rediger kilde]self:assertEquals(expected, actual, message)
This tests whether the first parameter is equal to the second parameter. If both parameters are numbers, the values are instead compared using assertWithinDelta
with delta 1e-8 (0.00000001) since numbers are represented as floating points with limited precision.
self:assertEquals(4, calculator.add(2, 2))
assertWithinDelta
[rediger kilde]self:assertWithinDelta(expected, actual, delta, message)
For two numbers, this tests whether the first is within a given distance (delta) from the second. This is useful to compare floating point numbers. In a test, we would like for instance 0.3 – 0.2 to evaluate as equal to 0.1, but in practice 0.3 – 0.2 = 0.09999999999999997780… is not strictly equal to 0.1 = 0.10000000000000000555…. Floating point numbers should therefore be considered equal if within a small distance (delta) of each other.
self:assertWithinDelta(0.1, calculator.subtract(0.3, 0.2), 1e-10)
assertDeepEquals
[rediger kilde]self:assertDeepEquals(expected, actual, message)
This tests whether the first parameter is equal to the second parameter. If the parameters are tables, they are compared recursively, and their __eq metamethods are respected.
self:assertDeepEquals(table1, table2)
assertTemplateEquals
[rediger kilde]self:assertTemplateEquals(expected, template, args, message)
This tests whether the first parameter equals a template call. The second parameter is the template name, and the third parameter is a table of the template arguments.
self:assertTemplateEquals(4, 'add', {2, 2}) -- true if {{add|2|2}} equals 4
Note that some tags written in XML notation cannot be tested correctly; see the note for the assertResultEquals
function below.
assertResultEquals
[rediger kilde]self:assertResultEquals(expected, text, message)
This tests whether the first parameter equals the expansion of any wikitext. The second parameter can be any wikitext.
self:assertResultEquals(4, '{{#invoke:Calculator|add|2|2}}')
Note that some special tags written in XML notation, such as <pre>
, <nowiki>
, <gallery>
and <ref>
cannot be compared correctly. These tags are converted to strip markers before they are processed by Lua. Strip markers are unique, even when generated from identical input, so any tests testing these tags for equality will fail. This also applies to the assertTemplateEquals
and assertSameResult
functions.
assertSameResult
[rediger kilde]self:assertSameResult(text1, text2, message)
This tests whether the expansion of a given string of wikitext equals the expansion of another string of wikitext. This can be useful for verifying that a module behaves in the same way as a template it is intended to replace.
self:assertSameResult('{{add|2|2}}', '{{#invoke:Calculator|add|2|2}}')
Note that some tags written in XML notation cannot be tested correctly; see the note for the assertResultEquals
function above.