Fandom

Wikia Developers Wiki

Global Lua Modules/Testharness

< Global Lua Modules

915pages on
this wiki
Add New Page
Talk6 Share
This meta-module helps to create tests to ensure that a particular module functions properly.

Description

This module facilitates the use of Module:UnitTests. Rather than entering all that information for every single module, all one needs to do is something like:

Syntax

Create a module with code similar to this:

-- Unit tests.  Click talk page to run tests.
local p ={
    hello={--<==function being tested
        options= {nowiki=false}, --<== options 
        tests = {
            preprocess_equals_many ={ --<== Test name
                -- Parameters      - Expected result
                {'Hello World!'    , 'Hello World!'},
                {'1=Hello|2=World' , 'Hello'},
            },
        },
    },
}
return p

Explanation:

  • hello = the name of the function that will be run
  • options = these are options that can be used when running the tests
    • nowiki = when this is true, the result will be wikitext, otherwise (if false) it will render the content of the page, e.g. [[Help:Lua]] -> Help:Lua.
  • tests = this is a table that contains tests that will be run, the names of the tests are based on Module:UnitTests, see Wikipedia:Module:UnitTests for more information.

In the example above the testharness will run the test "preprocess_equals_many" which will create the following (e.g. if it is run using Module:SharedTest):

Wikitext to run

Test

Expected

Results

Passed
{{#invoke:SharedTest|hello|Hello World!}} Compares expected vs results Hello World! Hello World! Yes
{{#invoke:SharedTest|hello|1=Hello|2=World}} Compares expected vs results Hello Hello Yes

It then lets Module:UnitTests run the tests and present the results when the wikitext below is added to a page.

Run the tests using:

{{#invoke:testharness|run_tests|modulename=<module>|testdata=<module with tests>}}

The module expects the tests to be stored in a subpage for testcases (e.g. Module:Bananas/testcases). It also expects the talkpage to be used to run the tests (and contain the invoke above), e.g.(Module_Talk:Bananas/testcases). This can be overwritten by using the parameters below:

Parameter Optional? Example Description
modulename Yes modulename=Links The module to be tested
testdata Yes testdata=Links/testcases The module containing the tests

Example

{{#invoke:testharness|run_tests|modulename=Links|testdata=Links/testcases}}

Tests that it runs

All tests available in Module:UnitTests shown in the table below[1]:

Test name Description Parameters
preprocess

_equals

Gives a piece of wikitext to preprocess and an expected resulting value preprocess_equals(text, expected, options)
preprocess

_equals

_many

Performs a series of preprocess_equals() calls on a set of given pairs. preprocess_equals_many(prefix, suffix, cases, options)
preprocess

_equals

_preprocess

Gives two pieces of wikitext to preprocess and determines if they produce the same value. Useful for comparing scripts to existing templates preprocess_equals_preprocess(text, expected, options)
preprocess

_equals

_preprocess

_many

Performs a series of preprocess_equals_preprocess() calls on a set of given pairs.  preprocess_equals_preprocess_many(prefix, suffix, cases, options)
Equals Gives a computed value and the expected value, and tests if they are equal. equals(name, actual, expected, options)
Equals_deep Similar to equals, but does a deep comparison in tables.Circular references should be avoided because they aren't handled and may result in infinite loops. equals_deep(name, actual, expected, options)

See also

References

  1. https://en.wikipedia.org/wiki/Module:UnitTests

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.