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

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