The ScriptTestCase Class | ![]() print-friendly |
Running in JUnit
The easiest way to run scripts in JUnit is to have a standard JUnit test runner run the class com.hjl.junitscript.ScriptTestCase. This will try all files in the current directory and subdirectories that could be scripts and will run them. For example:
java junit.textui.TestRunner com.hjl.junitscript.ScriptTestCase
However, this may be a little excessive since we cannot restrict the the files that will be attempted to be run as scripts.
Specifying the Test Scripts
To change the scripts that are chosen to run, we can set a system property. The property is com.hjl.junitscript.suite and should be set to a comma separated list of file name patterns in the manner of Ant patterns. So, to test all scripts having a parent test and extension bsh or js we can use this invocation:
java -Dcom.hjl.junitscript.suite=**/test/*.bsh,**/test/*.js com.hjl.junitscript.ScriptTestCase
If not otherwise specifed, the system property defaults to **/*
A more refined mechanism involves suite files, described in a later section.
Finding the Scripting Language
Once scripts have been found, the system needs to determine the appropriate scripting language. This done in the following way:
- Let e be the extension of the script filename.
- If e == "suite" then we treat the file specially as described in a later section.
- Examine the properties file found in the class path with name, com/hjl/junitscript/Languages.properties. This file contains pairs of extensions (without the '.') mapped to class names. If there is an extension matching e then the associated class encapsulates the language.
- If no language has been found, then default to BSF to find the language from the filename.
The system then instantiates the language class and asks it to test the script. The language class must derive from com.hjl.junitscript.Language
Currently, extensions that do not fall through to BSF are:
Extension | Language |
---|---|
.bsh | BeanShell |
.js | Javascript |
.suite | Suite files described below |
Suite Files
A suite file is a description of a set of scripts to be run. This mechanism will be generally more suitable than using the property mechanism above, although that should still be used to point out the suite files.
These files have the .suite extension. Their internal syntax is derived from the standard Java property file syntax. Essentially this creates a list of pairs where the first member of a pair is a comma separated list of file patterns (just like the ones used for the test scripts property) and the second member is a list of command line arguments. The same comment syntax and separation syntax as property files is used.
So, to run a list of scripts we might define a suite file called tests.suite, like this:
# A simple set of scripts tests/hello.bsh tests/world.js tests/**/*.py
And run it with:
java -Dcom.hjl.junitscript.suite=tests.suite com.hjl.junitscript.ScriptTestCase
This will run the BeanShell script, tests/hello.bsh, the Javascript file, tests/world.js and any Jython file ending in .py in any subdirectory of tests.
Command Line Arguments
The suite file syntax affords an excellent opportunity to add commandline style arguments to the scripts. This is the second member of the pairs in the suite file.
The second member of a pair in the suite file is parsed into an array of strings according to the normal command line syntax (this parsing is provided by Ant's org.apache.tools.ant.types.Commandline.translateCommandline).
This is then passed to the script language differently according to the language. The following table describes the current ways these arguments are passed:
Language | Arguments Passed By |
---|---|
BeanShell | bsh.args |
JavaScript | arguments |
Suite File | Not passed to sub suite files |
Default BSF | A bean named args is declared |
So we could set up a suite file like this:
# A slightly more complicated suite file tests/hello.bsh = "Argument 1" arg2 'another arg' tests/hello.bsh = Different arguments now tests/*.js,tests/*.py = a big \ long arg list on more than \ one line! tests/*.suite