Jump to content

Module:ScribuntoUnit/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 06:10, 5 December 2013 (Translate the first half of the documentation from hu:Module:ScribuntoUnit/doc, revision as of 23:59‎, 3 November 2013. Authors: hu:User:Tgr, hu:User:Pepo41, hu:User:84.3.90.28.). 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)

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/testcases. 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:

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

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

Ellenőrzések

hibaüzenetek

Minden ellenőrzés opcionális utolsó paraméterként elfogad egy üzenetet is, ami meg fog jelenni, ha az ellenőrzés sikertelen.

assertTrue, assertFalse

Azt ellenőrzik, hogy a vizsgált állítás igaz ill. hamis-e.

self:assertTrue(2 + 2 == 4)

assertEquals, assertDeepEquals

A paraméterek egyenlőségét vizsgálja. Hagyományosan az első paraméter az ismert eredmény, a második a vizsgált függvényhívás.

self:assertEquals(4, calculator.add(2, 2))

As assertDeepEquals táblák egyenlőségének a vizsgálatára is alkalmas.

assertTemplateEquals

Egy sablonhívás eredményét vizsgálja.

self:assertTemplateEquals(4, 'add', {2, 2}) -- akkor igaz, ha {{add|2|2}} értéke 4

assertResultEquals

Tetszőleges wikikód (tipikusan egy modulhívás) kimenetét vizsgálja.

self:assertResultEquals(4, '{{#invoke:Calculator|add|2|2}}')

assertSameResult

Két wikikód kimenetét hasonlítja össze; hasznos lehet egy sablon lecserélésénél annak ellenőrzésére, hogy az új sablon ugyanúgy viselkedik-e.

self:assertSameResult('{{add|2|2}}', '{{#invoke:Calculator|add|2|2}}')