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

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