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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <html>
  2. <head>
  3. <title>JUnit Task</title>
  4. </head>
  5. <body>
  6. <h2><a name="junit">JUnit</a></h2>
  7. <h3>Description</h3>
  8. <p>This task runs tests from the JUnit testing framework. The latest
  9. version of the framework can be found at
  10. <a href="http://www.junit.org">http://www.junit.org</a>.
  11. This task has been tested with JUnit 3.0 up to JUnit 3.8.1; it won't
  12. work with versions prior to JUnit 3.0.</p>
  13. <p><strong>Note:</strong> This task depends on external libraries not included
  14. in the Ant distribution. See <a href="../install.html#librarydependencies">
  15. Library Dependencies</a> for more information.
  16. </p>
  17. <p>
  18. <strong>Note</strong>:
  19. You must have <code>junit.jar</code> and the class files for the
  20. <code>&lt;junit&gt;</code> task in the same classpath.
  21. You can do one of:
  22. <ol>
  23. <li>
  24. Put both <code>junit.jar</code> and the optional tasks jar file in
  25. <code>ANT_HOME/lib</code>.
  26. </li>
  27. <li>
  28. Do not put either in <code>ANT_HOME/lib</code>, and instead
  29. include their locations in your <code>CLASSPATH</code> environment variable.
  30. </li>
  31. <li>
  32. Do neither of the above, and instead, specify their locations using
  33. a <code>&lt;classpath&gt;</code> element in the build file.
  34. See <a href="../../faq.html#delegating-classloader" target="_top">the
  35. FAQ</a> for details.
  36. </ol>
  37. </p>
  38. <p>Tests are defined by nested <code>test</code> or
  39. <code>batchtest</code> tags (see <a href="#nested">nested
  40. elements</a>).</p>
  41. <h3>Parameters</h3>
  42. <table border="1" cellpadding="2" cellspacing="0">
  43. <tr>
  44. <td width="12%" valign="top"><b>Attribute</b></td>
  45. <td width="78%" valign="top"><b>Description</b></td>
  46. <td width="10%" valign="top"><b>Required</b></td>
  47. </tr>
  48. <tr>
  49. <td valign="top">printsummary</td>
  50. <td valign="top">Print one-line statistics for each testcase. Can
  51. take the values <code>on</code>,
  52. <code>off</code>, and
  53. <code>withOutAndErr</code>.
  54. <code>withOutAndErr</code> is the same
  55. as <code>on</code> but also includes the output of the test
  56. as written to <code>System.out</code> and <code>System.err</code>.</td>
  57. <td align="center" valign="top">No; default is <code>off</code>.</td>
  58. </tr>
  59. <tr>
  60. <td valign="top">fork</td>
  61. <td valign="top">Run the tests in a separate VM.</td>
  62. <td align="center" valign="top">No; default is <code>off</code>.</td>
  63. </tr>
  64. <tr>
  65. <td valign="top">haltonerror</td>
  66. <td valign="top">Stop the build process if an error occurs during the test
  67. run.</td>
  68. <td align="center" valign="top">No; default is <code>off</code>.</td>
  69. </tr>
  70. <tr>
  71. <td valign="top">errorproperty</td>
  72. <td valign="top">The name of a property to set in the event of an error.</td>
  73. <td align="center" valign="top">No</td>
  74. </tr>
  75. <tr>
  76. <td valign="top">haltonfailure</td>
  77. <td valign="top">Stop the build process if a test fails (errors are
  78. considered failures as well).</td>
  79. <td align="center" valign="top">No; default is <code>off</code>.</td>
  80. </tr>
  81. <tr>
  82. <td valign="top">failureproperty</td>
  83. <td valign="top">The name of a property to set in the event of a failure
  84. (errors are considered failures as well).</td>
  85. <td align="center" valign="top">No.</td>
  86. </tr>
  87. <tr>
  88. <td valign="top">filtertrace</td>
  89. <td valign="top">Filter out Junit and Ant stack frames from error and failure stack traces.</td>
  90. <td align="center" valign="top">No; default is <code>on</code>.</td>
  91. </tr>
  92. <tr>
  93. <td valign="top">timeout</td>
  94. <td valign="top">Cancel the individual tests if they don&apos;t finish
  95. in the given time (measured in milliseconds). Ignored if
  96. <code>fork</code> is disabled.</td>
  97. <td align="center" valign="top">No</td>
  98. </tr>
  99. <tr>
  100. <td valign="top">maxmemory</td>
  101. <td valign="top">Maximum amount of memory to allocate to the forked VM.
  102. Ignored if <code>fork</code> is disabled.</td>
  103. <td align="center" valign="top">No</td>
  104. </tr>
  105. <tr>
  106. <td valign="top">jvm</td>
  107. <td valign="top">The command used to invoke the Java Virtual Machine,
  108. default is 'java'. The command is resolved by
  109. <code>java.lang.Runtime.exec()</code>.
  110. Ignored if <code>fork</code> is disabled.</td>
  111. <td align="center" valign="top">No; default is <code>java</code>.</td>
  112. </tr>
  113. <tr>
  114. <td valign="top">dir</td>
  115. <td valign="top">The directory in which to invoke the VM. Ignored if
  116. <code>fork</code> is disabled.</td>
  117. <td align="center" valign="top">No</td>
  118. </tr>
  119. <tr>
  120. <td valign="top">newenvironment</td>
  121. <td valign="top">Do not propagate the old environment when new
  122. environment variables are specified. Ignored if <code>fork</code> is
  123. disabled.</td>
  124. <td align="center" valign="top">No; default is <code>false</code>.</td>
  125. </tr>
  126. <tr>
  127. <td valign="top">includeantruntime</td>
  128. <td valign="top">Implicitly add the Ant classes required to run
  129. the tests and JUnit to the classpath in forked mode.</td>
  130. <td align="center" valign="top">No; default is <code>true</code>.</td>
  131. </tr>
  132. <tr>
  133. <td valign="top">showoutput</td>
  134. <td valign="top">Send any output generated by tests to Ant's
  135. logging system as well as to the formatters. By default only the
  136. formatters receive the output.</td>
  137. <td align="center" valign="top">No</td>
  138. </tr>
  139. <tr>
  140. <td valign="top">tempdir</td>
  141. <td valign="top">Where Ant should place temporary files.
  142. <em>Since Ant 1.6</em>.</td>
  143. <td align="center" valign="top">No; default is the project's base
  144. directory.</td>
  145. </tr>
  146. <tr>
  147. <td valign="top">reloading</td>
  148. <td valign="top">Whether or not a new classloader should be instantiated for each test case.<br/>
  149. Ignore if <code>fork</code> is set to true.
  150. <em>Since Ant 1.6</em>.</td>
  151. <td align="center" valign="top">No; default is <code>true</code>.</td>
  152. </tr>
  153. </table>
  154. <p>By using the <code>errorproperty</code> and <code>failureproperty</code>
  155. attributes, it is possible to
  156. perform setup work (such as starting an external server), execute the test,
  157. clean up, and still fail the build in the event of a failure.</p>
  158. <p>The <code>filtertrace</code> attribute condenses error and failure
  159. stack traces before reporting them.
  160. It works with both the plain and XML formatters. It filters out any lines
  161. that begin with the following string patterns:<pre>
  162. "junit.framework.TestCase"
  163. "junit.framework.TestResult"
  164. "junit.framework.TestSuite"
  165. "junit.framework.Assert."
  166. "junit.swingui.TestRunner"
  167. "junit.awtui.TestRunner"
  168. "junit.textui.TestRunner"
  169. "java.lang.reflect.Method.invoke("
  170. "org.apache.tools.ant."</pre></p>
  171. <h3><a name="nested">Nested Elements</a></h3>
  172. <p>The <code>&lt;junit&gt;</code> task
  173. supports a nested <code>&lt;classpath&gt;</code>
  174. element that represents a <a href="../using.html#path">PATH like
  175. structure</a>.</p>
  176. <h4>jvmarg</h4>
  177. <p>If <code>fork</code> is enabled, additional parameters may be passed to
  178. the new VM via nested <code>&lt;jvmarg&gt;</code> elements. For example:</p>
  179. <pre>
  180. &lt;junit fork=&quot;yes&quot;&gt;
  181. &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  182. ...
  183. &lt;/junit&gt;
  184. </pre>
  185. <p>would run the test in a VM without JIT.</p>
  186. <p><code>&lt;jvmarg&gt;</code> allows all attributes described in <a
  187. href="../using.html#arg">Command-line Arguments</a>.</p>
  188. <h4>sysproperty</h4>
  189. <p>Use nested <code>&lt;sysproperty&gt;</code> elements to specify system
  190. properties required by the class. These properties will be made available
  191. to the VM during the execution of the test (either ANT's VM or the forked VM,
  192. if <code>fork</code> is enabled).
  193. The attributes for this element are the same as for <a href="../CoreTasks/exec.html#env">environment variables</a>.</p>
  194. <pre>
  195. &lt;junit fork=&quot;no&quot;&gt;
  196. &lt;sysproperty key=&quot;basedir&quot; value=&quot;${basedir}&quot;/&gt;
  197. ...
  198. &lt;/junit&gt;
  199. </pre>
  200. <p>would run the test in ANT's VM and make the <code>basedir</code> property
  201. available to the test.</p>
  202. <h4>syspropertyset</h4>
  203. <p>You can specify a set of properties to be used as system properties
  204. with <a href="../CoreTypes/propertyset.html">syspropertyset</a>s.</p>
  205. <p><em>since Ant 1.6</em>.</p>
  206. <h4>env</h4>
  207. <p>It is possible to specify environment variables to pass to the
  208. forked VM via nested <code>&lt;env&gt;</code> elements. For a description
  209. of the <code>&lt;env&gt;</code> element's attributes, see the
  210. description in the <a href="../CoreTasks/exec.html#env">exec</a> task.</p>
  211. <p>Settings will be ignored if <code>fork</code> is disabled.</p>
  212. <h4>bootclasspath</h4>
  213. <p>The location of bootstrap class files can be specified using this
  214. <a href="../using.html#path">PATH like structure</a> - will be ignored
  215. if <i>fork</i> is not <code>true</code> or the target VM doesn't
  216. support it (i.e. Java 1.1).</p>
  217. <p><em>since Ant 1.6</em>.</p>
  218. <h4>formatter</h4>
  219. <p>The results of the tests can be printed in different
  220. formats. Output will always be sent to a file, unless you set the
  221. <code>usefile</code> attribute to <code>false</code>.
  222. The name of the file is determined by the
  223. name of the test and can be set by the <code>outfile</code> attribute
  224. of <code>&lt;test&gt;</code>.</p>
  225. <p>There are three predefined formatters - one prints the test results
  226. in XML format, the other emits plain text. The formatter named
  227. <code>brief</code> will only print detailed information for testcases
  228. that failed, while <code>plain</code> gives a little statistics line
  229. for all test cases. Custom formatters that need to implement
  230. <code>org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter</code>
  231. can be specified.</p>
  232. <p>If you use the XML formatter, it may not include the same output
  233. that your tests have written as some characters are illegal in XML
  234. documents and will be dropped.</p>
  235. <table border="1" cellpadding="2" cellspacing="0">
  236. <tr>
  237. <td width="12%" valign="top"><b>Attribute</b></td>
  238. <td width="78%" valign="top"><b>Description</b></td>
  239. <td width="10%" valign="top"><b>Required</b></td>
  240. </tr>
  241. <tr>
  242. <td valign="top">type</td>
  243. <td valign="top">Use a predefined formatter (either
  244. <code>xml</code>, <code>plain</code>, or <code>brief</code>).</td>
  245. <td align="center" rowspan="2">Exactly one of these.</td>
  246. </tr>
  247. <tr>
  248. <td valign="top">classname</td>
  249. <td valign="top">Name of a custom formatter class.</td>
  250. </tr>
  251. <tr>
  252. <td valign="top">extension</td>
  253. <td valign="top">Extension to append to the output filename.</td>
  254. <td align="center">Yes, if <code>classname</code> has been used.</td>
  255. </tr>
  256. <tr>
  257. <td valign="top">usefile</td>
  258. <td valign="top">Boolean that determines whether output should be
  259. sent to a file.</td>
  260. <td align="center">No; default is <code>true</code>.</td>
  261. </tr>
  262. <tr>
  263. <td valign="top">if</td>
  264. <td valign="top">Only use formatter if the named property is set.</td>
  265. <td align="center">No; default is <code>true</code>.</td>
  266. </tr>
  267. <tr>
  268. <td valign="top">unless</td>
  269. <td valign="top">Only use formatter if the named property is <b>not</b> set.</td>
  270. <td align="center">No; default is <code>true</code>.</td>
  271. </tr>
  272. </table>
  273. <h4>test</h4>
  274. <p>Defines a single test class.</p>
  275. <table border="1" cellpadding="2" cellspacing="0">
  276. <tr>
  277. <td width="12%" valign="top"><b>Attribute</b></td>
  278. <td width="78%" valign="top"><b>Description</b></td>
  279. <td width="10%" valign="top"><b>Required</b></td>
  280. </tr>
  281. <tr>
  282. <td valign="top">name</td>
  283. <td valign="top">Name of the test class.</td>
  284. <td align="center">Yes</td>
  285. </tr>
  286. <tr>
  287. <td valign="top">fork</td>
  288. <td valign="top">Run the tests in a separate VM.
  289. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  290. <td align="center" valign="top">No</td>
  291. </tr>
  292. <tr>
  293. <td valign="top">haltonerror</td>
  294. <td valign="top">Stop the build process if an error occurs during the test
  295. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  296. <td align="center" valign="top">No</td>
  297. </tr>
  298. <tr>
  299. <td valign="top">errorproperty</td>
  300. <td valign="top">The name of a property to set in the event of an error.
  301. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  302. <td align="center" valign="top">No</td>
  303. </tr>
  304. <tr>
  305. <td valign="top">haltonfailure</td>
  306. <td valign="top">Stop the build process if a test fails (errors are
  307. considered failures as well). Overrides value set in
  308. <code>&lt;junit&gt;</code>.</td>
  309. <td align="center" valign="top">No</td>
  310. </tr>
  311. <tr>
  312. <td valign="top">failureproperty</td>
  313. <td valign="top">The name of a property to set in the event of a failure
  314. (errors are considered failures as well). Overrides value set in
  315. <code>&lt;junit&gt;</code>.</td>
  316. <td align="center" valign="top">No</td>
  317. </tr>
  318. <tr>
  319. <td valign="top">filtertrace</td>
  320. <td valign="top">Filter out Junit and Ant stack frames from error and failure stack
  321. traces. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  322. <td align="center" valign="top">No; default is <code>on</code>.</td>
  323. </tr>
  324. <tr>
  325. <td valign="top">todir</td>
  326. <td valign="top">Directory to write the reports to.</td>
  327. <td align="center" valign="top">No; default is the current directory.</td>
  328. </tr>
  329. <tr>
  330. <td valign="top">outfile</td>
  331. <td valign="top">Base name of the test result. The full filename is
  332. determined by this attribute and the extension of
  333. <code>formatter</code>.</td>
  334. <td align="center" valign="top">No; default is
  335. <code>TEST-</code><em>name</em>, where <em>name</em> is the name of
  336. the test specified in the <code>name</code> attribute.</td>
  337. </tr>
  338. <tr>
  339. <td valign="top">if</td>
  340. <td valign="top">Only run test if the named property is set.</td>
  341. <td align="center" valign="top">No</td>
  342. </tr>
  343. <tr>
  344. <td valign="top">unless</td>
  345. <td valign="top">Only run test if the named property is <b>not</b> set.</td>
  346. <td align="center" valign="top">No</td>
  347. </tr>
  348. </table>
  349. <p>Tests can define their own formatters via nested
  350. <code>&lt;formatter&gt;</code> elements.</p>
  351. <h4>batchtest</h4>
  352. <p>Define a number of tests based on pattern matching.</p>
  353. <p><code>batchtest</code> collects the included files from any number
  354. of nested <a
  355. href="../CoreTypes/fileset.html"><code>&lt;fileset&gt;</code></a>s. It then
  356. generates a test class name for each file that ends in
  357. <code>.java</code> or <code>.class</code>.</p>
  358. <table border="1" cellpadding="2" cellspacing="0">
  359. <tr>
  360. <td width="12%" valign="top"><b>Attribute</b></td>
  361. <td width="78%" valign="top"><b>Description</b></td>
  362. <td width="10%" valign="top"><b>Required</b></td>
  363. </tr>
  364. <tr>
  365. <td valign="top">fork</td>
  366. <td valign="top">Run the tests in a separate VM.
  367. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  368. <td align="center" valign="top">No</td>
  369. </tr>
  370. <tr>
  371. <td valign="top">haltonerror</td>
  372. <td valign="top">Stop the build process if an error occurs during the test
  373. run. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  374. <td align="center" valign="top">No</td>
  375. </tr>
  376. <tr>
  377. <td valign="top">errorproperty</td>
  378. <td valign="top">The name of a property to set in the event of an error.
  379. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  380. <td align="center" valign="top">No</td>
  381. </tr>
  382. <tr>
  383. <td valign="top">haltonfailure</td>
  384. <td valign="top">Stop the build process if a test fails (errors are
  385. considered failures as well). Overrides value set in
  386. <code>&lt;junit&gt;</code>.</td>
  387. <td align="center" valign="top">No</td>
  388. </tr>
  389. <tr>
  390. <td valign="top">failureproperty</td>
  391. <td valign="top">The name of a property to set in the event of a failure
  392. (errors are considered failures as well). Overrides value set in
  393. <code>&lt;junit&gt;</code></td>
  394. <td align="center" valign="top">No</td>
  395. </tr>
  396. <tr>
  397. <td valign="top">filtertrace</td>
  398. <td valign="top">Filter out Junit and Ant stack frames from error and failure stack
  399. traces. Overrides value set in <code>&lt;junit&gt;</code>.</td>
  400. <td align="center" valign="top">No; default is <code>on</code>.</td>
  401. </tr>
  402. <tr>
  403. <td valign="top">todir</td>
  404. <td valign="top">Directory to write the reports to.</td>
  405. <td align="center" valign="top">No; default is the current directory.</td>
  406. </tr>
  407. <tr>
  408. <td valign="top">if</td>
  409. <td valign="top">Only run tests if the named property is set.</td>
  410. <td align="center" valign="top">No</td>
  411. </tr>
  412. <tr>
  413. <td valign="top">unless</td>
  414. <td valign="top">Only run tests if the named property is <strong>not</strong> set.</td>
  415. <td align="center" valign="top">No</td>
  416. </tr>
  417. </table>
  418. <p>Batchtests can define their own formatters via nested
  419. <code>&lt;formatter&gt;</code> elements.</p>
  420. <h3>Examples</h3>
  421. <pre>
  422. &lt;junit&gt;
  423. &lt;test name="my.test.TestCase"/&gt;
  424. &lt;/junit&gt;
  425. </pre>
  426. <p>Runs the test defined in <code>my.test.TestCase</code> in the same
  427. VM. No output will be generated unless the test fails.</p>
  428. <pre>
  429. &lt;junit printsummary="yes" fork="yes" haltonfailure="yes"&gt;
  430. &lt;formatter type="plain"/&gt;
  431. &lt;test name="my.test.TestCase"/&gt;
  432. &lt;/junit&gt;
  433. </pre>
  434. <p>Runs the test defined in <code>my.test.TestCase</code> in a
  435. separate VM. At the end of the test, a one-line summary will be
  436. printed. A detailed report of the test can be found in
  437. <code>TEST-my.test.TestCase.txt</code>. The build process will be
  438. stopped if the test fails.</p>
  439. <pre>
  440. &lt;junit printsummary="yes" haltonfailure="yes"&gt;
  441. &lt;classpath&gt;
  442. &lt;pathelement location="${build.tests}"/&gt;
  443. &lt;pathelement path="${java.class.path}"/&gt;
  444. &lt;/classpath&gt;
  445. &lt;formatter type="plain"/&gt;
  446. &lt;test name="my.test.TestCase" haltonfailure="no" outfile="result"&gt;
  447. &lt;formatter type="xml"/&gt;
  448. &lt;/test&gt;
  449. &lt;batchtest fork="yes" todir="${reports.tests}"&gt;
  450. &lt;fileset dir="${src.tests}"&gt;
  451. &lt;include name="**/*Test*.java"/&gt;
  452. &lt;exclude name="**/AllTests.java"/&gt;
  453. &lt;/fileset&gt;
  454. &lt;/batchtest&gt;
  455. &lt;/junit&gt;
  456. </pre>
  457. <p>Runs <code>my.test.TestCase</code> in the same VM, ignoring the
  458. given CLASSPATH; only a warning is printed if this test fails. In
  459. addition to the plain text test results, for this test a XML result
  460. will be output to <code>result.xml</code>.
  461. Then, for each matching file in the directory defined for
  462. <code>${src.tests}</code> a
  463. test is run in a separate VM. If a test fails, the build process is
  464. aborted. Results are collected in files named
  465. <code>TEST-</code><em>name</em><code>.txt</code> and written to
  466. <code>${reports.tests}</code>.</p>
  467. <hr>
  468. <p align="center">Copyright &copy; 2000-2003 Apache Software Foundation. All rights
  469. Reserved.</p>
  470. </body>
  471. </html>