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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 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. <tr>
  64. <td valign="top">dir</td>
  65. <td valign="top">The directory to invoke the VM in. (ignored if
  66. fork is disabled)</td>
  67. <td align="center" valign="top">No</td>
  68. </tr>
  69. </table>
  70. <h3><a name="nested">Nested Elements</a></h3>
  71. <p><code>junit</code> supports a nested <code>&lt;classpath&gt;</code>
  72. element, that represents a <a href="index.html#path">PATH like
  73. structure</a>. The value is ignored if <code>fork</code> is
  74. disabled.</p>
  75. <h4>jvmarg</h4>
  76. <p>If fork is enabled, additional parameters may be passed to the new
  77. VM via nested <code>&lt;jvmarg&gt;</code> attributes, for example:</p>
  78. <pre><blockquote>
  79. &lt;junit fork=&quot;yes&quot;&gt;
  80. &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  81. &lt;/junit&gt;
  82. </blockquote></pre>
  83. would run the test in a VM without JIT.</p>
  84. <p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
  85. href="index.html#arg">Command line arguments</a>.</p>
  86. <h4>formatter</h4>
  87. <p>The results of the tests can be printed in different
  88. formats. Output will always be sent to a file unless you set the
  89. usefile attribute to false, the name of the file is determined by the
  90. name of the test and can be set by the <code>outfile</code> attribute
  91. of <code>&lt;test&gt;</code>.
  92. <p>There are two predefined formatters, one prints the test results in
  93. XML format, the other emits plain text. Custom formatters that need to
  94. implement
  95. <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
  96. can be specified.</p>
  97. <table border="1" cellpadding="2" cellspacing="0">
  98. <tr>
  99. <td width="12%" valign="top"><b>Attribute</b></td>
  100. <td width="78%" valign="top"><b>Description</b></td>
  101. <td width="10%" valign="top"><b>Required</b></td>
  102. </tr>
  103. <tr>
  104. <td valign="top">type</td>
  105. <td valign="top">Use a predefined formatter (either "xml" or "plain").</td>
  106. <td align="center" rowspan="2">Exactly one of these.</td>
  107. </tr>
  108. <tr>
  109. <td valign="top">classname</td>
  110. <td valign="top">Name of a custom formatter class.</td>
  111. </tr>
  112. <tr>
  113. <td valign="top">extension</td>
  114. <td valign="top">Extension to append to the output filename.</td>
  115. <td align="center">Yes, if classname has been used.</td>
  116. </tr>
  117. <tr>
  118. <td valign="top">usefile</td>
  119. <td valign="top">Boolean that determines whether output should be
  120. sent to a file.</td>
  121. <td align="center">No, default true.</td>
  122. </tr>
  123. </table>
  124. <h4>test</h4>
  125. <p>Defines a single test class.</p>
  126. <table border="1" cellpadding="2" cellspacing="0">
  127. <tr>
  128. <td width="12%" valign="top"><b>Attribute</b></td>
  129. <td width="78%" valign="top"><b>Description</b></td>
  130. <td width="10%" valign="top"><b>Required</b></td>
  131. </tr>
  132. <tr>
  133. <td valign="top">name</td>
  134. <td valign="top">Name of the test class</td>
  135. <td align="center">Yes</td>
  136. </tr>
  137. <tr>
  138. <td valign="top">fork</td>
  139. <td valign="top">Run the tests in a separate VM.
  140. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  141. <td align="center" valign="top">No</td>
  142. </tr>
  143. <tr>
  144. <td valign="top">haltonerror</td>
  145. <td valign="top">Stop the build process if an error occurs during the test
  146. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  147. <td align="center" valign="top">No</td>
  148. </tr>
  149. <tr>
  150. <td valign="top">haltonfailure</td>
  151. <td valign="top">Stop the build process if a test fails (errors are
  152. considered failures as well). Overrides value set in
  153. <code>&lt;junit&gt;</code>.</td>
  154. <td align="center" valign="top">No</td>
  155. </tr>
  156. <tr>
  157. <td valign="top">outfile</td>
  158. <td valign="top">Base name of the test result. The full filename is
  159. determined by this attribute and the extension of
  160. <code>formatter</code>.</td>
  161. <td align="center" valign="top">No, default is
  162. <code>TEST-name</code> using the <code>name</code> attribute.</td>
  163. </tr>
  164. <tr>
  165. <td valign="top">if</td>
  166. <td valign="top">Only run test if the named property is set.</td>
  167. <td align="center" valign="top">No</td>
  168. </tr>
  169. <tr>
  170. <td valign="top">unless</td>
  171. <td valign="top">Only run test if the named property is <b>not</b> set.</td>
  172. <td align="center" valign="top">No</td>
  173. </tr>
  174. </table>
  175. <p>Tests can define their own formatters via nested
  176. <code>&lt;formatter&gt;</code> elements.</p>
  177. <h4>batchtest</h4>
  178. <p>Define a number of tests based on pattern matching.</p>
  179. <p><code>batchtest</code> collects the included files from any number
  180. of nested <a
  181. href="index.html#fileset"><code>&lt;fileset&gt;</code></a>s. It then
  182. generates a test class name for each file that ends in
  183. <code>.java</code> or <code>.class</code>.</p>
  184. <table border="1" cellpadding="2" cellspacing="0">
  185. <tr>
  186. <td width="12%" valign="top"><b>Attribute</b></td>
  187. <td width="78%" valign="top"><b>Description</b></td>
  188. <td width="10%" valign="top"><b>Required</b></td>
  189. </tr>
  190. <tr>
  191. <td valign="top">fork</td>
  192. <td valign="top">Run the tests in a separate VM.
  193. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  194. <td align="center" valign="top">No</td>
  195. </tr>
  196. <tr>
  197. <td valign="top">haltonerror</td>
  198. <td valign="top">Stop the build process if an error occurs during the test
  199. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  200. <td align="center" valign="top">No</td>
  201. </tr>
  202. <tr>
  203. <td valign="top">haltonfailure</td>
  204. <td valign="top">Stop the build process if a test fails (errors are
  205. considered failures as well). Overrides value set in
  206. <code>&lt;junit&gt;</code>.</td>
  207. <td align="center" valign="top">No</td>
  208. </tr>
  209. <tr>
  210. <td valign="top">if</td>
  211. <td valign="top">Only run tests if the named property is set.</td>
  212. <td align="center" valign="top">No</td>
  213. </tr>
  214. <tr>
  215. <td valign="top">unless</td>
  216. <td valign="top">Only run tests if the named property is <b>not</b> set.</td>
  217. <td align="center" valign="top">No</td>
  218. </tr>
  219. </table>
  220. <p>Batchtests can define their own formatters via nested
  221. <code>&lt;formatter&gt;</code> elements.</p>
  222. <h3>Examples</h3>
  223. <pre><blockquote>
  224. &lt;junit&gt;
  225. &lt;test name="my.test.TestCase" /&gt;
  226. &lt;/junit&gt;
  227. </pre></blockquote>
  228. <p>Runs the test defined in <code>my.test.TestCase</code> in the same
  229. VM. No output will be generated unless the test fails.</p>
  230. <pre><blockquote>
  231. &lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
  232. &lt;formatter type="plain" /&gt;
  233. &lt;test name="my.test.TestCase" /&gt;
  234. &lt;/junit&gt;
  235. </pre></blockquote>
  236. <p>Runs the test defined in <code>my.test.TestCase</code> in a
  237. separate VM. At the end of the test a single line summary will be
  238. printed. A detailed report of the test can be found in
  239. <code>TEST-my.test.TestCase.txt</code>. The build process will be
  240. stopped if the test fails.</p>
  241. <pre><blockquote>
  242. &lt;junit printsummary="yes" haltonfailure="yes"&gt;
  243. &lt;classpath&gt;
  244. &lt;pathelement location="${build.tests}" /&gt;
  245. &lt;pathelement path="${java.class.path}" /&gt;
  246. &lt;/classpath&gt;
  247. &lt;formatter type="plain" /&gt;
  248. &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result" &gt;
  249. &lt;formatter type="xml" /&gt;
  250. &lt;/test&gt;
  251. &lt;batchtest fork="yes"&gt;
  252. &lt;fileset dir="${src.tests}"&gt;
  253. &lt;include name="**/*Test*.java" /&gt;
  254. &lt;exclude name="**/AllTests.java" /&lt;
  255. &lt;/fileset&gt;
  256. &lt;/batchtest&gt;
  257. &lt;/junit&gt;
  258. </pre></blockquote>
  259. <p>Runs <code>my.test.TestCase</code> in the same VM (ignoring the
  260. given CLASSPATH), only a warning is printed if this test fails. In
  261. addition to the plain text test results, for this test a XML result
  262. will be output to <code>result.xml</code>.</p>
  263. <p>For each matching file in the directory <code>${src.tests}</code> a
  264. test is run in a separate VM. If a test fails, the build process is
  265. aborted. Results are collected in files named
  266. <code>TEST-<em>name</em>.txt</code>.</p>
  267. </body>
  268. </html>