Help:Template sandbox and test cases
Templates are a very powerful feature of MediaWiki, but mistakes can be easily made, even by experienced users. All templates should therefore be accompanied by sandboxes and test cases to prevent bugs. Testcases are also transcluded in the template's documentation to showcase the usage of the template.
Contents |
How to create /sandbox subpages
1: Suppose your template is named Template:X. Create sandbox and testcases subpages by clicking on the "create" links in the Note box under the documentation. The subpages will have the names Template:X/sandbox and Template:X/testcases and are preloaded with some default contents. When these subpages are present, the Note box automatically provide links to them. Note that "/sandbox" and "/testcases" are spelled in all lower-case.
2: Copy only the code (everything that is not surrounded by <noinclude>) from your Template:X to Template:X/sandbox. Keep the {{Template sandbox notice}} line at the bottom:
<noinclude>
{{Template sandbox notice}}
</noinclude>
In other words, the result should be:
[-- Template code --]<noinclude>
{{Template sandbox notice}}
</noinclude>
</noinclude> tag. Any blank space or newlines between the template code and the </noinclude> tag can cause the template to render unwanted spaces or newlines when used.How to create /testcases subpages
1: Create at least for each parameter one testcase. Use for this the {{Testcase}} template. As the /testcases subpage is transcluded to the /doc subpage to showcase the usage of the template, the testcases, which are for debbuging purpose only (e.g. testcases without manditory parameters), should be set between <noinclude>-tags. 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:
<noinclude>{{Template testcases notice}}</noinclude>
2: Thus Template:X/testcases will look like this:
<noinclude>{{Template testcases notice}}</noinclude>
<noinclude>{{Testcase
|title=No parameter
|{{X}}
|{{X}}
|{{X/sandbox}}
}}
</noinclude>{{Testcase
|title=Parameter: foo
|{{X|foo}}
|{{X |foo}}
|{{X/sandbox|foo}}
}}
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, copy 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
See also
- {{Template sandbox notice}} – To put at the bottom of /sandbox pages.
- {{Template testcases notice}} – To put on top of /testcases pages.