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

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