| org.apache.tools.ant.Task org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
JUnitTask | public class JUnitTask extends Task (Code) | | Runs JUnit tests.
JUnit is a framework to create unit tests. It has been initially
created by Erich Gamma and Kent Beck. JUnit can be found at http://www.junit.org.
JUnitTask can run a single specific
JUnitTest using the test element.
For example, the following target
<target name="test-int-chars" depends="jar-test">
<echo message="testing international characters"/>
<junit printsummary="no" haltonfailure="yes" fork="false">
<classpath refid="classpath"/>
<formatter type="plain" usefile="false" />
<test name="org.apache.ecs.InternationalCharTest" />
</junit>
</target>
runs a single junit test
(org.apache.ecs.InternationalCharTest ) in the current
VM using the path with id classpath as classpath and
presents the results formatted using the standard
plain formatter on the command line.
This task can also run batches of tests. The
batchtest element creates a BatchTest
based on a fileset. This allows, for example, all classes found in
directory to be run as testcases.
For example,
<target name="run-tests" depends="dump-info,compile-tests" if="junit.present">
<junit printsummary="no" haltonfailure="yes" fork="${junit.fork}">
<jvmarg value="-classic"/>
<classpath refid="tests-classpath"/>
<sysproperty key="build.tests" value="${build.tests}"/>
<formatter type="brief" usefile="false" />
<batchtest>
<fileset dir="${tests.dir}">
<include name="**/*Test*" />
</fileset>
</batchtest>
</junit>
</target>
this target finds any classes with a test directory
anywhere in their path (under the top ${tests.dir} , of
course) and creates JUnitTest 's for each one.
Of course, <junit> and
<batch> elements can be combined for more
complex tests. For an example, see the ant build.xml
target run-tests (the second example is an edited
version).
To spawn a new Java VM to prevent interferences between
different testcases, you need to enable fork . A
number of attributes and elements allow you to set up how this JVM
runs.
since: Ant 1.2 See Also: JUnitTest See Also: BatchTest |
Inner Class :protected class TestResultHolder | |
Inner Class :protected static class JUnitLogOutputStream extends LogOutputStream | |
Constructor Summary | |
public | JUnitTask() Creates a new JUnitRunner and enables fork of a new Java VM. |
Method Summary | |
protected void | actOnTestResult(int exitValue, boolean wasKilled, JUnitTest test, String name) Logs information about failed tests, potentially stops
processing (by throwing a BuildException) if a failure/error
occurred or sets a property. | protected void | actOnTestResult(TestResultHolder result, JUnitTest test, String name) Logs information about failed tests, potentially stops
processing (by throwing a BuildException) if a failure/error
occurred or sets a property. | public void | addAssertions(Assertions asserts) | protected boolean | addClasspathEntry(String resource) Search for the given resource and add the directory or archive
that contains it to the classpath. | public void | addConfiguredSysproperty(Environment.Variable sysp) Adds a system property that tests can access. | public void | addEnv(Environment.Variable var) Adds an environment variable; used when forking. | public void | addFormatter(FormatterElement fe) Add a new formatter to all tests of this task. | public void | addSysproperty(Environment.Variable sysp) Adds a system property that tests can access. | public void | addSyspropertyset(PropertySet sysp) Adds a set of properties that will be used as system properties
that tests can access. | public void | addTest(JUnitTest test) Add a new single testcase. | protected Enumeration | allTests() | public BatchTest | createBatchTest() Adds a set of tests based on pattern matching. | public Path | createBootclasspath() Adds a path to the bootclasspath. | public Path | createClasspath() Adds path to classpath used for tests. | public Commandline.Argument | createJvmarg() Adds a JVM argument; ignored if not forking. | public Permissions | createPermissions() Sets the permissions for the application run inside the same JVM. | protected ExecuteWatchdog | createWatchdog() | public void | execute() Runs the testcase. | protected void | execute(JUnitTest arg) Run the tests. | protected void | execute(List tests) Execute a list of tests in a single forked Java VM. | protected Collection | executeOrQueue(Enumeration testList, boolean runIndividual) Executes all tests that don't need to be forked (or all tests
if the runIndividual argument is true. | protected CommandlineJava | getCommandline() Get the command line used to run the tests. | protected OutputStream | getDefaultOutput() Get the default output for a formatter. | protected Enumeration | getIndividualTests() Merge all individual tests from the batchtest with all individual tests
and return an enumeration over all JUnitTest. | protected File | getOutput(FormatterElement fe, JUnitTest test) If the formatter sends output to a file, return that file. | public void | handleErrorFlush(String output) Pass output sent to System.err to the TestRunner so it can
collect it for the formatters. | public void | handleErrorOutput(String output) Pass output sent to System.err to the TestRunner so it can
collect it for the formatters. | protected void | handleFlush(String output) Pass output sent to System.out to the TestRunner so it can
collect ot for the formatters. | protected int | handleInput(byte[] buffer, int offset, int length) Handle an input request by this task.
See Also: Task.handleInput(byte[]intint) See Also: This implementation delegates to a runner if it See Also: present. Parameters: buffer - the buffer into which data is to be read. Parameters: offset - the offset into the buffer at which data is stored. Parameters: length - the amount of data to read. | protected void | handleOutput(String output) Pass output sent to System.out to the TestRunner so it can
collect ot for the formatters. | public void | init() Adds the jars or directories containing Ant, this task and
JUnit to the classpath - this should make the forked JVM work
without having to specify them directly. | public void | setCloneVm(boolean cloneVm) If set, system properties will be copied to the cloned VM - as
well as the bootclasspath unless you have explicitly specified
a bootclaspath. | public void | setDir(File dir) The directory to invoke the VM in. | public void | setErrorProperty(String propertyName) Property to set to "true" if there is a error in a test. | public void | setFailureProperty(String propertyName) Property to set to "true" if there is a failure in a test. | public void | setFiltertrace(boolean value) If true, smartly filter the stack frames of
JUnit errors and failures before reporting them. | public void | setFork(boolean value) If true, JVM should be forked for each test.
It avoids interference between testcases and possibly avoids
hanging the build. | public void | setForkMode(ForkMode mode) Set the behavior when
JUnitTask.setFork fork fork has been enabled.
Possible values are "once", "perTest" and "perBatch". | public void | setHaltonerror(boolean value) If true, stop the build process when there is an error in a test. | public void | setHaltonfailure(boolean value) If true, stop the build process if a test fails
(errors are considered failures as well). | public void | setIncludeantruntime(boolean b) If true, include ant.jar, optional.jar and junit.jar in the forked VM. | public void | setJvm(String value) The command used to invoke the Java Virtual Machine,
default is 'java'. | public void | setMaxmemory(String max) Set the maximum memory to be used by all forked JVMs. | public void | setNewenvironment(boolean newenv) If true, use a new environment when forked. | public void | setOutputToFormatters(boolean outputToFormatters) If true, send any output generated by tests to the formatters. | public void | setPrintsummary(SummaryAttribute value) If true, print one-line statistics for each test, or "withOutAndErr"
to also show standard output and error. | public void | setReloading(boolean value) | public void | setShowOutput(boolean showOutput) If true, send any output generated by tests to Ant's logging system
as well as to the formatters.
By default only the formatters receive the output.
Output will always be passed to the formatters and not by
shown by default. | public void | setTempdir(File tmpDir) Where Ant should place temporary files. | public void | setTimeout(Integer value) Set the timeout value (in milliseconds).
If the test is running for more than this value, the test
will be canceled. |
TESTLISTENER_PREFIX | final public static String TESTLISTENER_PREFIX(Code) | | since: Ant 1.7 |
JUnitTask | public JUnitTask() throws Exception(Code) | | Creates a new JUnitRunner and enables fork of a new Java VM.
throws: Exception - under ??? circumstances since: Ant 1.2 |
actOnTestResult | protected void actOnTestResult(int exitValue, boolean wasKilled, JUnitTest test, String name)(Code) | | Logs information about failed tests, potentially stops
processing (by throwing a BuildException) if a failure/error
occurred or sets a property.
Parameters: exitValue - the exitValue of the test. Parameters: wasKilled - if true, the test had been killed. Parameters: test - the test in question. Parameters: name - the name of the test. since: Ant 1.6.2 |
actOnTestResult | protected void actOnTestResult(TestResultHolder result, JUnitTest test, String name)(Code) | | Logs information about failed tests, potentially stops
processing (by throwing a BuildException) if a failure/error
occurred or sets a property.
Parameters: result - the result of the test. Parameters: test - the test in question. Parameters: name - the name of the test. since: Ant 1.7 |
addAssertions | public void addAssertions(Assertions asserts)(Code) | | Assertions to enable in this program (if fork=true)
since: Ant 1.6 Parameters: asserts - assertion set |
addClasspathEntry | protected boolean addClasspathEntry(String resource)(Code) | | Search for the given resource and add the directory or archive
that contains it to the classpath.
Doesn't work for archives in JDK 1.1 as the URL returned by
getResource doesn't contain the name of the archive.
Parameters: resource - resource that one wants to lookup true if something was in fact added since: Ant 1.4 |
addConfiguredSysproperty | public void addConfiguredSysproperty(Environment.Variable sysp)(Code) | | Adds a system property that tests can access.
This might be useful to tranfer Ant properties to the
testcases when JVM forking is not enabled.
Parameters: sysp - new environment variable to add since: Ant 1.6 |
addEnv | public void addEnv(Environment.Variable var)(Code) | | Adds an environment variable; used when forking.
Will be ignored if we are not forking a new VM.
Parameters: var - environment variable to be added since: Ant 1.5 |
addFormatter | public void addFormatter(FormatterElement fe)(Code) | | Add a new formatter to all tests of this task.
Parameters: fe - formatter element since: Ant 1.2 |
addSysproperty | public void addSysproperty(Environment.Variable sysp)(Code) | | Adds a system property that tests can access.
This might be useful to tranfer Ant properties to the
testcases when JVM forking is not enabled.
since: Ant 1.3 Parameters: sysp - environment variable to add |
addSyspropertyset | public void addSyspropertyset(PropertySet sysp)(Code) | | Adds a set of properties that will be used as system properties
that tests can access.
This might be useful to tranfer Ant properties to the
testcases when JVM forking is not enabled.
Parameters: sysp - set of properties to be added since: Ant 1.6 |
addTest | public void addTest(JUnitTest test)(Code) | | Add a new single testcase.
Parameters: test - a new single testcase See Also: JUnitTest since: Ant 1.2 |
allTests | protected Enumeration allTests()(Code) | | return an enumeration listing each test, then each batchtest
enumeration since: Ant 1.3 |
createBatchTest | public BatchTest createBatchTest()(Code) | | Adds a set of tests based on pattern matching.
a new instance of a batch test. See Also: BatchTest since: Ant 1.2 |
createBootclasspath | public Path createBootclasspath()(Code) | | Adds a path to the bootclasspath.
reference to the bootclasspath in the embedded java command line since: Ant 1.6 |
createClasspath | public Path createClasspath()(Code) | | Adds path to classpath used for tests.
reference to the classpath in the embedded java command line since: Ant 1.2 |
createPermissions | public Permissions createPermissions()(Code) | | Sets the permissions for the application run inside the same JVM.
since: Ant 1.6 . |
execute | protected void execute(List tests) throws BuildException(Code) | | Execute a list of tests in a single forked Java VM.
Parameters: tests - the list of tests to execute. throws: BuildException - on error. |
executeOrQueue | protected Collection executeOrQueue(Enumeration testList, boolean runIndividual)(Code) | | Executes all tests that don't need to be forked (or all tests
if the runIndividual argument is true. Returns a collection of
lists of tests that share the same VM configuration and haven't
been executed yet.
Parameters: testList - the list of tests to be executed or queued. Parameters: runIndividual - if true execute each test individually. a list of tasks to be executed. since: 1.6.2 |
getCommandline | protected CommandlineJava getCommandline()(Code) | | Get the command line used to run the tests.
the command line. since: Ant 1.6.2 |
getDefaultOutput | protected OutputStream getDefaultOutput()(Code) | | Get the default output for a formatter.
default output stream for a formatter since: Ant 1.3 |
getIndividualTests | protected Enumeration getIndividualTests()(Code) | | Merge all individual tests from the batchtest with all individual tests
and return an enumeration over all JUnitTest.
enumeration over individual tests since: Ant 1.3 |
getOutput | protected File getOutput(FormatterElement fe, JUnitTest test)(Code) | | If the formatter sends output to a file, return that file.
null otherwise.
Parameters: fe - formatter element Parameters: test - one JUnit test file reference since: Ant 1.3 |
handleErrorFlush | public void handleErrorFlush(String output)(Code) | | Pass output sent to System.err to the TestRunner so it can
collect it for the formatters.
Parameters: output - coming from System.err since: Ant 1.5.2 |
handleErrorOutput | public void handleErrorOutput(String output)(Code) | | Pass output sent to System.err to the TestRunner so it can
collect it for the formatters.
Parameters: output - output coming from System.err since: Ant 1.5 |
handleFlush | protected void handleFlush(String output)(Code) | | Pass output sent to System.out to the TestRunner so it can
collect ot for the formatters.
Parameters: output - output coming from System.out since: Ant 1.5.2 |
handleInput | protected int handleInput(byte[] buffer, int offset, int length) throws IOException(Code) | | Handle an input request by this task.
See Also: Task.handleInput(byte[]intint) See Also: This implementation delegates to a runner if it See Also: present. Parameters: buffer - the buffer into which data is to be read. Parameters: offset - the offset into the buffer at which data is stored. Parameters: length - the amount of data to read. the number of bytes read. exception: IOException - if the data cannot be read. since: Ant 1.6 |
handleOutput | protected void handleOutput(String output)(Code) | | Pass output sent to System.out to the TestRunner so it can
collect ot for the formatters.
Parameters: output - output coming from System.out since: Ant 1.5 |
init | public void init()(Code) | | Adds the jars or directories containing Ant, this task and
JUnit to the classpath - this should make the forked JVM work
without having to specify them directly.
since: Ant 1.4 |
setCloneVm | public void setCloneVm(boolean cloneVm)(Code) | | If set, system properties will be copied to the cloned VM - as
well as the bootclasspath unless you have explicitly specified
a bootclaspath.
Doesn't have any effect unless fork is true.
Parameters: cloneVm - a boolean value. since: Ant 1.7 |
setDir | public void setDir(File dir)(Code) | | The directory to invoke the VM in. Ignored if no JVM is forked.
Parameters: dir - the directory to invoke the JVM from. See Also: JUnitTask.setFork(boolean) since: Ant 1.2 |
setErrorProperty | public void setErrorProperty(String propertyName)(Code) | | Property to set to "true" if there is a error in a test.
This property is applied on all BatchTest (batchtest) and
JUnitTest (test), however, it can possibly be overriden by
their own properties.
Parameters: propertyName - the name of the property to set in theevent of an error. since: Ant 1.4 |
setFailureProperty | public void setFailureProperty(String propertyName)(Code) | | Property to set to "true" if there is a failure in a test.
This property is applied on all BatchTest (batchtest) and
JUnitTest (test), however, it can possibly be overriden by
their own properties.
Parameters: propertyName - the name of the property to set in theevent of an failure. since: Ant 1.4 |
setFiltertrace | public void setFiltertrace(boolean value)(Code) | | If true, smartly filter the stack frames of
JUnit errors and failures before reporting them.
This property is applied on all BatchTest (batchtest) and
JUnitTest (test) however it can possibly be overridden by their
own properties.
Parameters: value - false if it should not filter, otherwisetrue since: Ant 1.5 |
setFork | public void setFork(boolean value)(Code) | | If true, JVM should be forked for each test.
It avoids interference between testcases and possibly avoids
hanging the build. this property is applied on all BatchTest
(batchtest) and JUnitTest (test) however it can possibly be
overridden by their own properties.
Parameters: value - true if a JVM should be forked, otherwisefalse See Also: JUnitTask.setTimeout since: Ant 1.2 |
setForkMode | public void setForkMode(ForkMode mode)(Code) | | Set the behavior when
JUnitTask.setFork fork fork has been enabled.
Possible values are "once", "perTest" and "perBatch". If
set to "once", only a single Java VM will be forked for all
tests, with "perTest" (the default) each test will run in a
fresh Java VM and "perBatch" will run all tests from the same
<batchtest> in the same Java VM.
This attribute will be ignored if tests run in the same VM
as Ant.
Only tests with the same configuration of haltonerror,
haltonfailure, errorproperty, failureproperty and filtertrace
can share a forked Java VM, so even if you set the value to
"once", Ant may need to fork mutliple VMs.
Parameters: mode - the mode to use. since: Ant 1.6.2 |
setHaltonerror | public void setHaltonerror(boolean value)(Code) | | If true, stop the build process when there is an error in a test.
This property is applied on all BatchTest (batchtest) and JUnitTest
(test) however it can possibly be overridden by their own
properties.
Parameters: value - true if it should halt, otherwisefalse since: Ant 1.2 |
setHaltonfailure | public void setHaltonfailure(boolean value)(Code) | | If true, stop the build process if a test fails
(errors are considered failures as well).
This property is applied on all BatchTest (batchtest) and
JUnitTest (test) however it can possibly be overridden by their
own properties.
Parameters: value - true if it should halt, otherwisefalse since: Ant 1.2 |
setIncludeantruntime | public void setIncludeantruntime(boolean b)(Code) | | If true, include ant.jar, optional.jar and junit.jar in the forked VM.
Parameters: b - include ant run time yes or no since: Ant 1.5 |
setJvm | public void setJvm(String value)(Code) | | The command used to invoke the Java Virtual Machine,
default is 'java'. The command is resolved by
java.lang.Runtime.exec(). Ignored if fork is disabled.
Parameters: value - the new VM to use instead of java See Also: JUnitTask.setFork(boolean) since: Ant 1.2 |
setMaxmemory | public void setMaxmemory(String max)(Code) | | Set the maximum memory to be used by all forked JVMs.
Parameters: max - the value as defined by -mx or -Xmxin the java command line options. since: Ant 1.2 |
setNewenvironment | public void setNewenvironment(boolean newenv)(Code) | | If true, use a new environment when forked.
Will be ignored if we are not forking a new VM.
Parameters: newenv - boolean indicating if setting a new environment is wished since: Ant 1.5 |
setOutputToFormatters | public void setOutputToFormatters(boolean outputToFormatters)(Code) | | If true, send any output generated by tests to the formatters.
Parameters: outputToFormatters - if true, send output to formatters (Defaultis true). since: Ant 1.7.0 |
setPrintsummary | public void setPrintsummary(SummaryAttribute value)(Code) | | If true, print one-line statistics for each test, or "withOutAndErr"
to also show standard output and error.
Can take the values on, off, and withOutAndErr.
Parameters: value - true to print a summary,withOutAndErr to include the test's output aswell, false otherwise. See Also: SummaryJUnitResultFormatter since: Ant 1.2 |
setReloading | public void setReloading(boolean value)(Code) | | If true, force ant to re-classload all classes for each JUnit TestCase
Parameters: value - force class reloading for each test case |
setShowOutput | public void setShowOutput(boolean showOutput)(Code) | | If true, send any output generated by tests to Ant's logging system
as well as to the formatters.
By default only the formatters receive the output.
Output will always be passed to the formatters and not by
shown by default. This option should for example be set for
tests that are interactive and prompt the user to do
something.
Parameters: showOutput - if true, send output to Ant's logging system too since: Ant 1.5 |
setTempdir | public void setTempdir(File tmpDir)(Code) | | Where Ant should place temporary files.
Parameters: tmpDir - location where temporary files should go to since: Ant 1.6 |
setTimeout | public void setTimeout(Integer value)(Code) | | Set the timeout value (in milliseconds).
If the test is running for more than this value, the test
will be canceled. (works only when in 'fork' mode).
Parameters: value - the maximum time (in milliseconds) allowed beforedeclaring the test as 'timed-out' See Also: JUnitTask.setFork(boolean) since: Ant 1.2 |
|
|