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 33 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 id="junit">JUnit</h2>
  22. <h3>Description</h3>
  23. <p>This task runs tests from the JUnit testing framework. The latest version of the framework can be
  24. found at <a href="https://junit.org">https://junit.org</a>. This task has been tested with JUnit
  25. 3.0 up to JUnit 3.8.2; it won't work with versions prior to JUnit 3.0. It also works with JUnit 4.x,
  26. including "pure" JUnit 4 tests using only annotations and no <code>JUnit4TestAdapter</code>.</p>
  27. <p><strong>Note</strong>: This task depends on external libraries not included
  28. in the Apache Ant distribution. See <a href="../install.html#librarydependencies">
  29. Library Dependencies</a> for more information.</p>
  30. <p><strong>Note</strong>: You must have <samp>junit.jar</samp> available. You can do one of:</p>
  31. <ol>
  32. <li>Put both <samp>junit.jar</samp> and <samp>ant-junit.jar</samp>
  33. in <code>ANT_HOME/lib</code>.</li>
  34. <li>Do not put either in <code>ANT_HOME/lib</code>, and instead include their locations in
  35. your <code>CLASSPATH</code> environment variable.</li>
  36. <li>Add both JARs to your classpath using <code>-lib</code>.</li>
  37. <li>Specify the locations of both JARs using a <code>&lt;classpath&gt;</code> element in
  38. a <code>&lt;taskdef&gt;</code> in the build file.</li>
  39. <li>Leave <samp>ant-junit.jar</samp> in its default location in <code>ANT_HOME/lib</code> but
  40. include <samp>junit.jar</samp> in the <code>&lt;classpath&gt;</code> passed
  41. to <code>&lt;junit&gt;</code>. <em>Since Ant 1.7</em></li>
  42. </ol>
  43. <p>See <a href="https://ant.apache.org/faq.html#delegating-classloader" target="_top">the FAQ</a>
  44. for details.</p>
  45. <p>Tests are defined by nested <code>test</code> or <code>batchtest</code> tags
  46. (see <a href="#nested">nested elements</a>).</p>
  47. <h3>Parameters</h3>
  48. <table class="attr">
  49. <tr>
  50. <th>Attribute</th>
  51. <th>Description</th>
  52. <th>Required</th>
  53. </tr>
  54. <tr>
  55. <td>printsummary</td>
  56. <td>Print one-line statistics for each testcase. Can take the values <q>on</q>, <q>off</q>,
  57. and <q>withOutAndErr</q>. <q>withOutAndErr</q> is the same as <q>on</q> but also includes the
  58. output of the test as written to <code>System.out</code> and <code>System.err</code>.</td>
  59. <td>No; default is <q>off</q></td>
  60. </tr>
  61. <tr>
  62. <td>fork</td>
  63. <td>Run the tests in a separate JVM.</td>
  64. <td>No; default is <q>off</q></td>
  65. </tr>
  66. <tr>
  67. <td>forkmode</td>
  68. <td>Controls how many JVMs get created if you want to fork some tests. Possible values
  69. are <q>perTest</q> (the default), <q>perBatch</q> and <q>once</q>. <q>once</q> creates only a
  70. single JVM for all tests while <q>perTest</q> creates a new JVM for each TestCase
  71. class. <q>perBatch</q> creates a JVM for each nested <code>&lt;batchtest&gt;</code> and one
  72. collecting all nested <code>&lt;test&gt;</code>s. Note that only tests with the same settings
  73. of <var>filtertrace</var>, <var>haltonerror</var>, <var>haltonfailure</var>, <var>errorproperty</var>
  74. and <var>failureproperty</var> can share a JVM, so even if you set <var>forkmode</var>
  75. to <q>once</q>, Ant may have to create more than a single JVM. This attribute is ignored for
  76. tests that don't get forked into a new JVM. <em>Since Ant 1.6.2</em></td>
  77. <td>No; default is <q>perTest</q></td>
  78. </tr>
  79. <tr>
  80. <td>haltonerror</td>
  81. <td>Stop the build process if an error occurs during the test run.</td>
  82. <td>No; default is <q>off</q></td>
  83. </tr>
  84. <tr>
  85. <td>errorproperty</td>
  86. <td>The name of a property to set in the event of an error.</td>
  87. <td>No</td>
  88. </tr>
  89. <tr>
  90. <td>haltonfailure</td>
  91. <td>Stop the build process if a test fails (errors are considered failures as well).</td>
  92. <td>No; default is <q>off</q></td>
  93. </tr>
  94. <tr>
  95. <td>failureproperty</td>
  96. <td>The name of a property to set in the event of a failure (errors are considered failures as
  97. well).</td>
  98. <td>No</td>
  99. </tr>
  100. <tr>
  101. <td>filtertrace</td>
  102. <td>Filter out JUnit and Ant stack frames from error and failure stack traces.</td>
  103. <td>No; default is <q>on</q></td>
  104. </tr>
  105. <tr>
  106. <td>timeout</td>
  107. <td>Cancel the individual tests if they don't finish in the given time (measured in
  108. milliseconds). Ignored if <var>fork</var> is <q>off</q>. When running multiple tests inside
  109. the same JVM (see <var>forkMode</var>), <var>timeout</var> applies to the time that all tests
  110. use together, not to an individual test.</td>
  111. <td>No</td>
  112. </tr>
  113. <tr>
  114. <td>maxmemory</td>
  115. <td>Maximum amount of memory to allocate to the forked JVM. Ignored if <var>fork</var>
  116. is <q>off</q>. <strong>Note</strong>: If you get <code>java.lang.OutOfMemoryError: Java heap
  117. space</code> in some of your tests then you need to raise the size
  118. like <var>maxmemory</var>=<q>128m</q></td>
  119. <td>No</td>
  120. </tr>
  121. <tr>
  122. <td>jvm</td>
  123. <td>The command used to invoke JVM. The command is resolved
  124. by <code>java.lang.Runtime.exec()</code>.</td>
  125. <td>No; default is <q>java</q>, ignored if <var>fork</var> is <q>false</q></td>
  126. </tr>
  127. <tr>
  128. <td>dir</td>
  129. <td>The directory in which to invoke JVM.</td>
  130. <td>No, ignored if <var>fork</var> is <q>false</q></td>
  131. </tr>
  132. <tr>
  133. <td>newenvironment</td>
  134. <td>Do not propagate the old environment when new environment variables are specified.</td>
  135. <td>No; default is <q>false</q>, ignored if <var>fork</var> is <q>false</q></td>
  136. </tr>
  137. <tr>
  138. <td>includeantruntime</td>
  139. <td>Implicitly add the Ant classes required to run the tests and JUnit to the classpath in
  140. forked mode.</td>
  141. <td>No; default is <q>true</q></td>
  142. </tr>
  143. <tr>
  144. <td>showoutput</td>
  145. <td>Send any output generated by tests to Ant's logging system as well as to the
  146. formatters.</td>
  147. <td>No; by default only the formatters receive the output</td>
  148. </tr>
  149. <tr>
  150. <td>outputtoformatters</td>
  151. <td><em>Since Ant 1.7.0</em>.<br/>Send any output generated by tests to the test
  152. formatters.</td>
  153. <td>No; default is <q>true</q></td>
  154. </tr>
  155. <tr>
  156. <td>tempdir</td>
  157. <td>Where Ant should place temporary files. <em>Since Ant 1.6</em>.</td>
  158. <td>No; default is the project's <var>basedir</var></td>
  159. </tr>
  160. <tr>
  161. <td>reloading</td>
  162. <td>Whether or not a new classloader should be instantiated for each test case.<br/>Ignore
  163. if <var>fork</var> is set to <q>true</q>. <em>Since Ant 1.6</em>.</td>
  164. <td>No; default is <code>true</code></td>
  165. </tr>
  166. <tr>
  167. <td>clonevm</td>
  168. <td>If set to <q>true</q>, then all system properties and the <var>bootclasspath</var> of the
  169. forked JVM will be the same as those of the JVM running Ant. <em>since Ant 1.7</em></td>
  170. <td>No; default is <q>false</q>, ignored if <var>fork</var> is <q>false</q></td>
  171. </tr>
  172. <tr>
  173. <td>logfailedtests</td>
  174. <td>When Ant executes multiple tests and doesn't stop on errors or failures it will log a
  175. "FAILED" message for each failing test to its logging system. If you set this option
  176. to <q>false</q>, the message will not be logged and you have to rely on the formatter output
  177. to find the failing tests. <em>since Ant 1.8.0</em></td>
  178. <td>No</td>
  179. </tr>
  180. <tr>
  181. <td>enableTestListenerEvents</td>
  182. <td>Whether Ant should send fine grained information about the running tests to Ant's logging
  183. system at the verbose level. Such events may be used by custom test listeners to show the
  184. progress of tests.<br/><em>since Ant 1.8.2</em>&mdash;<strong>Ant 1.7.0 to 1.8.1 behave as if
  185. this attribute was <q>true</q> by default.</strong></td>
  186. <td>No; defaults to <q>false</q>, can be overridden by
  187. a <a href="#enabletestlistenerevents">magic property</a></td>
  188. </tr>
  189. <tr>
  190. <td>threads</td>
  191. <td>a number of threads to run the tests in.<br/>When this attribute is specified the tests will
  192. be split arbitrarily among the threads.<br/>Requires that the tests be forked with
  193. the <q>perTest</q> option to be operative.<br/><em>since Ant 1.9.4</em></td>
  194. <td>No</td>
  195. </tr>
  196. </table>
  197. <p>By using the <var>errorproperty</var> and <var>failureproperty</var> attributes, it is possible
  198. to perform setup work (such as starting an external server), execute the test, clean up, and still
  199. fail the build in the event of a failure.</p>
  200. <p>The <var>filtertrace</var> attribute condenses error and failure stack traces before reporting
  201. them. It works with both the plain and XML formatters. It filters out any lines that begin with
  202. the following string patterns:</p>
  203. <pre>
  204. &quot;junit.framework.TestCase&quot;
  205. &quot;junit.framework.TestResult&quot;
  206. &quot;junit.framework.TestSuite&quot;
  207. &quot;junit.framework.Assert.&quot;
  208. &quot;junit.swingui.TestRunner&quot;
  209. &quot;junit.awtui.TestRunner&quot;
  210. &quot;junit.textui.TestRunner&quot;
  211. &quot;java.lang.reflect.Method.invoke(&quot;
  212. &quot;sun.reflect.&quot;
  213. &quot;org.apache.tools.ant.&quot;
  214. &quot;org.junit.&quot;
  215. &quot;junit.framework.JUnit4TestAdapter&quot;
  216. &quot; more&quot;</pre>
  217. <h3 id="nested">Nested Elements</h3>
  218. <p>The <code>&lt;junit&gt;</code> task supports a nested <code>&lt;classpath&gt;</code> element that
  219. represents a <a href="../using.html#path">PATH like structure</a>.</p>
  220. <p><em>Since Ant 1.7</em>, this classpath may be used to refer to <samp>junit.jar</samp> as well as
  221. your tests and the tested code.</p>
  222. <h4>jvmarg</h4>
  223. <p>If <var>fork</var> is <q>true</q>, additional parameters may be passed to the new JVM via
  224. nested <code>&lt;jvmarg&gt;</code> elements. For example:</p>
  225. <pre>
  226. &lt;junit fork=&quot;yes&quot;&gt;
  227. &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  228. ...
  229. &lt;/junit&gt;
  230. </pre>
  231. <p>would run the test in a JVM without JIT.</p>
  232. <p><code>&lt;jvmarg&gt;</code> allows all attributes described
  233. in <a href="../using.html#arg">Command-line Arguments</a>.</p>
  234. <h4>sysproperty</h4>
  235. <p>Use nested <code>&lt;sysproperty&gt;</code> elements to specify system properties required by the
  236. class. These properties will be made available to JVM during the execution of the test (either Ant's
  237. JVM or the forked JVM, if <var>fork</var>=<q>true</q>). The attributes for this element are the
  238. same as for <a href="../Tasks/exec.html#env">environment variables</a>.</p>
  239. <pre>
  240. &lt;junit fork=&quot;no&quot;&gt;
  241. &lt;sysproperty key=&quot;basedir&quot; value=&quot;${basedir}&quot;/&gt;
  242. ...
  243. &lt;/junit&gt;
  244. </pre>
  245. <p>would run the test in Ant's JVM and make the <code>basedir</code> property available to the
  246. test.</p>
  247. <h4>syspropertyset</h4>
  248. <p>You can specify a set of properties to be used as system properties
  249. with <a href="../Types/propertyset.html">syspropertyset</a>s.</p>
  250. <p><em>since Ant 1.6</em>.</p>
  251. <h4>env</h4>
  252. <p>It is possible to specify environment variables to pass to the forked JVM via
  253. nested <code>&lt;env&gt;</code> elements. For a description of the <code>&lt;env&gt;</code>
  254. element's attributes, see the description in the <a href="../Tasks/exec.html#env">exec</a> task.</p>
  255. <p>Settings will be ignored if <var>fork</var>=<q>false</q>.</p>
  256. <h4>bootclasspath</h4>
  257. <p>The location of bootstrap class files can be specified using
  258. this <a href="../using.html#path">PATH like structure</a>&mdash;will be ignored if <var>fork</var>
  259. is <q>false</q> or the target JVM doesn't support it (i.e. Java 1.1).</p>
  260. <p><em>since Ant 1.6</em>.</p>
  261. <h4>permissions</h4>
  262. <p>Security permissions can be revoked and granted during the execution of the class via a
  263. nested <code>permissions</code> element. For more information please
  264. see <a href="../Types/permissions.html">permissions</a></p>
  265. <p>Settings will be ignored if <var>fork</var>=<q>true</q>.</p>
  266. <p><em>since Ant 1.6</em>.</p>
  267. <h4>assertions</h4>
  268. <p>You can control enablement of Java 1.4 assertions with
  269. an <a href="../Types/assertions.html"><code>&lt;assertions&gt;</code></a> subelement.</p>
  270. <p>Assertion statements are currently ignored in non-forked mode.</p>
  271. <p><em>Since Ant 1.6</em>.</p>
  272. <h4>modulepath</h4>
  273. <p>The location of modules can be specified using this <a href="../using.html#path">PATH like
  274. structure</a>.<br/>The <code>modulepath</code> requires <var>fork</var> to be set to <q>true</q>.
  275. <p><em>since Ant 1.9.8</em></p>
  276. <h4>upgrademodulepath</h4>
  277. <p>The location of modules that replace upgradeable modules in the runtime image can be specified
  278. using this <a href="../using.html#path">PATH like
  279. structure</a>.<br/>The <code>upgrademodulepath</code> requires <var>fork</var> to be set
  280. to <q>true</q>.</p>
  281. <p><em>since Ant 1.9.8</em></p>
  282. <h4>formatter</h4>
  283. <p>The results of the tests can be printed in different formats. Output will always be sent to a
  284. file, unless you set the <var>usefile</var> attribute to <q>false</q>. The name of the file is
  285. determined by the name of the test and can be set by the <var>outfile</var> attribute
  286. of <code>&lt;test&gt;</code>.</p>
  287. <p>There are four predefined formatters&mdash;one prints the test results in XML format, the other
  288. emits plain text. The formatter named <q>brief</q> will only print detailed information for test
  289. cases that failed, while <q>plain</q> gives a little statistics line for all test cases. Custom
  290. formatters that need to
  291. implement <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code> can be
  292. specified.</p>
  293. <p>If you use the XML formatter, it may not include the same output that your tests have written as
  294. some characters are illegal in XML documents and will be dropped.</p>
  295. <p>The fourth formatter named <q>failure</q> (<em>since Ant 1.8.0</em>) collects all
  296. failing <code>testXXX()</code> methods and creates a new <code>TestCase</code> which delegates only
  297. these failing methods. The name and the location can be specified via Java System property or Ant
  298. property <code>ant.junit.failureCollector</code>. The value has to point to the directory and the
  299. name of the resulting class (without suffix). It defaults
  300. to <samp><i>java-tmp-dir</i>/FailedTests</samp>.</p>
  301. <table class="attr">
  302. <tr>
  303. <th>Attribute</th>
  304. <th>Description</th>
  305. <th>Required</th>
  306. </tr>
  307. <tr>
  308. <td>type</td>
  309. <td>Use a predefined formatter (either <q>xml</q>, <q>plain</q>, <q>brief</q>
  310. or <q>failure</q>).</td>
  311. <td rowspan="2">Exactly one of these</td>
  312. </tr>
  313. <tr>
  314. <td>classname</td>
  315. <td>Name of a custom formatter class.</td>
  316. </tr>
  317. <tr>
  318. <td>extension</td>
  319. <td>Extension to append to the output filename.</td>
  320. <td>Yes, if <var>classname</var> has been used</td>
  321. </tr>
  322. <tr>
  323. <td>usefile</td>
  324. <td>Boolean that determines whether output should be sent to a file.</td>
  325. <td>No; default is <q>true</q></td>
  326. </tr>
  327. <tr>
  328. <td>if</td>
  329. <td>Only use formatter <a href="../properties.html#if+unless">if the named property is
  330. set</a>.</td>
  331. <td>No; default is <q>true</q></td>
  332. </tr>
  333. <tr>
  334. <td>unless</td>
  335. <td>Only use formatter <a href="../properties.html#if+unless">if the named property
  336. is <strong>not</strong> set</a>.</td>
  337. <td>No; default is <q>true</q></td>
  338. </tr>
  339. </table>
  340. <h4>test</h4>
  341. <p>Defines a single test class.</p>
  342. <table class="attr">
  343. <tr>
  344. <th>Attribute</th>
  345. <th>Description</th>
  346. <th>Required</th>
  347. </tr>
  348. <tr>
  349. <td>name</td>
  350. <td>Name of the test class.</td>
  351. <td>Yes</td>
  352. </tr>
  353. <tr>
  354. <td>methods</td>
  355. <td>Comma-separated list of names of test case methods to execute. <em>Since 1.8.2</em>
  356. <p>The <var>methods</var> attribute can be useful in the followiang scenarios:</p>
  357. <ul>
  358. <li>A test method has failed and you want to re-run the test method to test a fix or re-run
  359. the test under the Java debugger without having to wait for the other (possibly long
  360. running) test methods to complete.</li>
  361. <li>One or more test methods are running slower than expected and you want to re-run them
  362. under a Java profiler (without the overhead of running the profiler whilst other test
  363. methods are being executed).</li>
  364. </ul>
  365. <p>If the <var>methods</var> attribute is used but no test method is specified, then no test
  366. method from the suite will be executed.</p>
  367. </td>
  368. <td>No; default is to run all test methods in the suite</td>
  369. </tr>
  370. <tr>
  371. <td>fork</td>
  372. <td>Run the tests in a separate JVM. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  373. <td>No</td>
  374. </tr>
  375. <tr>
  376. <td>haltonerror</td>
  377. <td>Stop the build process if an error occurs during the test run. Overrides value set
  378. in <code>&lt;junit&gt;</code>.</td>
  379. <td>No</td>
  380. </tr>
  381. <tr>
  382. <td>errorproperty</td>
  383. <td>The name of a property to set in the event of an error. Overrides value set
  384. in <code>&lt;junit&gt;</code>.</td>
  385. <td>No</td>
  386. </tr>
  387. <tr>
  388. <td>haltonfailure</td>
  389. <td>Stop the build process if a test fails (errors are considered failures as well). Overrides
  390. value set in <code>&lt;junit&gt;</code>.</td>
  391. <td>No</td>
  392. </tr>
  393. <tr>
  394. <td>failureproperty</td>
  395. <td>The name of a property to set in the event of a failure (errors are considered failures as
  396. well). Overrides value set in <code>&lt;junit&gt;</code>.</td>
  397. <td>No</td>
  398. </tr>
  399. <tr>
  400. <td>filtertrace</td>
  401. <td>Filter out JUnit and Ant stack frames from error and failure stack traces. Overrides value
  402. set in <code>&lt;junit&gt;</code>.</td>
  403. <td>No; default is <q>on</q></td>
  404. </tr>
  405. <tr>
  406. <td>todir</td>
  407. <td>Directory to write the reports to.</td>
  408. <td>No; default is the current directory</td>
  409. </tr>
  410. <tr>
  411. <td>outfile</td>
  412. <td>Base name of the test result. The full filename
  413. is <var>outfile</var>.<var>formatter</var>.</td>
  414. <td>No; default is <samp>TEST-</samp><var>name</var></td>
  415. </tr>
  416. <tr>
  417. <td>if</td>
  418. <td>Only run test <a href="../properties.html#if+unless">if the named property is set</a>.</td>
  419. <td>No</td>
  420. </tr>
  421. <tr>
  422. <td>unless</td>
  423. <td>Only run test <a href="../properties.html#if+unless">if the named property
  424. is <strong>not</strong> set</a>.</td>
  425. <td>No</td>
  426. </tr>
  427. <tr>
  428. <td>skipNonTests</td>
  429. <td>Do not pass any classes that do not contain JUnit tests to the test runner. This prevents
  430. non tests from appearing as test errors in test results.<br/> Tests are identified by looking
  431. for the <code>@Test</code> annotation on any methods in concrete classes that don't
  432. extend <code>junit.framework.TestCase</code>, or for public/protected methods with names
  433. starting with <q>test</q> in concrete classes that
  434. extend <code>junit.framework.TestCase</code>. Classes marked with the JUnit
  435. 4 <code>org.junit.runner.RunWith</code> or <code>org.junit.runner.Suite.SuiteClasses</code>
  436. annotations are also passed to JUnit for execution, as is any class with a public/protected
  437. no-argument <code>suite</code> method.</td>
  438. <td>No; default is <q>false</q></td>
  439. </tr>
  440. </table>
  441. <p>Tests can define their own formatters via nested <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="../Types/resources.html">resources</a> from
  445. any number of nested <a href="../Types/resources.html#collection">Resource Collection</a>s. It then
  446. generates a test class name for each resource that ends in <samp>.java</samp>
  447. or <samp>.class</samp>.</p>
  448. <p>Any type of Resource Collection is supported as a nested element, prior to Ant 1.7
  449. only <code>&lt;fileset&gt;</code> has been supported.</p>
  450. <table class="attr">
  451. <tr>
  452. <th>Attribute</th>
  453. <th>Description</th>
  454. <th>Required</th>
  455. </tr>
  456. <tr>
  457. <td>fork</td>
  458. <td>Run the tests in a separate JVM. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  459. <td>No</td>
  460. </tr>
  461. <tr>
  462. <td>haltonerror</td>
  463. <td>Stop the build process if an error occurs during the test run. Overrides value set
  464. in <code>&lt;junit&gt;</code>.</td>
  465. <td>No</td>
  466. </tr>
  467. <tr>
  468. <td>errorproperty</td>
  469. <td>The name of a property to set in the event of an error. Overrides value set
  470. in <code>&lt;junit&gt;</code>.</td>
  471. <td>No</td>
  472. </tr>
  473. <tr>
  474. <td>haltonfailure</td>
  475. <td>Stop the build process if a test fails (errors are considered failures as well). Overrides
  476. value set in <code>&lt;junit&gt;</code>.</td>
  477. <td>No</td>
  478. </tr>
  479. <tr>
  480. <td>failureproperty</td>
  481. <td>The name of a property to set in the event of a failure (errors are considered failures as
  482. well). Overrides value set in <code>&lt;junit&gt;</code></td>
  483. <td>No</td>
  484. </tr>
  485. <tr>
  486. <td>filtertrace</td>
  487. <td>Filter out JUnit and Ant stack frames from error and failure stack traces. Overrides value
  488. set in <code>&lt;junit&gt;</code>.</td>
  489. <td>No; default is <q>on</q></td>
  490. </tr>
  491. <tr>
  492. <td>todir</td>
  493. <td>Directory to write the reports to.</td>
  494. <td>No; default is the current directory</td>
  495. </tr>
  496. <tr>
  497. <td>if</td>
  498. <td>Only run tests <a href="../properties.html#if+unless">if the named property is set</a>.</td>
  499. <td>No</td>
  500. </tr>
  501. <tr>
  502. <td>unless</td>
  503. <td>Only run tests <a href="../properties.html#if+unless">if the named property
  504. is <strong>not</strong> set</a>.</td>
  505. <td>No</td>
  506. </tr>
  507. <tr>
  508. <td>skipNonTests</td>
  509. <td>Do not pass any classes that do not contain JUnit tests to the test runner. This prevents
  510. non tests from appearing as test errors in test results.<br/>Tests are identified by looking
  511. for the <code>@Test</code> annotation on any methods in concrete classes that don't
  512. extend <code>junit.framework.TestCase</code>, or for public/protected methods with names
  513. starting with 'test' in concrete classes that extend <code>junit.framework.TestCase</code>.
  514. Classes marked with the JUnit4 <code>org.junit.runner.RunWith</code>
  515. or <code>org.junit.runner.Suite.SuiteClasses</code> annotations are also passed to JUnit for
  516. execution, as is any class with a public/protected no-argument <code>suite</code> method.</td>
  517. <td>No; default is <q>false</q></td>
  518. </tr>
  519. </table>
  520. <p>Batch tests can define their own formatters via nested <code>&lt;formatter&gt;</code>
  521. elements.</p>
  522. <h3>Forked tests and <code>tearDown()</code></h3>
  523. <p>If a forked test runs into a timeout, Ant will terminate the JVM process it has created, which
  524. probably means the test's <code>tearDown()</code> method will never be called. The same is true if
  525. the forked JVM crashes for some other reason.</p>
  526. <p><em>Since Ant 1.8.0</em>, a special formatter is distributed with Ant that tries to load the
  527. testcase that was in the forked JVM and invoke that class' <code>tearDown()</code> method. This
  528. formatter has the following limitations:</p>
  529. <ul>
  530. <li>It runs in the same JVM as Ant itself, this is a different JVM than the one that was executing
  531. the test and it may see a different classloader (and thus may be unable to load the test
  532. class).</li>
  533. <li>It cannot determine which test was run when the timeout/crash occurred if the forked JVM was
  534. running multiple test. I.e. the formatter cannot work with any <var>forkMode</var> other
  535. than <q>perTest</q> and it won't do anything if the test class contains a <code>suite()</code>
  536. method.</li>
  537. </ul>
  538. <p>If the formatter recognizes an incompatible <var>forkMode</var> or a <code>suite</code> method or
  539. fails to load the test class it will silently do nothing.</p>
  540. <p>The formatter doesn't have any effect on tests that were not forked or didn't cause timeouts or
  541. JVM crashes.</p>
  542. <p>To enable the formatter, add a <code>formatter</code> like</p>
  543. <pre>
  544. &lt;formatter classname="org.apache.tools.ant.taskdefs.optional.junit.TearDownOnVmCrash"
  545. usefile="false"/&gt;
  546. </pre>
  547. <p>to your <code>junit</code> task.</p>
  548. <h3 id="enabletestlistenerevents"><code>ant.junit.enabletestlistenerevents</code> magic property</h3>
  549. <p><em>Since Ant 1.8.2</em> the <var>enableTestListenerEvents</var> attribute of the task controls
  550. whether fine grained logging messages will be sent to the task's verbose log. In addition to this
  551. attribute Ant will consult the property <code>ant.junit.enabletestlistenerevents</code> and the
  552. value of the property overrides the setting of the attribute.</p>
  553. <p>This property exists so that containers running Ant that depend on the additional logging events
  554. can ensure they will be generated even if the build file disables them.</p>
  555. <h3>Examples</h3>
  556. <pre>
  557. &lt;junit&gt;
  558. &lt;test name="my.test.TestCase"/&gt;
  559. &lt;/junit&gt;</pre>
  560. <p>Runs the test defined in <code>my.test.TestCase</code> in the same VM. No output will be
  561. generated unless the test fails.</p>
  562. <pre>
  563. &lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
  564. &lt;formatter type="plain"/&gt;
  565. &lt;test name="my.test.TestCase"/&gt;
  566. &lt;/junit&gt;</pre>
  567. <p>Runs the test defined in <code>my.test.TestCase</code> in a separate JVM. At the end of the
  568. test, a one-line summary will be printed. A detailed report of the test can be found
  569. in <samp>TEST-my.test.TestCase.txt</samp>. The build process will be stopped if the test fails.</p>
  570. <pre>
  571. &lt;junit printsummary="yes" haltonfailure="yes"&gt;
  572. &lt;classpath&gt;
  573. &lt;pathelement location="${build.tests}"/&gt;
  574. &lt;pathelement path="${java.class.path}"/&gt;
  575. &lt;/classpath&gt;
  576. &lt;formatter type="plain"/&gt;
  577. &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result"&gt;
  578. &lt;formatter type="xml"/&gt;
  579. &lt;/test&gt;
  580. &lt;batchtest fork="yes" todir="${reports.tests}"&gt;
  581. &lt;fileset dir="${src.tests}"&gt;
  582. &lt;include name="**/*Test*.java"/&gt;
  583. &lt;exclude name="**/AllTests.java"/&gt;
  584. &lt;/fileset&gt;
  585. &lt;/batchtest&gt;
  586. &lt;/junit&gt;</pre>
  587. <p>Runs <code>my.test.TestCase</code> in the same JVM, ignoring the given <code>CLASSPATH</code>;
  588. only a warning is printed if this test fails. In addition to the plain text test results, for this
  589. test a XML result will be output to <samp>result.xml</samp>. Then, for each matching file in the
  590. directory defined for <samp>${src.tests}</samp> a test is run in a separate JVM. If a test fails,
  591. the build process is aborted. Results are collected in files
  592. named <samp>TEST-</samp><var>name</var><samp>.txt</samp> and written
  593. to <samp>${reports.tests}</samp>.</p>
  594. <pre>
  595. &lt;target name=&quot;test&quot;&gt;
  596. &lt;property name=&quot;collector.dir&quot; value=&quot;${build.dir}/failingTests&quot;/&gt;
  597. &lt;property name=&quot;collector.class&quot; value=&quot;FailedTests&quot;/&gt;
  598. &lt;!-- Delete 'old' collector classes --&gt;
  599. &lt;delete&gt;
  600. &lt;fileset dir=&quot;${collector.dir}&quot; includes=&quot;${collector.class}*.class&quot;/&gt;
  601. &lt;/delete&gt;
  602. &lt;!-- compile the FailedTests class if present --&gt;
  603. &lt;javac srcdir=&quot;${collector.dir}&quot; destdir=&quot;${collector.dir}&quot;/&gt;
  604. &lt;available file=&quot;${collector.dir}/${collector.class}.class&quot; property=&quot;hasFailingTests&quot;/&gt;
  605. &lt;junit haltonerror=&quot;false&quot; haltonfailure=&quot;false&quot;&gt;
  606. &lt;sysproperty key=&quot;ant.junit.failureCollector&quot; value=&quot;${collector.dir}/${collector.class}&quot;/&gt;
  607. &lt;classpath&gt;
  608. &lt;pathelement location=&quot;${collector.dir}&quot;/&gt;
  609. &lt;/classpath&gt;
  610. &lt;batchtest todir=&quot;${collector.dir}&quot; unless=&quot;hasFailingTests&quot;&gt;
  611. &lt;fileset dir=&quot;${collector.dir}&quot; includes=&quot;**/*.java&quot; excludes=&quot;**/${collector.class}.*&quot;/&gt;
  612. &lt;!-- for initial creation of the FailingTests.java --&gt;
  613. &lt;formatter type=&quot;failure&quot;/&gt;
  614. &lt;!-- I want to see something ... --&gt;
  615. &lt;formatter type=&quot;plain&quot; usefile=&quot;false&quot;/&gt;
  616. &lt;/batchtest&gt;
  617. &lt;test name=&quot;FailedTests&quot; if=&quot;hasFailingTests&quot;&gt;
  618. &lt;!-- update the FailingTests.java --&gt;
  619. &lt;formatter type=&quot;failure&quot;/&gt;
  620. &lt;!-- again, I want to see something --&gt;
  621. &lt;formatter type=&quot;plain&quot; usefile=&quot;false&quot;/&gt;
  622. &lt;/test&gt;
  623. &lt;/junit&gt;
  624. &lt;/target&gt;</pre>
  625. <p>On the first run all tests are collected via the <code>&lt;batchtest/&gt;</code>
  626. element. Its <var>plain</var> formatter shows the output on the console. The <var>failure</var>
  627. formatter creates a Java source file in <samp>${build.dir}/failingTests/FailedTests.java</samp>
  628. which extends <code>junit.framework.TestCase</code> and returns from a <code>suite()</code> method a
  629. test suite for the failing tests.<br/> On a second run the collector class exists and instead of
  630. the <code>&lt;batchtest/&gt;</code> the single <code>&lt;test/&gt;</code> will run. So only the
  631. failing test cases are re-run. The two nested formatters are for displaying (for the user) and for
  632. updating the collector class.</p>
  633. <pre>
  634. &lt;junit fork="true"
  635. jvm="${platform.java}"&gt;
  636. &lt;jvmarg line="--patch-module ${module.name}=${build.test.classes}"/&gt;
  637. &lt;jvmarg line="--add-modules ${module.name}"/&gt;
  638. &lt;jvmarg line="--add-reads ${module.name}=ALL-UNNAMED"/&gt;
  639. &lt;jvmarg line="--add-exports ${module.name}/my.test=ALL-UNNAMED"/&gt;
  640. &lt;classpath&gt;
  641. &lt;pathelement path="${libs.junit}"/&gt;
  642. &lt;/classpath&gt;
  643. &lt;modulepath&gt;
  644. &lt;pathelement path="${modules}:${build.classes}"/&gt;
  645. &lt;/modulepath&gt;
  646. &lt;formatter type="plain"/&gt;
  647. &lt;test name="my.test.TestCase"/&gt;
  648. &lt;/junit&gt;
  649. </pre>
  650. <p>Runs <code>my.test.TestCase</code> as a white-box test in the forked JVM given by
  651. the <code>platform.java</code> property. The JUnit library is a part of an unnamed module while the
  652. tested project and required modules are on the module path. The tests do not have module-info file
  653. and are executed in the project module given by <code>module.name</code> property.<br/>
  654. The <code>--patch-module</code> Java option executes the tests built
  655. into <samp>${build.test.classes}</samp> in a module given by <code>module.name</code> property.<br/>
  656. The <code>--add-modules</code> Java option enables the tested module.<br/>
  657. The <code>--add-reads</code> Java option makes the unnamed module containing JUnit readable by
  658. tested module.<br/> The <code>--add-exports</code> Java option makes the non-exported test
  659. package <code>my.test</code> accessible from the unnamed module containing JUnit.</p>
  660. <pre>
  661. &lt;junit fork="true"
  662. jvm="${platform.java}"&gt;
  663. &lt;jvmarg line="--add-modules ${test.module.name}"/&gt;
  664. &lt;jvmarg line="--add-exports ${test.module.name}/my.test=junit,ALL-UNNAMED"/&gt;
  665. &lt;modulepath&gt;
  666. &lt;pathelement path="${modules}:${build.classes}:${libs.junit}"/&gt;
  667. &lt;/modulepath&gt;
  668. &lt;formatter type="plain"/&gt;
  669. &lt;test name="my.test.TestCase"/&gt;
  670. &lt;/junit&gt;
  671. </pre>
  672. <p>Runs <code>my.test.TestCase</code> as a black-box test in the forked JVM given by
  673. the <code>platform.java</code> property. The JUnit library is used as an automatic module. The
  674. tests' module-info requires the tested module and JUnit.<br/> The <code>--add-modules</code> Java
  675. option enables the test module.<br/> The <code>--add-exports</code> Java option makes the
  676. non-exported test package <code>my.test</code> accessible from the JUnit module and Ant's test
  677. runner. Another possibility is to export the test package in the tests' module-info
  678. by <code>exports my.test</code> directive.</p>
  679. </body>
  680. </html>