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