Wikipedia:Template test cases

Templates are a very powerful feature of MediaWiki, but mistakes can be easily made, even by experienced users. Complex templates should therefore be accompanied by sandboxes and test cases to prevent bugs.

For what kind of templates
It is recommended to apply the test case pattern to templates using ParserFunctions that can generate very different looking output, such as the ones that take many parameters, or the ones that have many branches of the #switch operator.

For navigational boxes, of which the outputs are almost identical on each transcluding page, the benefit may not outweight the time and effort.

Any template that is transcluded in thousands of pages should of course be tested before an edit is made.

Prerequisite
The template must be properly documented following Template documentation.

Furthermore, the template should not have too many interleaving   and   blocks. A hat note and the documentation in their respestive   block would be appropriate.

How to create sandbox and test case subpages

 * Suppose your template is named Template:X. Create a subpage with the name Template:X/sandbox and another with Template:X/testcases. When both of these subpages are present, the documentation would include links to them.
 * Copy only the code (everything that is not surrounded by  ) from your Template:X</tt> to Template:X/sandbox</tt>. And add the following line at the beginning of the first line, in the same line.
 * Copy a few transcluding examples from articles ( </tt>) to your Template:X/testcases</tt>. In the test case subpage, duplicate each example, and for one copy of each example, replace X</tt> with X/testcases</tt>. This is done so that you may compare the old and new outputs side by side in the future. Lastly, add the following line at the beginning of the test case page. It does not matter how many blank lines you put around it.
 * Copy a few transcluding examples from articles ( </tt>) to your Template:X/testcases</tt>. In the test case subpage, duplicate each example, and for one copy of each example, replace X</tt> with X/testcases</tt>. This is done so that you may compare the old and new outputs side by side in the future. Lastly, add the following line at the beginning of the test case page. It does not matter how many blank lines you put around it.

How to use them subsequently

 * Sync sandbox from the real template (i.e. step 2 above)
 * Have fun editing the sandbox
 * Verify the test cases
 * If everything looks good, import the code from the sandbox to the real template

If you chose not to compare the new and old outputs side by side described at the last bullet point of the previous section, it is recommended to use a tabbed browser and open the test case subpage before changing the sandbox, and another after changing the sandbox. This can ensure that the differences are compared accurately.

Example

 * Infobox Country
 * Infobox Settlement