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

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