Jump to content

Module:UnitTests/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dcoetzee (talk | contribs) at 03:07, 25 February 2013 (Created page with 'UnitTests provides a unit test facility that can be used by other scripts using '''require'''. See Wikipedia:Lua#Unit_testing for details. Following is a sam...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

UnitTests provides a unit test facility that can be used by other scripts using require. See Wikipedia:Lua#Unit_testing for details. Following is a sample from Module:Bananas/tests:

-- Unit tests for [[Module:Bananas]]. Click talk page to run tests.
local p = require('Module:UnitTests')
 
function p:test_hello()
    self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!')
end
 
return p

The talk page Module talk:Bananas/tests executes it with {{#invoke: Bananas/tests | run_tests}}. Test methods like test_hello above must begin with "test".

Methods

  • preprocess_equals(text, expected): Gives a piece of wikitext to preprocess and an expected resulting value. Scripts and templates can be invoked in the same manner they would be in a page.
    self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!')
  • preprocess_equals_preprocess(text, expected): Gives two pieces of wikitext to preprocess and determines if they produce the same value. Useful for comparing scripts to existing templates.
    self:preprocess_equals_preprocess('{{#invoke:Bananas | hello}}', '{{Hello}}')