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.8 KiB

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