source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/docs/manual/OptionalTasks/junit.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 20.5 KB
Line 
1<html>
2<head>
3<title>JUnit Task</title>
4<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
5</head>
6<body>
7
8<h2><a name="junit">JUnit</a></h2>
9<h3>Description</h3>
10
11<p>This task runs tests from the JUnit testing framework. The latest
12version of the framework can be found at
13<a href="http://www.junit.org">http://www.junit.org</a>.
14This task has been tested with JUnit 3.0 up to JUnit 3.8.1; it won't
15work with versions prior to JUnit 3.0.</p>
16<p><strong>Note:</strong> This task depends on external libraries not included
17in the Ant distribution. See <a href="../install.html#librarydependencies">
18Library Dependencies</a> for more information.
19</p>
20<p>
21<strong>Note</strong>:
22You must have <code>junit.jar</code> and the class files for the
23<code>&lt;junit&gt;</code> task in the same classpath.
24You can do one of:
25<ol>
26<li>
27Put both <code>junit.jar</code> and the optional tasks jar file in
28<code>ANT_HOME/lib</code>.
29</li>
30<li>
31Do not put either in <code>ANT_HOME/lib</code>, and instead
32include their locations in your <code>CLASSPATH</code> environment variable.
33</li>
34<li>
35Do neither of the above, and instead, specify their locations using
36a <code>&lt;classpath&gt;</code> element in the build file.
37
38See <a href="../../faq.html#delegating-classloader" target="_top">the
39FAQ</a> for details.
40</ol>
41</p>
42
43
44<p>Tests are defined by nested <code>test</code> or
45<code>batchtest</code> tags (see <a href="#nested">nested
46elements</a>).</p>
47
48<h3>Parameters</h3>
49<table border="1" cellpadding="2" cellspacing="0">
50<tr>
51 <td width="12%" valign="top"><b>Attribute</b></td>
52 <td width="78%" valign="top"><b>Description</b></td>
53 <td width="10%" valign="top"><b>Required</b></td>
54</tr>
55 <tr>
56 <td valign="top">printsummary</td>
57 <td valign="top">Print one-line statistics for each testcase. Can
58 take the values <code>on</code>,
59 <code>off</code>, and
60 <code>withOutAndErr</code>.
61 <code>withOutAndErr</code> is the same
62 as <code>on</code> but also includes the output of the test
63 as written to <code>System.out</code> and <code>System.err</code>.</td>
64 <td align="center" valign="top">No; default is <code>off</code>.</td>
65 </tr>
66 <tr>
67 <td valign="top">fork</td>
68 <td valign="top">Run the tests in a separate VM.</td>
69 <td align="center" valign="top">No; default is <code>off</code>.</td>
70 </tr>
71 <tr>
72 <td valign="top">forkmode</td>
73 <td valign="top">Controls how many Java Virtual Machines get
74 created if you want to fork some tests. Possible values are
75 &quot;perTest&quot; (the default), &quot;perBatch&quot; and
76 &quot;once&quot;. &quot;once&quot; creates only a single Java VM
77 for all tests while &quot;perTest&quot; creates a new VM for each
78 TestCase class. &quot;perBatch&quot; creates a VM for each nested
79 <code>&lt;batchtest&gt;</code> and one collecting all nested
80 <code>&lt;test&gt;</code>s. Note that only tests with the same
81 settings of <code>filtertrace</code>, <code>haltonerror</code>,
82 <code>haltonfailure</code>, <code>errorproperty</code> and
83 <code>failureproperty</code> can share a VM, so even if you set
84 <code>forkmode</code> to &quot;once&quot;, Ant may have to create
85 more than a single Java VM. This attribute is ignored for tests
86 that don't get forked into a new Java VM. <em>since Ant 1.6.2</em></td>
87 <td align="center" valign="top">No; default is <code>perTest</code>.</td>
88 </tr>
89 <tr>
90 <td valign="top">haltonerror</td>
91 <td valign="top">Stop the build process if an error occurs during the test
92 run.</td>
93 <td align="center" valign="top">No; default is <code>off</code>.</td>
94 </tr>
95<tr>
96 <td valign="top">errorproperty</td>
97 <td valign="top">The name of a property to set in the event of an error.</td>
98 <td align="center" valign="top">No</td>
99</tr>
100 <tr>
101 <td valign="top">haltonfailure</td>
102 <td valign="top">Stop the build process if a test fails (errors are
103 considered failures as well).</td>
104 <td align="center" valign="top">No; default is <code>off</code>.</td>
105 </tr>
106<tr>
107 <td valign="top">failureproperty</td>
108 <td valign="top">The name of a property to set in the event of a failure
109 (errors are considered failures as well).</td>
110 <td align="center" valign="top">No.</td>
111</tr>
112 <tr>
113 <td valign="top">filtertrace</td>
114 <td valign="top">Filter out Junit and Ant stack frames from error and failure stack traces.</td>
115 <td align="center" valign="top">No; default is <code>on</code>.</td>
116 </tr>
117 <tr>
118 <td valign="top">timeout</td>
119 <td valign="top">Cancel the individual tests if they don't finish
120 in the given time (measured in milliseconds). Ignored if
121 <code>fork</code> is disabled.</td>
122 <td align="center" valign="top">No</td>
123 </tr>
124 <tr>
125 <td valign="top">maxmemory</td>
126 <td valign="top">Maximum amount of memory to allocate to the forked VM.
127 Ignored if <code>fork</code> is disabled.</td>
128 <td align="center" valign="top">No</td>
129 </tr>
130 <tr>
131 <td valign="top">jvm</td>
132 <td valign="top">The command used to invoke the Java Virtual Machine,
133 default is 'java'. The command is resolved by
134 <code>java.lang.Runtime.exec()</code>.
135 Ignored if <code>fork</code> is disabled.</td>
136 <td align="center" valign="top">No; default is <code>java</code>.</td>
137 </tr>
138 <tr>
139 <td valign="top">dir</td>
140 <td valign="top">The directory in which to invoke the VM. Ignored if
141 <code>fork</code> is disabled.</td>
142 <td align="center" valign="top">No</td>
143 </tr>
144 <tr>
145 <td valign="top">newenvironment</td>
146 <td valign="top">Do not propagate the old environment when new
147 environment variables are specified. Ignored if <code>fork</code> is
148 disabled.</td>
149 <td align="center" valign="top">No; default is <code>false</code>.</td>
150 </tr>
151 <tr>
152 <td valign="top">includeantruntime</td>
153 <td valign="top">Implicitly add the Ant classes required to run
154 the tests and JUnit to the classpath in forked mode.
155 <b>Note:</b> Please read the <a
156 href="../../faq.html#junit-no-runtime-xml">Ant FAQ</a> if you
157 want to set this to <code>false</code> and use the XML formatter
158 at the same time.</td>
159 <td align="center" valign="top">No; default is <code>true</code>.</td>
160 </tr>
161 <tr>
162 <td valign="top">showoutput</td>
163 <td valign="top">Send any output generated by tests to Ant's
164 logging system as well as to the formatters. By default only the
165 formatters receive the output.</td>
166 <td align="center" valign="top">No</td>
167 </tr>
168 <tr>
169 <td valign="top">tempdir</td>
170 <td valign="top">Where Ant should place temporary files.
171 <em>Since Ant 1.6</em>.</td>
172 <td align="center" valign="top">No; default is the project's base
173 directory.</td>
174 </tr>
175 <tr>
176 <td valign="top">reloading</td>
177 <td valign="top">Whether or not a new classloader should be instantiated for each test case.<br/>
178 Ignore if <code>fork</code> is set to true.
179 <em>Since Ant 1.6</em>.</td>
180 <td align="center" valign="top">No; default is <code>true</code>.</td>
181 </tr>
182</table>
183
184<p>By using the <code>errorproperty</code> and <code>failureproperty</code>
185attributes, it is possible to
186perform setup work (such as starting an external server), execute the test,
187clean up, and still fail the build in the event of a failure.</p>
188
189<p>The <code>filtertrace</code> attribute condenses error and failure
190stack traces before reporting them.
191It works with both the plain and XML formatters. It filters out any lines
192that begin with the following string patterns:<pre>
193 "junit.framework.TestCase"
194 "junit.framework.TestResult"
195 "junit.framework.TestSuite"
196 "junit.framework.Assert."
197 "junit.swingui.TestRunner"
198 "junit.awtui.TestRunner"
199 "junit.textui.TestRunner"
200 "java.lang.reflect.Method.invoke("
201 "sun.reflect."
202 "org.apache.tools.ant."</pre></p>
203
204<h3><a name="nested">Nested Elements</a></h3>
205
206<p>The <code>&lt;junit&gt;</code> task
207supports a nested <code>&lt;classpath&gt;</code>
208element that represents a <a href="../using.html#path">PATH like
209structure</a>.</p>
210
211<h4>jvmarg</h4>
212
213<p>If <code>fork</code> is enabled, additional parameters may be passed to
214the new VM via nested <code>&lt;jvmarg&gt;</code> elements. For example:</p>
215
216<pre>
217&lt;junit fork=&quot;yes&quot;&gt;
218 &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
219 ...
220&lt;/junit&gt;
221</pre>
222
223<p>would run the test in a VM without JIT.</p>
224
225<p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
226href="../using.html#arg">Command-line Arguments</a>.</p>
227
228<h4>sysproperty</h4>
229
230<p>Use nested <code>&lt;sysproperty&gt;</code> elements to specify system
231properties required by the class. These properties will be made available
232to the VM during the execution of the test (either ANT's VM or the forked VM,
233if <code>fork</code> is enabled).
234The attributes for this element are the same as for <a href="../CoreTasks/exec.html#env">environment variables</a>.</p>
235
236<pre>
237&lt;junit fork=&quot;no&quot;&gt;
238 &lt;sysproperty key=&quot;basedir&quot; value=&quot;${basedir}&quot;/&gt;
239 ...
240&lt;/junit&gt;
241</pre>
242
243<p>would run the test in ANT's VM and make the <code>basedir</code> property
244available to the test.</p>
245
246<h4>syspropertyset</h4>
247
248<p>You can specify a set of properties to be used as system properties
249with <a href="../CoreTypes/propertyset.html">syspropertyset</a>s.</p>
250
251<p><em>since Ant 1.6</em>.</p>
252
253<h4>env</h4>
254
255<p>It is possible to specify environment variables to pass to the
256forked VM via nested <code>&lt;env&gt;</code> elements. For a description
257of the <code>&lt;env&gt;</code> element's attributes, see the
258description in the <a href="../CoreTasks/exec.html#env">exec</a> task.</p>
259
260<p>Settings will be ignored if <code>fork</code> is disabled.</p>
261
262<h4>bootclasspath</h4>
263
264<p>The location of bootstrap class files can be specified using this
265<a href="../using.html#path">PATH like structure</a> - will be ignored
266if <i>fork</i> is not <code>true</code> or the target VM doesn't
267support it (i.e. Java 1.1).</p>
268
269<p><em>since Ant 1.6</em>.</p>
270
271<h4>permissions</h4>
272<p>Security permissions can be revoked and granted during the execution of the
273class via a nested <i>permissions</i> element. For more information please
274see <a href="../CoreTypes/permissions.html">permissions</a></p>
275
276<p>Settings will be ignored if fork is enabled.</p>
277
278<p><em>since Ant 1.6</em>.</p>
279
280<h4>assertions</h4>
281
282<p>You can control enablement of Java 1.4 assertions with an
283<a href="../CoreTypes/assertions.html"><tt>&lt;assertions&gt;</tt></a>
284subelement.</p>
285
286<p>Assertion statements are currently ignored in non-forked mode.</p>
287
288<p><em>since Ant 1.6.</em></p>
289
290<h4>formatter</h4>
291
292<p>The results of the tests can be printed in different
293formats. Output will always be sent to a file, unless you set the
294<code>usefile</code> attribute to <code>false</code>.
295The name of the file is determined by the
296name of the test and can be set by the <code>outfile</code> attribute
297of <code>&lt;test&gt;</code>.</p>
298
299<p>There are three predefined formatters - one prints the test results
300in XML format, the other emits plain text. The formatter named
301<code>brief</code> will only print detailed information for testcases
302that failed, while <code>plain</code> gives a little statistics line
303for all test cases. Custom formatters that need to implement
304<code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
305can be specified.</p>
306
307<p>If you use the XML formatter, it may not include the same output
308that your tests have written as some characters are illegal in XML
309documents and will be dropped.</p>
310
311<p><b>Note:</b> Please read the <a
312href="../../faq.html#junit-no-runtime-xml">Ant FAQ</a> if you want to
313set the fork attribute to <code>true</code>, the includeAntRuntime
314attribute to <code>false</code> and use the XML formatter at the same
315time.</p>
316
317<table border="1" cellpadding="2" cellspacing="0">
318<tr>
319 <td width="12%" valign="top"><b>Attribute</b></td>
320 <td width="78%" valign="top"><b>Description</b></td>
321 <td width="10%" valign="top"><b>Required</b></td>
322</tr>
323 <tr>
324 <td valign="top">type</td>
325 <td valign="top">Use a predefined formatter (either
326 <code>xml</code>, <code>plain</code>, or <code>brief</code>).</td>
327 <td align="center" rowspan="2">Exactly one of these.</td>
328 </tr>
329 <tr>
330 <td valign="top">classname</td>
331 <td valign="top">Name of a custom formatter class.</td>
332 </tr>
333 <tr>
334 <td valign="top">extension</td>
335 <td valign="top">Extension to append to the output filename.</td>
336 <td align="center">Yes, if <code>classname</code> has been used.</td>
337 </tr>
338 <tr>
339 <td valign="top">usefile</td>
340 <td valign="top">Boolean that determines whether output should be
341 sent to a file.</td>
342 <td align="center">No; default is <code>true</code>.</td>
343 </tr>
344 <tr>
345 <td valign="top">if</td>
346 <td valign="top">Only use formatter if the named property is set.</td>
347 <td align="center">No; default is <code>true</code>.</td>
348 </tr>
349 <tr>
350 <td valign="top">unless</td>
351 <td valign="top">Only use formatter if the named property is <b>not</b> set.</td>
352 <td align="center">No; default is <code>true</code>.</td>
353 </tr>
354</table>
355
356<h4>test</h4>
357
358<p>Defines a single test class.</p>
359
360<table border="1" cellpadding="2" cellspacing="0">
361<tr>
362 <td width="12%" valign="top"><b>Attribute</b></td>
363 <td width="78%" valign="top"><b>Description</b></td>
364 <td width="10%" valign="top"><b>Required</b></td>
365</tr>
366 <tr>
367 <td valign="top">name</td>
368 <td valign="top">Name of the test class.</td>
369 <td align="center">Yes</td>
370 </tr>
371 <tr>
372 <td valign="top">fork</td>
373 <td valign="top">Run the tests in a separate VM.
374 Overrides value set in <code>&lt;junit&gt;</code>.</td>
375 <td align="center" valign="top">No</td>
376 </tr>
377 <tr>
378 <td valign="top">haltonerror</td>
379 <td valign="top">Stop the build process if an error occurs during the test
380 run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
381 <td align="center" valign="top">No</td>
382 </tr>
383<tr>
384 <td valign="top">errorproperty</td>
385 <td valign="top">The name of a property to set in the event of an error.
386 Overrides value set in <code>&lt;junit&gt;</code>.</td>
387 <td align="center" valign="top">No</td>
388</tr>
389 <tr>
390 <td valign="top">haltonfailure</td>
391 <td valign="top">Stop the build process if a test fails (errors are
392 considered failures as well). Overrides value set in
393 <code>&lt;junit&gt;</code>.</td>
394 <td align="center" valign="top">No</td>
395 </tr>
396<tr>
397 <td valign="top">failureproperty</td>
398 <td valign="top">The name of a property to set in the event of a failure
399 (errors are considered failures as well). Overrides value set in
400 <code>&lt;junit&gt;</code>.</td>
401 <td align="center" valign="top">No</td>
402</tr>
403 <tr>
404 <td valign="top">filtertrace</td>
405 <td valign="top">Filter out Junit and Ant stack frames from error and failure stack
406 traces. Overrides value set in <code>&lt;junit&gt;</code>.</td>
407 <td align="center" valign="top">No; default is <code>on</code>.</td>
408 </tr>
409 <tr>
410 <td valign="top">todir</td>
411 <td valign="top">Directory to write the reports to.</td>
412 <td align="center" valign="top">No; default is the current directory.</td>
413 </tr>
414 <tr>
415 <td valign="top">outfile</td>
416 <td valign="top">Base name of the test result. The full filename is
417 determined by this attribute and the extension of
418 <code>formatter</code>.</td>
419 <td align="center" valign="top">No; default is
420 <code>TEST-</code><em>name</em>, where <em>name</em> is the name of
421 the test specified in the <code>name</code> attribute.</td>
422 </tr>
423 <tr>
424 <td valign="top">if</td>
425 <td valign="top">Only run test if the named property is set.</td>
426 <td align="center" valign="top">No</td>
427 </tr>
428 <tr>
429 <td valign="top">unless</td>
430 <td valign="top">Only run test if the named property is <b>not</b> set.</td>
431 <td align="center" valign="top">No</td>
432 </tr>
433</table>
434
435<p>Tests can define their own formatters via nested
436<code>&lt;formatter&gt;</code> elements.</p>
437
438<h4>batchtest</h4>
439
440<p>Define a number of tests based on pattern matching.</p>
441
442<p><code>batchtest</code> collects the included files from any number
443of nested <a
444href="../CoreTypes/fileset.html"><code>&lt;fileset&gt;</code></a>s. It then
445generates a test class name for each file that ends in
446<code>.java</code> or <code>.class</code>.</p>
447
448<table border="1" cellpadding="2" cellspacing="0">
449<tr>
450 <td width="12%" valign="top"><b>Attribute</b></td>
451 <td width="78%" valign="top"><b>Description</b></td>
452 <td width="10%" valign="top"><b>Required</b></td>
453</tr>
454 <tr>
455 <td valign="top">fork</td>
456 <td valign="top">Run the tests in a separate VM.
457 Overrides value set in <code>&lt;junit&gt;</code>.</td>
458 <td align="center" valign="top">No</td>
459 </tr>
460 <tr>
461 <td valign="top">haltonerror</td>
462 <td valign="top">Stop the build process if an error occurs during the test
463 run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
464 <td align="center" valign="top">No</td>
465 </tr>
466<tr>
467 <td valign="top">errorproperty</td>
468 <td valign="top">The name of a property to set in the event of an error.
469 Overrides value set in <code>&lt;junit&gt;</code>.</td>
470 <td align="center" valign="top">No</td>
471</tr>
472 <tr>
473 <td valign="top">haltonfailure</td>
474 <td valign="top">Stop the build process if a test fails (errors are
475 considered failures as well). Overrides value set in
476 <code>&lt;junit&gt;</code>.</td>
477 <td align="center" valign="top">No</td>
478 </tr>
479<tr>
480 <td valign="top">failureproperty</td>
481 <td valign="top">The name of a property to set in the event of a failure
482 (errors are considered failures as well). Overrides value set in
483 <code>&lt;junit&gt;</code></td>
484 <td align="center" valign="top">No</td>
485</tr>
486 <tr>
487 <td valign="top">filtertrace</td>
488 <td valign="top">Filter out Junit and Ant stack frames from error and failure stack
489 traces. Overrides value set in <code>&lt;junit&gt;</code>.</td>
490 <td align="center" valign="top">No; default is <code>on</code>.</td>
491 </tr>
492 <tr>
493 <td valign="top">todir</td>
494 <td valign="top">Directory to write the reports to.</td>
495 <td align="center" valign="top">No; default is the current directory.</td>
496 </tr>
497 <tr>
498 <td valign="top">if</td>
499 <td valign="top">Only run tests if the named property is set.</td>
500 <td align="center" valign="top">No</td>
501 </tr>
502 <tr>
503 <td valign="top">unless</td>
504 <td valign="top">Only run tests if the named property is <strong>not</strong> set.</td>
505 <td align="center" valign="top">No</td>
506 </tr>
507</table>
508
509<p>Batchtests can define their own formatters via nested
510<code>&lt;formatter&gt;</code> elements.</p>
511
512<h3>Examples</h3>
513
514<pre>
515&lt;junit&gt;
516 &lt;test name="my.test.TestCase"/&gt;
517&lt;/junit&gt;
518</pre>
519
520<p>Runs the test defined in <code>my.test.TestCase</code> in the same
521VM. No output will be generated unless the test fails.</p>
522
523<pre>
524&lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
525 &lt;formatter type="plain"/&gt;
526 &lt;test name="my.test.TestCase"/&gt;
527&lt;/junit&gt;
528</pre>
529
530<p>Runs the test defined in <code>my.test.TestCase</code> in a
531separate VM. At the end of the test, a one-line summary will be
532printed. A detailed report of the test can be found in
533<code>TEST-my.test.TestCase.txt</code>. The build process will be
534stopped if the test fails.</p>
535
536<pre>
537&lt;junit printsummary="yes" haltonfailure="yes"&gt;
538 &lt;classpath&gt;
539 &lt;pathelement location="${build.tests}"/&gt;
540 &lt;pathelement path="${java.class.path}"/&gt;
541 &lt;/classpath&gt;
542
543 &lt;formatter type="plain"/&gt;
544
545 &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result"&gt;
546 &lt;formatter type="xml"/&gt;
547 &lt;/test&gt;
548
549 &lt;batchtest fork="yes" todir="${reports.tests}"&gt;
550 &lt;fileset dir="${src.tests}"&gt;
551 &lt;include name="**/*Test*.java"/&gt;
552 &lt;exclude name="**/AllTests.java"/&gt;
553 &lt;/fileset&gt;
554 &lt;/batchtest&gt;
555&lt;/junit&gt;
556</pre>
557
558<p>Runs <code>my.test.TestCase</code> in the same VM, ignoring the
559given CLASSPATH; only a warning is printed if this test fails. In
560addition to the plain text test results, for this test a XML result
561will be output to <code>result.xml</code>.
562Then, for each matching file in the directory defined for
563<code>${src.tests}</code> a
564test is run in a separate VM. If a test fails, the build process is
565aborted. Results are collected in files named
566<code>TEST-</code><em>name</em><code>.txt</code> and written to
567<code>${reports.tests}</code>.</p>
568
569<hr>
570<p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation. All rights
571Reserved.</p>
572</body>
573</html>
Note: See TracBrowser for help on using the repository browser.