You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

junit.html 26 KiB

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