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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. </table>
  91. <h3><a name="nested">Nested Elements</a></h3>
  92. <p><code>junit</code> supports a nested <code>&lt;classpath&gt;</code>
  93. element, that represents a <a href="../using.html#path">PATH like
  94. structure</a>.</p>
  95. <h4>jvmarg</h4>
  96. <p>If fork is enabled, additional parameters may be passed to the new
  97. VM via nested <code>&lt;jvmarg&gt;</code> attributes, for example:</p>
  98. <pre>
  99. &lt;junit fork=&quot;yes&quot;&gt;
  100. &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  101. ...
  102. &lt;/junit&gt;
  103. </pre>
  104. <p>would run the test in a VM without JIT.</p>
  105. <p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
  106. href="../using.html#arg">Command line arguments</a>.</p>
  107. <h4>sysproperty</h4>
  108. <p>Use nested <code>&lt;sysproperty&gt;</code> elements to specify system
  109. properties required by the class. These properties will be made available
  110. to the VM during the execution of the test (either ANT's VM or the forked VM).
  111. The attributes for this element are the same as for <a href="../CoreTasks/exec.html#env">environment variables</a>.</p>
  112. <pre>
  113. &lt;junit fork=&quot;no&quot;&gt;
  114. &lt;sysproperty key=&quot;basedir&quot; value=&quot;${basedir}&quot;/&gt;
  115. ...
  116. &lt;/junit&gt;
  117. </pre>
  118. <p>would run the test in ANT's VM and make the <code>basedir</code> property
  119. available to the test.</p>
  120. <h4>formatter</h4>
  121. <p>The results of the tests can be printed in different
  122. formats. Output will always be sent to a file unless you set the
  123. usefile attribute to false, the name of the file is determined by the
  124. name of the test and can be set by the <code>outfile</code> attribute
  125. of <code>&lt;test&gt;</code>.</p>
  126. <p>There are three predefined formatters, one prints the test results
  127. in XML format, the other emit plain text. The formatter named
  128. &quot;brief&quot; will only print detailed information for testcases
  129. that failed, while &quot;plain&quot; gives a little statistics line
  130. for all test cases. Custom formatters that need to implement
  131. <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
  132. can be specified.</p>
  133. <table border="1" cellpadding="2" cellspacing="0">
  134. <tr>
  135. <td width="12%" valign="top"><b>Attribute</b></td>
  136. <td width="78%" valign="top"><b>Description</b></td>
  137. <td width="10%" valign="top"><b>Required</b></td>
  138. </tr>
  139. <tr>
  140. <td valign="top">type</td>
  141. <td valign="top">Use a predefined formatter (either
  142. &quot;xml&quot;, &quot;plain&quot; or &quot;brief&quot;).</td>
  143. <td align="center" rowspan="2">Exactly one of these.</td>
  144. </tr>
  145. <tr>
  146. <td valign="top">classname</td>
  147. <td valign="top">Name of a custom formatter class.</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">extension</td>
  151. <td valign="top">Extension to append to the output filename.</td>
  152. <td align="center">Yes, if classname has been used.</td>
  153. </tr>
  154. <tr>
  155. <td valign="top">usefile</td>
  156. <td valign="top">Boolean that determines whether output should be
  157. sent to a file.</td>
  158. <td align="center">No, default true.</td>
  159. </tr>
  160. </table>
  161. <h4>test</h4>
  162. <p>Defines a single test class.</p>
  163. <table border="1" cellpadding="2" cellspacing="0">
  164. <tr>
  165. <td width="12%" valign="top"><b>Attribute</b></td>
  166. <td width="78%" valign="top"><b>Description</b></td>
  167. <td width="10%" valign="top"><b>Required</b></td>
  168. </tr>
  169. <tr>
  170. <td valign="top">name</td>
  171. <td valign="top">Name of the test class</td>
  172. <td align="center">Yes</td>
  173. </tr>
  174. <tr>
  175. <td valign="top">fork</td>
  176. <td valign="top">Run the tests in a separate VM.
  177. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  178. <td align="center" valign="top">No</td>
  179. </tr>
  180. <tr>
  181. <td valign="top">haltonerror</td>
  182. <td valign="top">Stop the build process if an error occurs during the test
  183. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  184. <td align="center" valign="top">No</td>
  185. </tr>
  186. <tr>
  187. <td valign="top">errorProperty</td>
  188. <td valign="top">The name of a property to set in the event of an error.
  189. Overrides value set in &lt;junit&gt;.</td>
  190. <td align="center" valign="top">No.</td>
  191. </tr>
  192. <tr>
  193. <td valign="top">haltonfailure</td>
  194. <td valign="top">Stop the build process if a test fails (errors are
  195. considered failures as well). Overrides value set in
  196. <code>&lt;junit&gt;</code>.</td>
  197. <td align="center" valign="top">No</td>
  198. </tr>
  199. <tr>
  200. <td valign="top">failureProperty</td>
  201. <td valign="top">The name of a property to set in the event of a failure
  202. (errors are considered failures as well). Overrides value set in &lt;junit&gt;.</td>
  203. <td align="center" valign="top">No.</td>
  204. </tr>
  205. <tr>
  206. <td valign="top">filtertrace</td>
  207. <td valign="top">Filter out junit and ant stack frames from error and failure stack
  208. traces. Overrides value set in &lt;junit&gt;</td>
  209. <td align="center" valign="top">No, default is "on."</td>
  210. </tr>
  211. <tr>
  212. <td valign="top">todir</td>
  213. <td valign="top">Directory to write the reports to.</td>
  214. <td align="center" valign="top">No, default is current directory.</td>
  215. </tr>
  216. <tr>
  217. <td valign="top">outfile</td>
  218. <td valign="top">Base name of the test result. The full filename is
  219. determined by this attribute and the extension of
  220. <code>formatter</code>.</td>
  221. <td align="center" valign="top">No, default is
  222. <code>TEST-name</code> using the <code>name</code> attribute.</td>
  223. </tr>
  224. <tr>
  225. <td valign="top">if</td>
  226. <td valign="top">Only run test if the named property is set.</td>
  227. <td align="center" valign="top">No</td>
  228. </tr>
  229. <tr>
  230. <td valign="top">unless</td>
  231. <td valign="top">Only run test if the named property is <b>not</b> set.</td>
  232. <td align="center" valign="top">No</td>
  233. </tr>
  234. </table>
  235. <p>Tests can define their own formatters via nested
  236. <code>&lt;formatter&gt;</code> elements.</p>
  237. <h4>batchtest</h4>
  238. <p>Define a number of tests based on pattern matching.</p>
  239. <p><code>batchtest</code> collects the included files from any number
  240. of nested <a
  241. href="../CoreTypes/fileset.html"><code>&lt;fileset&gt;</code></a>s. It then
  242. generates a test class name for each file that ends in
  243. <code>.java</code> or <code>.class</code>.</p>
  244. <table border="1" cellpadding="2" cellspacing="0">
  245. <tr>
  246. <td width="12%" valign="top"><b>Attribute</b></td>
  247. <td width="78%" valign="top"><b>Description</b></td>
  248. <td width="10%" valign="top"><b>Required</b></td>
  249. </tr>
  250. <tr>
  251. <td valign="top">fork</td>
  252. <td valign="top">Run the tests in a separate VM.
  253. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  254. <td align="center" valign="top">No</td>
  255. </tr>
  256. <tr>
  257. <td valign="top">haltonerror</td>
  258. <td valign="top">Stop the build process if an error occurs during the test
  259. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  260. <td align="center" valign="top">No</td>
  261. </tr>
  262. <tr>
  263. <td valign="top">errorProperty</td>
  264. <td valign="top">The name of a property to set in the event of an error.
  265. Overrides value set in &lt;junit&gt;.</td>
  266. <td align="center" valign="top">No.</td>
  267. </tr>
  268. <tr>
  269. <td valign="top">haltonfailure</td>
  270. <td valign="top">Stop the build process if a test fails (errors are
  271. considered failures as well). Overrides value set in
  272. <code>&lt;junit&gt;</code>.</td>
  273. <td align="center" valign="top">No</td>
  274. </tr>
  275. <tr>
  276. <td valign="top">failureProperty</td>
  277. <td valign="top">The name of a property to set in the event of a failure
  278. (errors are considered failures as well). Overrides value set in &lt;junit&gt;</td>
  279. <td align="center" valign="top">No.</td>
  280. </tr>
  281. <tr>
  282. <td valign="top">filtertrace</td>
  283. <td valign="top">Filter out junit and ant stack frames from error and failure stack
  284. traces. Overrides value set in &lt;junit&gt;</td>
  285. <td align="center" valign="top">No, default is "on."</td>
  286. </tr>
  287. <tr>
  288. <td valign="top">todir</td>
  289. <td valign="top">Directory to write the reports to.</td>
  290. <td align="center" valign="top">No, default is current directory.</td>
  291. </tr>
  292. <tr>
  293. <td valign="top">if</td>
  294. <td valign="top">Only run tests if the named property is set.</td>
  295. <td align="center" valign="top">No</td>
  296. </tr>
  297. <tr>
  298. <td valign="top">unless</td>
  299. <td valign="top">Only run tests if the named property is <b>not</b> set.</td>
  300. <td align="center" valign="top">No</td>
  301. </tr>
  302. </table>
  303. <p>Batchtests can define their own formatters via nested
  304. <code>&lt;formatter&gt;</code> elements.</p>
  305. <h3>Examples</h3>
  306. <pre>
  307. &lt;junit&gt;
  308. &lt;test name="my.test.TestCase" /&gt;
  309. &lt;/junit&gt;
  310. </pre>
  311. <p>Runs the test defined in <code>my.test.TestCase</code> in the same
  312. VM. No output will be generated unless the test fails.</p>
  313. <pre>
  314. &lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
  315. &lt;formatter type="plain" /&gt;
  316. &lt;test name="my.test.TestCase" /&gt;
  317. &lt;/junit&gt;
  318. </pre>
  319. <p>Runs the test defined in <code>my.test.TestCase</code> in a
  320. separate VM. At the end of the test a single line summary will be
  321. printed. A detailed report of the test can be found in
  322. <code>TEST-my.test.TestCase.txt</code>. The build process will be
  323. stopped if the test fails.</p>
  324. <pre>
  325. &lt;junit printsummary="yes" haltonfailure="yes"&gt;
  326. &lt;classpath&gt;
  327. &lt;pathelement location="${build.tests}" /&gt;
  328. &lt;pathelement path="${java.class.path}" /&gt;
  329. &lt;/classpath&gt;
  330. &lt;formatter type="plain" /&gt;
  331. &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result" &gt;
  332. &lt;formatter type="xml" /&gt;
  333. &lt;/test&gt;
  334. &lt;batchtest fork="yes" todir="${reports.tests}"&gt;
  335. &lt;fileset dir="${src.tests}"&gt;
  336. &lt;include name="**/*Test*.java" /&gt;
  337. &lt;exclude name="**/AllTests.java" /&gt;
  338. &lt;/fileset&gt;
  339. &lt;/batchtest&gt;
  340. &lt;/junit&gt;
  341. </pre>
  342. <p>Runs <code>my.test.TestCase</code> in the same VM (ignoring the
  343. given CLASSPATH), only a warning is printed if this test fails. In
  344. addition to the plain text test results, for this test a XML result
  345. will be output to <code>result.xml</code>.</p>
  346. <p>For each matching file in the directory <code>${src.tests}</code> a
  347. test is run in a separate VM. If a test fails, the build process is
  348. aborted. Results are collected in files named
  349. <code>TEST-<em>name</em>.txt</code> and written to <code>${reports.tests}</code>.</p>
  350. <p>By using the errorProperty and failureProperty attributes, it is possible to
  351. perform setup work (such as starting an external server), execute the test,
  352. clean up, and still fail the build in the event of a failure.</p>
  353. <p>The filtertrace attribute condenses error and failure stack traces before reporting them.
  354. It works with the plain and xml formatters. It filters out lines that begin with the following
  355. string patterns:<pre>
  356. "junit.framework.TestCase"
  357. "junit.framework.TestResult"
  358. "junit.framework.TestSuite"
  359. "junit.framework.Assert."
  360. "junit.swingui.TestRunner"
  361. "junit.awtui.TestRunner"
  362. "junit.textui.TestRunner"
  363. "java.lang.reflect.Method.invoke("
  364. "org.apache.tools.ant."</pre></p>
  365. <hr>
  366. <p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
  367. Reserved.</p>
  368. </body>
  369. </html>