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

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