16.13. Testcase Templates

16.13.1. Creating a New Template
16.13.2. Using a Template
16.13.3. Choosing a Custom Location for Storing Templates

After creating a number of test cases in Squish, you might find that many of them have some basic structure in common. For example, they might all have the same startup code in their main test script. To avoid having to copy and paste common code whenever you create a new testcase, you can instead create a test case template and use it when creating new test cases.

16.13.1. Creating a New Template

Each new test case template is effectively a copy of an existing test. So to create a new test case template, first create a new "dummy" test case, and then copy or type into the test.py (or test.js, etc.) test script exactly—and only—the code that you want in the template. For example, you might create a test case template like this (which happens to use Python, but the same applies in principle to any scripting language):

Python
import os
import re
import sys

def main():
    # Import functionality needed by more than one test script
    source(findFile("scripts", "common.py"))

Here we always import some standard library modules and our own custom file (common.py), that contains functions that we want all our tests to be able to access.

Once we have created the dummy test case with the contents we want to use for the template, we are ready to create the template itself. Right click the dummy test to pop up the context menu and choose the Use As Testcase Template option. This will cause a small dialog to pop up, asking you for a name for the template—enter a suitably descriptive name, then press the OK button. (If you enter the name of a template that already exists, Squish will tell you and ask you to enter a different name.)

Once the test case template has been created it is safe to delete the dummy test case.

16.13.2. Using a Template

Once we have created one or more templates we can use them whenever we want to create a new test case. This is easily done by choosing File|New Test Case From Template... from the menu bar (or by choosing the menu item with the same name from the context menu of test suite items).

Assuming you have created at least one test case template—if you haven't, a message box will say so—a small dialog will pop up with a combobox containing the names of all the test case templates. Simply select the one you want to use as the basis for your new test case, then press the OK button.

As a result, Squish will create a new test case in the currently active test suite—you must give the test case a name in the tree view by clicking the empty name, typing in the name you want to use, and then pressing Enter to confirm the creation. The new test case will have a test.py (or test.js, etc.) test script with exactly the same contents as the test case template you chose.

16.13.3. Choosing a Custom Location for Storing Templates

By default, test case templates are stored in %APPDATA%\froglogic\Squish\templates on Windows, or ~/.squish/templates on Unix-like systems (Linux, Mac OS X, etc.). This makes it possible to reuse test case templates between different test suites.

In some situations it might be better to store test case templates together with the test suites they have been created to be used with. For example, a test suite might be stored in a version control system and you might want all the test case templates to be stored with the suite so that every user who downloads the test suite from the version control system can use exactly the same test case templates.

This can be achieved by creating a test suite-specific directory in which the templates should be stored and setting the SQUISH_TESTCASE_TEMPLATES_DIR environment variable to this directory. When the Squish IDE starts up it checks for the existence of this environment variable; if it doesn't exist, Squish uses the default directory mentioned earlier, but if it does exist, Squish uses the specified directory.

[Note]Note

Since the SQUISH_TESTCASE_TEMPLATES_DIR controls an aspect of the Squish IDE's behavior it cannot be set inside the Squish IDE (so setting it in the Test Suite Settings environment variables tab won't work). Instead it must be set before Squish IDE is run—perhaps by starting the Squish IDE from a shell script or batch file that first sets up the environment and then runs the Squish IDE.