Practise templates

Template sandbox and test cases: change a template safely

A template sandbox is a copy of a template at Template:Name/sandbox where you change the code safely. Its partner, Template:Name/testcases, calls both versions side by side, so you compare the output before copying your change to the live template.

Updated 4 min read
On this page
  1. Why templates need a sandbox
  2. The two subpages
  3. Creating /sandbox and /testcases
  4. Changing a template that already has a sandbox
  5. Writing good test cases
  6. What a testcases page cannot show
  7. Throwaway pages for template experiments
  8. Frequently asked questions

Why templates need a sandbox

One edit to a template changes every page that uses it, and mistakes are easy even for experienced editors. Before touching a live template, open What links here to see how many pages depend on it. The English Wikipedia's guidance is to test in a sandbox with test cases, and always to test templates marked high-risk.

  • Always worth it: templates built with ParserFunctions whose output varies, especially those with many parameters or a long #switch.
  • Often not worth it: navigation boxes that show nearly the same thing on every page, though the advice is still if in doubt, sandbox it.
  • For a brand-new template: build it in your own user space first, then move it.

The two subpages

Where a template's sandbox and test cases live
PageNameHolds
Main templateTemplate:XThe live code every page uses
SandboxTemplate:X/sandboxYour changed copy of the code
Test casesTemplate:X/testcasesCalls of both versions with the same parameters

Both names are all lowercase, and testcases is one word. Most protected templates on the English Wikipedia already have both subpages, linked from the foot of their documentation box.

Creating /sandbox and /testcases

The quickest way is the create and mirror links at the foot of the template's documentation box, which build the subpages for you. By hand it takes five steps:

  1. Create Template:X/sandbox and paste in the whole main template, including its noinclude tags and documentation call. Use an edit summary such as create sandbox version of Template:X.
  2. Create Template:X/testcases. It is linked from the documentation box only once it exists, so type its name in the search box.
  3. Put {{testcases notice}} at the top and add test cases (see below).
  4. Make your change on the sandbox and save it.
  5. Open the testcases page, purge it so the results update, and compare the two versions. If everything matches what you expect, copy the sandbox code to the main template.

Changing a template that already has a sandbox

Someone may have edited the main template since the sandbox was last used, so the first step is always to sync: copy the whole main template into the sandbox and save it with the summary Sync. Then make your change, add test cases for any new parameter or behaviour, purge the testcases page and check every result before copying the code back.

Writing good test cases

Write one test case for each thing the template does differently. If a banner template has a plain message, an italic=yes option and a date parameter, that is three test cases. Testing every combination is rarely practical, so pick the ones editors really use.

Test-case formats used on the English Wikipedia
FormatGood for
Main and Sandbox headings under each callBanners and long text such as citations
A wikitable with Code, Main and Sandbox columnsShort, one-line output that should line up
{{Test case}}General use: parameters are typed once and fed to both versions
{{Testcase table}}Long, thin templates such as infoboxes, side by side
{{Test case nowiki}}Templates with complicated calls
{{Collapsible test case}}Hiding results where both versions already agree

Templates that type the parameters once are safer: with hand-written cases, a typo in one call can look like a bug in the template. For worked examples, see the subpages of {{Quote}}, {{Diff}} and {{Infobox settlement}}.

What a testcases page cannot show

Some behaviour depends on where a template is used: output that appears only in articles, or that differs on subpages. For these, use Preview page with this template, shown under the edit window on template pages: it previews another page using the code in your edit window, before you save anything. Special:TemplateSandbox does the same from its own form, and Special:ExpandTemplates shows the wikitext a call turns into. Leave a note on the testcases page about anything you could not test there.

Throwaway pages for template experiments

  • {{X1}} to {{X20}} and {{Template sandbox}}: temporary templates anyone can overwrite.
  • {{TemplateStyles sandbox}}: for stylesheets with the sanitized-css content model, which can only be created in the Template namespace.
  • Category:X1 to Category:X9: for testing how templates add categories.
  • Lua modules have their own sandboxes and unit tests; see Lua modules and Scribunto.

Frequently asked questions

What is the difference between /sandbox and /testcases?

The /sandbox subpage holds your changed copy of the template's code. The /testcases subpage calls the live template and the sandbox with the same parameters, so you can compare their output.

Why don't my test cases show my change?

The testcases page is probably cached. Purge it (the {{testcases notice}} box has a link) and the results update.

Does every template need test cases?

No. They matter most for templates whose output varies with many parameters or #switch branches. A navigation box that looks the same everywhere may not be worth the effort, though high-risk templates should always be tested.

Where can I practise templates without affecting Wikipedia?

In the temporary templates {{X1}} to {{X20}}, in your user space, or on a MediaWiki installation of your own.

Keep reading

Wiki Practice Guide is an independent guide. It is not run by or affiliated with the Wikimedia Foundation, Wikipedia, MediaWiki.org, any test wiki it describes (including The Test Wiki, once hosted at wiki.xyrael.net) or any previous owner of this domain.