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 9.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <a
  9. href="http://www.xprogramming.com/software.htm">http://www.xprogramming.com/software.htm</a>.
  10. This task requires JUnit 3.0 or above.</p>
  11. <p>Tests are defined by nested <code>test</code> or
  12. <code>batchtest</code> tags, see <a href="#nested">nested
  13. elements</a>.</p>
  14. <h3>Parameters</h3>
  15. <table border="1" cellpadding="2" cellspacing="0">
  16. <tr>
  17. <td width="12%" valign="top"><b>Attribute</b></td>
  18. <td width="78%" valign="top"><b>Description</b></td>
  19. <td width="10%" valign="top"><b>Required</b></td>
  20. </tr>
  21. <tr>
  22. <td valign="top">printsummary</td>
  23. <td valign="top">Print one line statistics for each testcase.</td>
  24. <td align="center" valign="top">No, default is "off"</td>
  25. </tr>
  26. <tr>
  27. <td valign="top">fork</td>
  28. <td valign="top">Run the tests in a separate VM.</td>
  29. <td align="center" valign="top">No, default is "off"</td>
  30. </tr>
  31. <tr>
  32. <td valign="top">haltonerror</td>
  33. <td valign="top">Stop the build process if an error occurs during the test
  34. run.</td>
  35. <td align="center" valign="top">No, default is "off"</td>
  36. </tr>
  37. <tr>
  38. <td valign="top">haltonfailure</td>
  39. <td valign="top">Stop the build process if a test fails (errors are
  40. considered failures as well).</td>
  41. <td align="center" valign="top">No, default is "off"</td>
  42. </tr>
  43. <tr>
  44. <td valign="top">timeout</td>
  45. <td valign="top">Cancel the individual tests if the don't finish
  46. in the given time (measured in milliseconds). Ignored if fork is
  47. disabled.</td>
  48. <td align="center" valign="top">No</td>
  49. </tr>
  50. <tr>
  51. <td valign="top">maxmemory</td>
  52. <td valign="top">Max amount of memory to allocate to the forked VM
  53. (ignored if fork is disabled)</td>
  54. <td align="center" valign="top">No</td>
  55. </tr>
  56. <tr>
  57. <td valign="top">jvm</td>
  58. <td valign="top">the command used to invoke the Java Virtual Machine,
  59. default is 'java'. The command is resolved by java.lang.Runtime.exec().
  60. Ignored if fork is disabled.</td>
  61. <td align="center" valign="top">No, default &quot;java&quot;</td>
  62. </tr>
  63. </table>
  64. <h3><a name="nested">Nested Elements</a></h3>
  65. <p><code>junit</code> supports a nested <code>&lt;classpath&gt;</code>
  66. element, that represents a <a href="index.html#path">PATH like
  67. structure</a>. The value is ignored if <code>fork</code> is
  68. disabled.</p>
  69. <h4>jvmarg</h4>
  70. <p>If fork is enabled, additional parameters may be passed to the new
  71. VM via nested <code>&lt;jvmarg&gt;</code> attributes, for example:</p>
  72. <pre><blockquote>
  73. &lt;junit fork=&quot;yes&quot;&gt;
  74. &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  75. &lt;/junit&gt;
  76. </blockquote></pre>
  77. would run the test in a VM without JIT.</p>
  78. <p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
  79. href="index.html#arg">Command line arguments</a>.</p>
  80. <h4>formatter</h4>
  81. <p>The results of the tests can be printed in different
  82. formats. Output will always be sent to a file, the name of the file is
  83. determined by the name of the test and can be set by the
  84. <code>outfile</code> attribute of <code>&lt;test&gt;</code>.
  85. <p>There are two predefined formatters, one prints the test results in
  86. XML format, the other emits plain text. Custom formatters that need to
  87. implement
  88. <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
  89. can be specified.</p>
  90. <table border="1" cellpadding="2" cellspacing="0">
  91. <tr>
  92. <td width="12%" valign="top"><b>Attribute</b></td>
  93. <td width="78%" valign="top"><b>Description</b></td>
  94. <td width="10%" valign="top"><b>Required</b></td>
  95. </tr>
  96. <tr>
  97. <td valign="top">type</td>
  98. <td valign="top">Use a predefined formatter (either "xml" or "plain").</td>
  99. <td align="center" rowspan="2">Exactly one of these.</td>
  100. </tr>
  101. <tr>
  102. <td valign="top">classname</td>
  103. <td valign="top">Name of a custom formatter class.</td>
  104. </tr>
  105. <tr>
  106. <td valign="top">extension</td>
  107. <td valign="top">Extension to append to the output filename.</td>
  108. <td align="center">Yes, if classname has been used.</td>
  109. </tr>
  110. </table>
  111. <h4>test</h4>
  112. <p>Defines a single test class.</p>
  113. <table border="1" cellpadding="2" cellspacing="0">
  114. <tr>
  115. <td width="12%" valign="top"><b>Attribute</b></td>
  116. <td width="78%" valign="top"><b>Description</b></td>
  117. <td width="10%" valign="top"><b>Required</b></td>
  118. </tr>
  119. <tr>
  120. <td valign="top">name</td>
  121. <td valign="top">Name of the test class</td>
  122. <td align="center">Yes</td>
  123. </tr>
  124. <tr>
  125. <td valign="top">fork</td>
  126. <td valign="top">Run the tests in a separate VM.
  127. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  128. <td align="center" valign="top">No</td>
  129. </tr>
  130. <tr>
  131. <td valign="top">haltonerror</td>
  132. <td valign="top">Stop the build process if an error occurs during the test
  133. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  134. <td align="center" valign="top">No</td>
  135. </tr>
  136. <tr>
  137. <td valign="top">haltonfailure</td>
  138. <td valign="top">Stop the build process if a test fails (errors are
  139. considered failures as well). Overrides value set in
  140. <code>&lt;junit&gt;</code>.</td>
  141. <td align="center" valign="top">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">outfile</td>
  145. <td valign="top">Base name of the test result. The full filename is
  146. determined by this attribute and the extension of
  147. <code>formatter</code>.</td>
  148. <td align="center" valign="top">No, default is
  149. <code>TEST-name</code> using the <code>name</code> attribute.</td>
  150. </tr>
  151. <tr>
  152. <td valign="top">if</td>
  153. <td valign="top">Only run test if the named property is set.</td>
  154. <td align="center" valign="top">No</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">unless</td>
  158. <td valign="top">Only run test if the named property is <b>not</b> set.</td>
  159. <td align="center" valign="top">No</td>
  160. </tr>
  161. </table>
  162. <p>Tests can define their own formatters via nested
  163. <code>&lt;formatter&gt;</code> elements.</p>
  164. <h4>batchtest</h4>
  165. <p>Define a number of tests based on pattern matching.</p>
  166. <p><code>batchtest</code> collects the included files from any number
  167. of nested <a
  168. href="index.html#fileset"><code>&lt;fileset&gt;</code></a> and
  169. <code>&lt;filesetref&gt;</code> (referring to a
  170. <code>&lt;fileset&gt;</code> defined elsewhere via its <code>id</code>
  171. attribute) elements. It then generates a test class name for each file
  172. that ends in <code>.java</code> or <code>.class</code>.</p>
  173. <table border="1" cellpadding="2" cellspacing="0">
  174. <tr>
  175. <td width="12%" valign="top"><b>Attribute</b></td>
  176. <td width="78%" valign="top"><b>Description</b></td>
  177. <td width="10%" valign="top"><b>Required</b></td>
  178. </tr>
  179. <tr>
  180. <td valign="top">fork</td>
  181. <td valign="top">Run the tests in a separate VM.
  182. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  183. <td align="center" valign="top">No</td>
  184. </tr>
  185. <tr>
  186. <td valign="top">haltonerror</td>
  187. <td valign="top">Stop the build process if an error occurs during the test
  188. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  189. <td align="center" valign="top">No</td>
  190. </tr>
  191. <tr>
  192. <td valign="top">haltonfailure</td>
  193. <td valign="top">Stop the build process if a test fails (errors are
  194. considered failures as well). Overrides value set in
  195. <code>&lt;junit&gt;</code>.</td>
  196. <td align="center" valign="top">No</td>
  197. </tr>
  198. <tr>
  199. <td valign="top">if</td>
  200. <td valign="top">Only run tests if the named property is set.</td>
  201. <td align="center" valign="top">No</td>
  202. </tr>
  203. <tr>
  204. <td valign="top">unless</td>
  205. <td valign="top">Only run tests if the named property is <b>not</b> set.</td>
  206. <td align="center" valign="top">No</td>
  207. </tr>
  208. </table>
  209. <p>Batchtests can define their own formatters via nested
  210. <code>&lt;formatter&gt;</code> elements.</p>
  211. <h3>Examples</h3>
  212. <pre><blockquote>
  213. &lt;junit&gt;
  214. &lt;test name="my.test.TestCase" /&gt;
  215. &lt;/junit&gt;
  216. </pre></blockquote>
  217. <p>Runs the test defined in <code>my.test.TestCase</code> in the same
  218. VM. No output will be generated unless the test fails.</p>
  219. <pre><blockquote>
  220. &lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
  221. &lt;formatter type="plain" /&gt;
  222. &lt;test name="my.test.TestCase" /&gt;
  223. &lt;/junit&gt;
  224. </pre></blockquote>
  225. <p>Runs the test defined in <code>my.test.TestCase</code> in a
  226. separate VM. At the end of the test a single line summary will be
  227. printed. A detailed report of the test can be found in
  228. <code>TEST-my.test.TestCase.txt</code>. The build process will be
  229. stopped if the test fails.</p>
  230. <pre><blockquote>
  231. &lt;junit printsummary="yes" haltonfailure="yes"&gt;
  232. &lt;classpath&gt;
  233. &lt;pathelement location="${build.tests}" /&gt;
  234. &lt;pathelement path="${java.class.path}" /&gt;
  235. &lt;/classpath&gt;
  236. &lt;formatter type="plain" /&gt;
  237. &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result" &gt;
  238. &lt;formatter type="xml" /&gt;
  239. &lt;/test&gt;
  240. &lt;batchtest fork="yes"&gt;
  241. &lt;fileset dir="${src.tests}"&gt;
  242. &lt;include name="**/*Test*.java" /&gt;
  243. &lt;exclude name="**/AllTests.java" /&lt;
  244. &lt;/fileset&gt;
  245. &lt;/batchtest&gt;
  246. &lt;/junit&gt;
  247. </pre></blockquote>
  248. <p>Runs <code>my.test.TestCase</code> in the same VM (ignoring the
  249. given CLASSPATH), only a warning is printed if this test fails. In
  250. addition to the plain text test results, for this test a XML result
  251. will be output to <code>result.xml</code>.</p>
  252. <p>For each matching file in the directory <code>${src.tests}</code> a
  253. test is run in a separate VM. If a test fails, the build process is
  254. aborted. Results are collected in files named
  255. <code>TEST-<em>name</em>.txt</code>.</p>
  256. </body>
  257. </html>