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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <html>
  2. <head>
  3. <title>JUnit Task</title>
  4. </head>
  5. <body>
  6. <h2><a name="junit">JUnit</a></h2>
  7. <h3>Description</h3>
  8. <p>This task runs tests from the JUnit testing framework. The latest
  9. version of the framework can be found at
  10. <a href="http://www.junit.org">http://www.junit.org</a>.
  11. This task has been tested with JUnit 3.0 up to JUnit 3.7, it won't
  12. work with versions prior to JUnit 3.0.</p>
  13. <p><strong>Note:</strong> This task depends on external libraries not included
  14. in the Ant distribution. See <a href="../install.html#librarydependencies">
  15. Library Dependencies</a> for more information.
  16. </p>
  17. <p>Tests are defined by nested <code>test</code> or
  18. <code>batchtest</code> tags, see <a href="#nested">nested
  19. elements</a>.</p>
  20. <h3>Parameters</h3>
  21. <table border="1" cellpadding="2" cellspacing="0">
  22. <tr>
  23. <td width="12%" valign="top"><b>Attribute</b></td>
  24. <td width="78%" valign="top"><b>Description</b></td>
  25. <td width="10%" valign="top"><b>Required</b></td>
  26. </tr>
  27. <tr>
  28. <td valign="top">printsummary</td>
  29. <td valign="top">Print one line statistics for each testcase. Can
  30. take the values &quot;on&quot;, &quot;off&quot; and
  31. &quot;withOutAndErr&quot; - &quot;withOutAndErr&quot; is the same
  32. as &quot;on&quot; but also includes the output of the test
  33. as written to System.out and System.err.</td>
  34. <td align="center" valign="top">No, default is "off"</td>
  35. </tr>
  36. <tr>
  37. <td valign="top">fork</td>
  38. <td valign="top">Run the tests in a separate VM.</td>
  39. <td align="center" valign="top">No, default is "off"</td>
  40. </tr>
  41. <tr>
  42. <td valign="top">haltonerror</td>
  43. <td valign="top">Stop the build process if an error occurs during the test
  44. run.</td>
  45. <td align="center" valign="top">No, default is "off"</td>
  46. </tr>
  47. <tr>
  48. <td valign="top">errorProperty</td>
  49. <td valign="top">The name of a property to set in the event of an error.</td>
  50. <td align="center" valign="top">No.</td>
  51. </tr>
  52. <tr>
  53. <td valign="top">haltonfailure</td>
  54. <td valign="top">Stop the build process if a test fails (errors are
  55. considered failures as well).</td>
  56. <td align="center" valign="top">No, default is "off"</td>
  57. </tr>
  58. <tr>
  59. <td valign="top">failureProperty</td>
  60. <td valign="top">The name of a property to set in the event of a failure
  61. (errors are considered failures as well).</td>
  62. <td align="center" valign="top">No.</td>
  63. </tr>
  64. <tr>
  65. <td valign="top">filtertrace</td>
  66. <td valign="top">Filter out junit and ant stack frames from error and failure stack traces.</td>
  67. <td align="center" valign="top">No, default is "on."</td>
  68. </tr>
  69. <tr>
  70. <td valign="top">timeout</td>
  71. <td valign="top">Cancel the individual tests if the don't finish
  72. in the given time (measured in milliseconds). Ignored if fork is
  73. disabled.</td>
  74. <td align="center" valign="top">No</td>
  75. </tr>
  76. <tr>
  77. <td valign="top">maxmemory</td>
  78. <td valign="top">Max amount of memory to allocate to the forked VM
  79. (ignored if fork is disabled)</td>
  80. <td align="center" valign="top">No</td>
  81. </tr>
  82. <tr>
  83. <td valign="top">jvm</td>
  84. <td valign="top">the command used to invoke the Java Virtual Machine,
  85. default is 'java'. The command is resolved by java.lang.Runtime.exec().
  86. Ignored if fork is disabled.</td>
  87. <td align="center" valign="top">No, default &quot;java&quot;</td>
  88. </tr>
  89. <tr>
  90. <td valign="top">dir</td>
  91. <td valign="top">The directory to invoke the VM in. (ignored if
  92. fork is disabled)</td>
  93. <td align="center" valign="top">No</td>
  94. </tr>
  95. <tr>
  96. <td valign="top">newenvironment</td>
  97. <td valign="top">Do not propagate old environment when new
  98. environment variables are specified. Ignored if fork is
  99. disabled.</td>
  100. <td align="center" valign="top">No, default is &quot;false&quot;.</td>
  101. </tr>
  102. <tr>
  103. <td valign="top">includeantruntime</td>
  104. <td valign="top">implicitly add the Ant classes required to run
  105. the tests and JUnit to the classpath in forked mode.</td>
  106. <td align="center" valign="top">No, default is &quot;true&quot;.</td>
  107. </tr>
  108. </table>
  109. <h3><a name="nested">Nested Elements</a></h3>
  110. <p><code>junit</code> supports a nested <code>&lt;classpath&gt;</code>
  111. element, that represents a <a href="../using.html#path">PATH like
  112. structure</a>.</p>
  113. <h4>jvmarg</h4>
  114. <p>If fork is enabled, additional parameters may be passed to the new
  115. VM via nested <code>&lt;jvmarg&gt;</code> attributes, for example:</p>
  116. <pre>
  117. &lt;junit fork=&quot;yes&quot;&gt;
  118. &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  119. ...
  120. &lt;/junit&gt;
  121. </pre>
  122. <p>would run the test in a VM without JIT.</p>
  123. <p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
  124. href="../using.html#arg">Command line arguments</a>.</p>
  125. <h4>sysproperty</h4>
  126. <p>Use nested <code>&lt;sysproperty&gt;</code> elements to specify system
  127. properties required by the class. These properties will be made available
  128. to the VM during the execution of the test (either ANT's VM or the forked VM).
  129. The attributes for this element are the same as for <a href="../CoreTasks/exec.html#env">environment variables</a>.</p>
  130. <pre>
  131. &lt;junit fork=&quot;no&quot;&gt;
  132. &lt;sysproperty key=&quot;basedir&quot; value=&quot;${basedir}&quot;/&gt;
  133. ...
  134. &lt;/junit&gt;
  135. </pre>
  136. <p>would run the test in ANT's VM and make the <code>basedir</code> property
  137. available to the test.</p>
  138. <h4>env</h4>
  139. <p>It is possible to specify environment variables to pass to the
  140. forked VM via nested <code>&lt;env&gt;</code> elements. See the
  141. description in the section about
  142. <a href="../CoreTasks/exec.html#env">exec</a></p>
  143. <p>Settings will be ignored if fork is disabled.</p>
  144. <h4>formatter</h4>
  145. <p>The results of the tests can be printed in different
  146. formats. Output will always be sent to a file unless you set the
  147. usefile attribute to false, the name of the file is determined by the
  148. name of the test and can be set by the <code>outfile</code> attribute
  149. of <code>&lt;test&gt;</code>.</p>
  150. <p>There are three predefined formatters, one prints the test results
  151. in XML format, the other emit plain text. The formatter named
  152. &quot;brief&quot; will only print detailed information for testcases
  153. that failed, while &quot;plain&quot; gives a little statistics line
  154. for all test cases. Custom formatters that need to implement
  155. <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
  156. can be specified.</p>
  157. <table border="1" cellpadding="2" cellspacing="0">
  158. <tr>
  159. <td width="12%" valign="top"><b>Attribute</b></td>
  160. <td width="78%" valign="top"><b>Description</b></td>
  161. <td width="10%" valign="top"><b>Required</b></td>
  162. </tr>
  163. <tr>
  164. <td valign="top">type</td>
  165. <td valign="top">Use a predefined formatter (either
  166. &quot;xml&quot;, &quot;plain&quot; or &quot;brief&quot;).</td>
  167. <td align="center" rowspan="2">Exactly one of these.</td>
  168. </tr>
  169. <tr>
  170. <td valign="top">classname</td>
  171. <td valign="top">Name of a custom formatter class.</td>
  172. </tr>
  173. <tr>
  174. <td valign="top">extension</td>
  175. <td valign="top">Extension to append to the output filename.</td>
  176. <td align="center">Yes, if classname has been used.</td>
  177. </tr>
  178. <tr>
  179. <td valign="top">usefile</td>
  180. <td valign="top">Boolean that determines whether output should be
  181. sent to a file.</td>
  182. <td align="center">No, default true.</td>
  183. </tr>
  184. </table>
  185. <h4>test</h4>
  186. <p>Defines a single test class.</p>
  187. <table border="1" cellpadding="2" cellspacing="0">
  188. <tr>
  189. <td width="12%" valign="top"><b>Attribute</b></td>
  190. <td width="78%" valign="top"><b>Description</b></td>
  191. <td width="10%" valign="top"><b>Required</b></td>
  192. </tr>
  193. <tr>
  194. <td valign="top">name</td>
  195. <td valign="top">Name of the test class</td>
  196. <td align="center">Yes</td>
  197. </tr>
  198. <tr>
  199. <td valign="top">fork</td>
  200. <td valign="top">Run the tests in a separate VM.
  201. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  202. <td align="center" valign="top">No</td>
  203. </tr>
  204. <tr>
  205. <td valign="top">haltonerror</td>
  206. <td valign="top">Stop the build process if an error occurs during the test
  207. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  208. <td align="center" valign="top">No</td>
  209. </tr>
  210. <tr>
  211. <td valign="top">errorProperty</td>
  212. <td valign="top">The name of a property to set in the event of an error.
  213. Overrides value set in &lt;junit&gt;.</td>
  214. <td align="center" valign="top">No.</td>
  215. </tr>
  216. <tr>
  217. <td valign="top">haltonfailure</td>
  218. <td valign="top">Stop the build process if a test fails (errors are
  219. considered failures as well). Overrides value set in
  220. <code>&lt;junit&gt;</code>.</td>
  221. <td align="center" valign="top">No</td>
  222. </tr>
  223. <tr>
  224. <td valign="top">failureProperty</td>
  225. <td valign="top">The name of a property to set in the event of a failure
  226. (errors are considered failures as well). Overrides value set in &lt;junit&gt;.</td>
  227. <td align="center" valign="top">No.</td>
  228. </tr>
  229. <tr>
  230. <td valign="top">filtertrace</td>
  231. <td valign="top">Filter out junit and ant stack frames from error and failure stack
  232. traces. Overrides value set in &lt;junit&gt;</td>
  233. <td align="center" valign="top">No, default is "on."</td>
  234. </tr>
  235. <tr>
  236. <td valign="top">todir</td>
  237. <td valign="top">Directory to write the reports to.</td>
  238. <td align="center" valign="top">No, default is current directory.</td>
  239. </tr>
  240. <tr>
  241. <td valign="top">outfile</td>
  242. <td valign="top">Base name of the test result. The full filename is
  243. determined by this attribute and the extension of
  244. <code>formatter</code>.</td>
  245. <td align="center" valign="top">No, default is
  246. <code>TEST-name</code> using the <code>name</code> attribute.</td>
  247. </tr>
  248. <tr>
  249. <td valign="top">if</td>
  250. <td valign="top">Only run test if the named property is set.</td>
  251. <td align="center" valign="top">No</td>
  252. </tr>
  253. <tr>
  254. <td valign="top">unless</td>
  255. <td valign="top">Only run test if the named property is <b>not</b> set.</td>
  256. <td align="center" valign="top">No</td>
  257. </tr>
  258. </table>
  259. <p>Tests can define their own formatters via nested
  260. <code>&lt;formatter&gt;</code> elements.</p>
  261. <h4>batchtest</h4>
  262. <p>Define a number of tests based on pattern matching.</p>
  263. <p><code>batchtest</code> collects the included files from any number
  264. of nested <a
  265. href="../CoreTypes/fileset.html"><code>&lt;fileset&gt;</code></a>s. It then
  266. generates a test class name for each file that ends in
  267. <code>.java</code> or <code>.class</code>.</p>
  268. <table border="1" cellpadding="2" cellspacing="0">
  269. <tr>
  270. <td width="12%" valign="top"><b>Attribute</b></td>
  271. <td width="78%" valign="top"><b>Description</b></td>
  272. <td width="10%" valign="top"><b>Required</b></td>
  273. </tr>
  274. <tr>
  275. <td valign="top">fork</td>
  276. <td valign="top">Run the tests in a separate VM.
  277. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  278. <td align="center" valign="top">No</td>
  279. </tr>
  280. <tr>
  281. <td valign="top">haltonerror</td>
  282. <td valign="top">Stop the build process if an error occurs during the test
  283. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  284. <td align="center" valign="top">No</td>
  285. </tr>
  286. <tr>
  287. <td valign="top">errorProperty</td>
  288. <td valign="top">The name of a property to set in the event of an error.
  289. Overrides value set in &lt;junit&gt;.</td>
  290. <td align="center" valign="top">No.</td>
  291. </tr>
  292. <tr>
  293. <td valign="top">haltonfailure</td>
  294. <td valign="top">Stop the build process if a test fails (errors are
  295. considered failures as well). Overrides value set in
  296. <code>&lt;junit&gt;</code>.</td>
  297. <td align="center" valign="top">No</td>
  298. </tr>
  299. <tr>
  300. <td valign="top">failureProperty</td>
  301. <td valign="top">The name of a property to set in the event of a failure
  302. (errors are considered failures as well). Overrides value set in &lt;junit&gt;</td>
  303. <td align="center" valign="top">No.</td>
  304. </tr>
  305. <tr>
  306. <td valign="top">filtertrace</td>
  307. <td valign="top">Filter out junit and ant stack frames from error and failure stack
  308. traces. Overrides value set in &lt;junit&gt;</td>
  309. <td align="center" valign="top">No, default is "on."</td>
  310. </tr>
  311. <tr>
  312. <td valign="top">todir</td>
  313. <td valign="top">Directory to write the reports to.</td>
  314. <td align="center" valign="top">No, default is current directory.</td>
  315. </tr>
  316. <tr>
  317. <td valign="top">if</td>
  318. <td valign="top">Only run tests if the named property is set.</td>
  319. <td align="center" valign="top">No</td>
  320. </tr>
  321. <tr>
  322. <td valign="top">unless</td>
  323. <td valign="top">Only run tests if the named property is <b>not</b> set.</td>
  324. <td align="center" valign="top">No</td>
  325. </tr>
  326. </table>
  327. <p>Batchtests can define their own formatters via nested
  328. <code>&lt;formatter&gt;</code> elements.</p>
  329. <h3>Examples</h3>
  330. <pre>
  331. &lt;junit&gt;
  332. &lt;test name="my.test.TestCase" /&gt;
  333. &lt;/junit&gt;
  334. </pre>
  335. <p>Runs the test defined in <code>my.test.TestCase</code> in the same
  336. VM. No output will be generated unless the test fails.</p>
  337. <pre>
  338. &lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
  339. &lt;formatter type="plain" /&gt;
  340. &lt;test name="my.test.TestCase" /&gt;
  341. &lt;/junit&gt;
  342. </pre>
  343. <p>Runs the test defined in <code>my.test.TestCase</code> in a
  344. separate VM. At the end of the test a single line summary will be
  345. printed. A detailed report of the test can be found in
  346. <code>TEST-my.test.TestCase.txt</code>. The build process will be
  347. stopped if the test fails.</p>
  348. <pre>
  349. &lt;junit printsummary="yes" haltonfailure="yes"&gt;
  350. &lt;classpath&gt;
  351. &lt;pathelement location="${build.tests}" /&gt;
  352. &lt;pathelement path="${java.class.path}" /&gt;
  353. &lt;/classpath&gt;
  354. &lt;formatter type="plain" /&gt;
  355. &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result" &gt;
  356. &lt;formatter type="xml" /&gt;
  357. &lt;/test&gt;
  358. &lt;batchtest fork="yes" todir="${reports.tests}"&gt;
  359. &lt;fileset dir="${src.tests}"&gt;
  360. &lt;include name="**/*Test*.java" /&gt;
  361. &lt;exclude name="**/AllTests.java" /&gt;
  362. &lt;/fileset&gt;
  363. &lt;/batchtest&gt;
  364. &lt;/junit&gt;
  365. </pre>
  366. <p>Runs <code>my.test.TestCase</code> in the same VM (ignoring the
  367. given CLASSPATH), only a warning is printed if this test fails. In
  368. addition to the plain text test results, for this test a XML result
  369. will be output to <code>result.xml</code>.</p>
  370. <p>For each matching file in the directory <code>${src.tests}</code> a
  371. test is run in a separate VM. If a test fails, the build process is
  372. aborted. Results are collected in files named
  373. <code>TEST-<em>name</em>.txt</code> and written to <code>${reports.tests}</code>.</p>
  374. <p>By using the errorProperty and failureProperty attributes, it is possible to
  375. perform setup work (such as starting an external server), execute the test,
  376. clean up, and still fail the build in the event of a failure.</p>
  377. <p>The filtertrace attribute condenses error and failure stack traces before reporting them.
  378. It works with the plain and xml formatters. It filters out lines that begin with the following
  379. string patterns:<pre>
  380. "junit.framework.TestCase"
  381. "junit.framework.TestResult"
  382. "junit.framework.TestSuite"
  383. "junit.framework.Assert."
  384. "junit.swingui.TestRunner"
  385. "junit.awtui.TestRunner"
  386. "junit.textui.TestRunner"
  387. "java.lang.reflect.Method.invoke("
  388. "org.apache.tools.ant."</pre></p>
  389. <hr>
  390. <p align="center">Copyright &copy; 2001-2002 Apache Software Foundation. All rights
  391. Reserved.</p>
  392. </body>
  393. </html>