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

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