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 32 KiB

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