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

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