Module:ScribuntoUnit/documentation

This module provides unit tests for other Lua modules. To test a module, you must create a separate test module, usually located at. 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
To make a test module (test suite), start with the following code:

After you have done this you can add individual test functions to the  object. Any function that begins with  is treated as a test. (Other functions will be ignored by ScribuntoUnit, but can be used in the tests themselves.)

The tests you write should make assertions, and ScribuntoUnit will check whether those assertions are true. For example,  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  object.

Running the tests
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. If you are running them from a wiki page, use the code. This will generate a table containing the results. It is also possible to display a more compact table by using the code.

Error messages
The last parameter of all the test methods is a message that is displayed if validation fails.

assertTrue, assertFalse
These test whether the given expression evaluates to  or. Note that in Lua  and   evaluate to , and everything else evaluates to.

assertStringContains
This tests whether  is found in the string. If  is true, then   is interpreted as literal text; otherwise,   is interpreted as a ustring pattern.

If the string is not found, the error message shows the values of  and  ; if   is more than 70 characters long then a truncated version is displayed. This method is useful for testing specific behaviours in complex wikitext.

assertNotStringContains
This is the opposite of. The test will fail if  is found in the string. If  is true, then   is interpreted as literal text; otherwise,   is interpreted as a ustring pattern.

assertEquals
This tests whether the first parameter is equal to the second parameter. If both parameters are numbers, the values are instead compared using  with delta 1e-8 (0.00000001) since numbers are represented as floating points with limited precision.

assertWithinDelta
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, which are used to represent numbers in the standard installation of Lua. (To be precise, it uses numbers.) For example, on the version of Scribunto installed on the English Wikipedia, the expression   evaluates to. This is because in practice, the expression  equals   and the number   equals. The slight error between the two means that Lua does not consider them equal. Therefore, to test for equality between two floating point numbers, we should accept values within a small distance (delta) of each other, not just equal values. Note that this problem does not affect integers, which can be represented exactly using double-precision floating point numbers up to values of 2^53.

assertDeepEquals
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.

assertTemplateEquals
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.

Note that some tags written in XML notation cannot be tested correctly; see the note for the  function below.

assertResultEquals
This tests whether the first parameter equals the expansion of any wikitext. The second parameter can be any wikitext.

Note that some special tags written in XML notation, such as,  ,   and   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  and   functions.

assertSameResult
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.

Note that some tags written in XML notation cannot be tested correctly; see the note for the  function above.

assertThrows
This tests whether a given function throws an exception. If  is not , it will check that an exception was thrown with the given error message.