source: release-kits/lirk3/bin/ant-installer/web/manual1.6.2/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 "org.apache.tools.ant."</pre></p>
202
203<a name="nested"></a><h3>Nested Elements</h3>
204
205<p>The <code>&lt;junit&gt;</code> task
206supports a nested <code>&lt;classpath&gt;</code>
207element that represents a <a href="../using.html#path">PATH like
208structure</a>.</p>
209
210<h4>jvmarg</h4>
211
212<p>If <code>fork</code> is enabled, additional parameters may be passed to
213the new VM via nested <code>&lt;jvmarg&gt;</code> elements. For example:</p>
214
215<pre>
216&lt;junit fork=&quot;yes&quot;&gt;
217 &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
218 ...
219&lt;/junit&gt;
220</pre>
221
222<p>would run the test in a VM without JIT.</p>
223
224<p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
225href="../using.html#arg">Command-line Arguments</a>.</p>
226
227<h4>sysproperty</h4>
228
229<p>Use nested <code>&lt;sysproperty&gt;</code> elements to specify system
230properties required by the class. These properties will be made available
231to the VM during the execution of the test (either ANT's VM or the forked VM,
232if <code>fork</code> is enabled).
233The attributes for this element are the same as for <a href="../CoreTasks/exec.html#env">environment variables</a>.</p>
234
235<pre>
236&lt;junit fork=&quot;no&quot;&gt;
237 &lt;sysproperty key=&quot;basedir&quot; value=&quot;${basedir}&quot;/&gt;
238 ...
239&lt;/junit&gt;
240</pre>
241
242<p>would run the test in ANT's VM and make the <code>basedir</code> property
243available to the test.</p>
244
245<h4>syspropertyset</h4>
246
247<p>You can specify a set of properties to be used as system properties
248with <a href="../CoreTypes/propertyset.html">syspropertyset</a>s.</p>
249
250<p><em>since Ant 1.6</em>.</p>
251
252<h4>env</h4>
253
254<p>It is possible to specify environment variables to pass to the
255forked VM via nested <code>&lt;env&gt;</code> elements. For a description
256of the <code>&lt;env&gt;</code> element's attributes, see the
257description in the <a href="../CoreTasks/exec.html#env">exec</a> task.</p>
258
259<p>Settings will be ignored if <code>fork</code> is disabled.</p>
260
261<h4>bootclasspath</h4>
262
263<p>The location of bootstrap class files can be specified using this
264<a href="../using.html#path">PATH like structure</a> - will be ignored
265if <i>fork</i> is not <code>true</code> or the target VM doesn't
266support it (i.e. Java 1.1).</p>
267
268<p><em>since Ant 1.6</em>.</p>
269
270<h4>permissions</h4>
271<p>Security permissions can be revoked and granted during the execution of the
272class via a nested <i>permissions</i> element. For more information please
273see <a href="../CoreTypes/permissions.html">permissions</a></p>
274
275<p>Settings will be ignored if fork is enabled.</p>
276
277<p><em>since Ant 1.6</em>.</p>
278
279<h4>assertions</h4>
280
281<p>You can control enablement of Java 1.4 assertions with an
282<a href="../CoreTypes/assertions.html"><tt>&lt;assertions&gt;</tt></a>
283subelement.</p>
284
285<p>Assertion statements are currently ignored in non-forked mode.</p>
286
287<p><em>since Ant 1.6.</em></p>
288
289<h4>formatter</h4>
290
291<p>The results of the tests can be printed in different
292formats. Output will always be sent to a file, unless you set the
293<code>usefile</code> attribute to <code>false</code>.
294The name of the file is determined by the
295name of the test and can be set by the <code>outfile</code> attribute
296of <code>&lt;test&gt;</code>.</p>
297
298<p>There are three predefined formatters - one prints the test results
299in XML format, the other emits plain text. The formatter named
300<code>brief</code> will only print detailed information for testcases
301that failed, while <code>plain</code> gives a little statistics line
302for all test cases. Custom formatters that need to implement
303<code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
304can be specified.</p>
305
306<p>If you use the XML formatter, it may not include the same output
307that your tests have written as some characters are illegal in XML
308documents and will be dropped.</p>
309
310<p><b>Note:</b> Please read the <a
311href="../../faq.html#junit-no-runtime-xml">Ant FAQ</a> if you want to
312set the fork attribute to <code>true</code>, the includeAntRuntime
313attribute to <code>false</code> and use the XML formatter at the same
314time.</p>
315
316<table border="1" cellpadding="2" cellspacing="0">
317<tr>
318 <td width="12%" valign="top"><b>Attribute</b></td>
319 <td width="78%" valign="top"><b>Description</b></td>
320 <td width="10%" valign="top"><b>Required</b></td>
321</tr>
322 <tr>
323 <td valign="top">type</td>
324 <td valign="top">Use a predefined formatter (either
325 <code>xml</code>, <code>plain</code>, or <code>brief</code>).</td>
326 <td align="center" rowspan="2">Exactly one of these.</td>
327 </tr>
328 <tr>
329 <td valign="top">classname</td>
330 <td valign="top">Name of a custom formatter class.</td>
331 </tr>
332 <tr>
333 <td valign="top">extension</td>
334 <td valign="top">Extension to append to the output filename.</td>
335 <td align="center">Yes, if <code>classname</code> has been used.</td>
336 </tr>
337 <tr>
338 <td valign="top">usefile</td>
339 <td valign="top">Boolean that determines whether output should be
340 sent to a file.</td>
341 <td align="center">No; default is <code>true</code>.</td>
342 </tr>
343 <tr>
344 <td valign="top">if</td>
345 <td valign="top">Only use formatter if the named property is set.</td>
346 <td align="center">No; default is <code>true</code>.</td>
347 </tr>
348 <tr>
349 <td valign="top">unless</td>
350 <td valign="top">Only use formatter if the named property is <b>not</b> set.</td>
351 <td align="center">No; default is <code>true</code>.</td>
352 </tr>
353</table>
354
355<h4>test</h4>
356
357<p>Defines a single test class.</p>
358
359<table border="1" cellpadding="2" cellspacing="0">
360<tr>
361 <td width="12%" valign="top"><b>Attribute</b></td>
362 <td width="78%" valign="top"><b>Description</b></td>
363 <td width="10%" valign="top"><b>Required</b></td>
364</tr>
365 <tr>
366 <td valign="top">name</td>
367 <td valign="top">Name of the test class.</td>
368 <td align="center">Yes</td>
369 </tr>
370 <tr>
371 <td valign="top">fork</td>
372 <td valign="top">Run the tests in a separate VM.
373 Overrides value set in <code>&lt;junit&gt;</code>.</td>
374 <td align="center" valign="top">No</td>
375 </tr>
376 <tr>
377 <td valign="top">haltonerror</td>
378 <td valign="top">Stop the build process if an error occurs during the test
379 run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
380 <td align="center" valign="top">No</td>
381 </tr>
382<tr>
383 <td valign="top">errorproperty</td>
384 <td valign="top">The name of a property to set in the event of an error.
385 Overrides value set in <code>&lt;junit&gt;</code>.</td>
386 <td align="center" valign="top">No</td>
387</tr>
388 <tr>
389 <td valign="top">haltonfailure</td>
390 <td valign="top">Stop the build process if a test fails (errors are
391 considered failures as well). Overrides value set in
392 <code>&lt;junit&gt;</code>.</td>
393 <td align="center" valign="top">No</td>
394 </tr>
395<tr>
396 <td valign="top">failureproperty</td>
397 <td valign="top">The name of a property to set in the event of a failure
398 (errors are considered failures as well). Overrides value set in
399 <code>&lt;junit&gt;</code>.</td>
400 <td align="center" valign="top">No</td>
401</tr>
402 <tr>
403 <td valign="top">filtertrace</td>
404 <td valign="top">Filter out Junit and Ant stack frames from error and failure stack
405 traces. Overrides value set in <code>&lt;junit&gt;</code>.</td>
406 <td align="center" valign="top">No; default is <code>on</code>.</td>
407 </tr>
408 <tr>
409 <td valign="top">todir</td>
410 <td valign="top">Directory to write the reports to.</td>
411 <td align="center" valign="top">No; default is the current directory.</td>
412 </tr>
413 <tr>
414 <td valign="top">outfile</td>
415 <td valign="top">Base name of the test result. The full filename is
416 determined by this attribute and the extension of
417 <code>formatter</code>.</td>
418 <td align="center" valign="top">No; default is
419 <code>TEST-</code><em>name</em>, where <em>name</em> is the name of
420 the test specified in the <code>name</code> attribute.</td>
421 </tr>
422 <tr>
423 <td valign="top">if</td>
424 <td valign="top">Only run test if the named property is set.</td>
425 <td align="center" valign="top">No</td>
426 </tr>
427 <tr>
428 <td valign="top">unless</td>
429 <td valign="top">Only run test if the named property is <b>not</b> set.</td>
430 <td align="center" valign="top">No</td>
431 </tr>
432</table>
433
434<p>Tests can define their own formatters via nested
435<code>&lt;formatter&gt;</code> elements.</p>
436
437<h4>batchtest</h4>
438
439<p>Define a number of tests based on pattern matching.</p>
440
441<p><code>batchtest</code> collects the included files from any number
442of nested <a
443href="../CoreTypes/fileset.html"><code>&lt;fileset&gt;</code></a>s. It then
444generates a test class name for each file that ends in
445<code>.java</code> or <code>.class</code>.</p>
446
447<table border="1" cellpadding="2" cellspacing="0">
448<tr>
449 <td width="12%" valign="top"><b>Attribute</b></td>
450 <td width="78%" valign="top"><b>Description</b></td>
451 <td width="10%" valign="top"><b>Required</b></td>
452</tr>
453 <tr>
454 <td valign="top">fork</td>
455 <td valign="top">Run the tests in a separate VM.
456 Overrides value set in <code>&lt;junit&gt;</code>.</td>
457 <td align="center" valign="top">No</td>
458 </tr>
459 <tr>
460 <td valign="top">haltonerror</td>
461 <td valign="top">Stop the build process if an error occurs during the test
462 run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
463 <td align="center" valign="top">No</td>
464 </tr>
465<tr>
466 <td valign="top">errorproperty</td>
467 <td valign="top">The name of a property to set in the event of an error.
468 Overrides value set in <code>&lt;junit&gt;</code>.</td>
469 <td align="center" valign="top">No</td>
470</tr>
471 <tr>
472 <td valign="top">haltonfailure</td>
473 <td valign="top">Stop the build process if a test fails (errors are
474 considered failures as well). Overrides value set in
475 <code>&lt;junit&gt;</code>.</td>
476 <td align="center" valign="top">No</td>
477 </tr>
478<tr>
479 <td valign="top">failureproperty</td>
480 <td valign="top">The name of a property to set in the event of a failure
481 (errors are considered failures as well). Overrides value set in
482 <code>&lt;junit&gt;</code></td>
483 <td align="center" valign="top">No</td>
484</tr>
485 <tr>
486 <td valign="top">filtertrace</td>
487 <td valign="top">Filter out Junit and Ant stack frames from error and failure stack
488 traces. Overrides value set in <code>&lt;junit&gt;</code>.</td>
489 <td align="center" valign="top">No; default is <code>on</code>.</td>
490 </tr>
491 <tr>
492 <td valign="top">todir</td>
493 <td valign="top">Directory to write the reports to.</td>
494 <td align="center" valign="top">No; default is the current directory.</td>
495 </tr>
496 <tr>
497 <td valign="top">if</td>
498 <td valign="top">Only run tests if the named property is set.</td>
499 <td align="center" valign="top">No</td>
500 </tr>
501 <tr>
502 <td valign="top">unless</td>
503 <td valign="top">Only run tests if the named property is <strong>not</strong> set.</td>
504 <td align="center" valign="top">No</td>
505 </tr>
506</table>
507
508<p>Batchtests can define their own formatters via nested
509<code>&lt;formatter&gt;</code> elements.</p>
510
511<h3>Examples</h3>
512
513<pre>
514&lt;junit&gt;
515 &lt;test name="my.test.TestCase"/&gt;
516&lt;/junit&gt;
517</pre>
518
519<p>Runs the test defined in <code>my.test.TestCase</code> in the same
520VM. No output will be generated unless the test fails.</p>
521
522<pre>
523&lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
524 &lt;formatter type="plain"/&gt;
525 &lt;test name="my.test.TestCase"/&gt;
526&lt;/junit&gt;
527</pre>
528
529<p>Runs the test defined in <code>my.test.TestCase</code> in a
530separate VM. At the end of the test, a one-line summary will be
531printed. A detailed report of the test can be found in
532<code>TEST-my.test.TestCase.txt</code>. The build process will be
533stopped if the test fails.</p>
534
535<pre>
536&lt;junit printsummary="yes" haltonfailure="yes"&gt;
537 &lt;classpath&gt;
538 &lt;pathelement location="${build.tests}"/&gt;
539 &lt;pathelement path="${java.class.path}"/&gt;
540 &lt;/classpath&gt;
541
542 &lt;formatter type="plain"/&gt;
543
544 &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result"&gt;
545 &lt;formatter type="xml"/&gt;
546 &lt;/test&gt;
547
548 &lt;batchtest fork="yes" todir="${reports.tests}"&gt;
549 &lt;fileset dir="${src.tests}"&gt;
550 &lt;include name="**/*Test*.java"/&gt;
551 &lt;exclude name="**/AllTests.java"/&gt;
552 &lt;/fileset&gt;
553 &lt;/batchtest&gt;
554&lt;/junit&gt;
555</pre>
556
557<p>Runs <code>my.test.TestCase</code> in the same VM, ignoring the
558given CLASSPATH; only a warning is printed if this test fails. In
559addition to the plain text test results, for this test a XML result
560will be output to <code>result.xml</code>.
561Then, for each matching file in the directory defined for
562<code>${src.tests}</code> a
563test is run in a separate VM. If a test fails, the build process is
564aborted. Results are collected in files named
565<code>TEST-</code><em>name</em><code>.txt</code> and written to
566<code>${reports.tests}</code>.</p>
567
568<hr>
569<p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights
570Reserved.</p>
571</body>
572</html>
Note: See TracBrowser for help on using the repository browser.