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

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