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.

index.html 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html lang="en">
  3. <!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! -->
  4. <head>
  5. <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  6. <title>Apache Ant - AntUnit</title>
  7. <link type="text/css" href="../../page.css" rel="stylesheet">
  8. </head>
  9. <body>
  10. <p class="navpath">
  11. <script src="../../breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  12. </p>
  13. <div class="logobar">
  14. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  15. <tr>
  16. <td align="left"><img border="0" alt="Apache Ant site" src="../../images/group-logo.gif"></td>
  17. <td align="center" width="100%"><img alt="Apache Ant logo" border="0" src="../../images/project-logo.gif"></td>
  18. <td align="right">
  19. <form target="_blank" onsubmit="q.value = query.value + ' site:ant.apache.org'" action="http://www.google.com/search" method="get">
  20. <table summary="search" border="0" cellspacing="0" cellpadding="0" bgcolor="#4C6C8F">
  21. <tr>
  22. <td colspan="3"><img height="10" width="1" alt="" src="../../images/spacer.gif"></td>
  23. </tr>
  24. <tr>
  25. <td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
  26. <td nowrap="nowrap" class="searchcaption">
  27. <input name="q" type="hidden">
  28. <input size="15" id="query" type="text">
  29. <img height="1" width="5" alt="" src="../../images/spacer.gif">
  30. <input name="Search" value="Search" type="submit">
  31. <br>
  32. the Apache Ant site
  33. </td>
  34. <td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
  35. </tr>
  36. <tr>
  37. <td><img alt="" border="0" height="10" width="9" src="../../images/search-left.gif"></td>
  38. <td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
  39. <td><img alt="" border="0" height="10" width="9" src="../../images/search-right.gif"></td>
  40. </tr>
  41. </table>
  42. </form>
  43. </td>
  44. </tr>
  45. </table>
  46. </div>
  47. <div class="tab">
  48. <table summary="tab bar" border="0" cellpadding="0" cellspacing="0">
  49. <tr>
  50. <td width="5"><img alt="" height="8" width="8" src="../../images/spacer.gif"></td><td valign="bottom">
  51. <table summary="non selected tab" style="height: 1.4em" border="0" cellpadding="0" cellspacing="0">
  52. <tr>
  53. <td valign="top" width="5" bgcolor="#B2C4E0"><img height="5" width="5" alt="" src="../../images/tab-left.gif"></td><td valign="middle" bgcolor="#B2C4E0"><a href="../../index.html"><font size="2" face="Arial, Helvetica, Sans-serif">Home</font></a></td><td valign="top" width="5" bgcolor="#B2C4E0"><img height="5" width="5" alt="" src="../../images/tab-right.gif"></td>
  54. </tr>
  55. </table>
  56. </td>
  57. <td width="8"><img alt="" height="5" width="8" src="../../images/spacer.gif"></td><td valign="bottom">
  58. <table summary="selected tab" style="height: 1.5em" border="0" cellpadding="0" cellspacing="0">
  59. <tr>
  60. <td valign="top" width="5" bgcolor="#4C6C8F"><img height="5" width="5" alt="" src="../../images/tabSel-left.gif"></td><td valign="middle" bgcolor="#4C6C8F"><font color="#ffffff" size="2" face="Arial, Helvetica, Sans-serif"><b>Projects</b></font></td><td valign="top" width="5" bgcolor="#4C6C8F"><img height="5" width="5" alt="" src="../../images/tabSel-right.gif"></td>
  61. </tr>
  62. </table>
  63. </td>
  64. </tr>
  65. </table>
  66. </div>
  67. <div class="bluebar"></div>
  68. <div class="menucontainer">
  69. <div class="menu">
  70. <ul>
  71. <li class="menuheader">Projects
  72. <ul>
  73. <li>
  74. <a href="../../projects/index.html">Welcome</a>
  75. </li>
  76. </ul>
  77. </li>
  78. <li class="menuheader">Ant Libraries
  79. <ul>
  80. <li>
  81. <a href="../../antlibs/index.html">Introduction</a>
  82. </li>
  83. <li>
  84. <a href="../../antlibs/charter.html">Charter</a>
  85. </li>
  86. <li>
  87. <a href="../../antlibs/proper.html">Ant Libraries</a>
  88. </li>
  89. <li>
  90. <a href="../../antlibs/sandbox.html">Sandbox Ant Libraries</a>
  91. </li>
  92. <li>
  93. <a href="http://ant.apache.org/antlibs/bindownload.cgi">Binary Distributions</a>
  94. </li>
  95. <li>
  96. <a href="http://ant.apache.org/antlibs/srcdownload.cgi">Source Distributions</a>
  97. </li>
  98. </ul>
  99. </li>
  100. <li class="menuheader">Ivy
  101. <ul>
  102. <li>
  103. <a href="../../projects/ivy.html">Introduction</a>
  104. </li>
  105. <li>
  106. <a href="http://ant.apache.org/ivy/">Homepage</a>
  107. </li>
  108. <li>
  109. <a href="http://ant.apache.org/ivy/download.cgi">Distributions</a>
  110. </li>
  111. <li>
  112. <a href="http://wiki.apache.org/ivy/">Wiki</a>
  113. </li>
  114. </ul>
  115. </li>
  116. </ul>
  117. </div>
  118. <center>
  119. <!--#include virtual="/ads/buttonbar.html" -->
  120. </center>
  121. <img style="float: left" height="10" width="10" border="0" alt="" src="../../images/menu-left.gif">
  122. <img style="float: right" height="10" width="10" border="0" alt="" src="../../images/menu-right.gif">
  123. </div>
  124. <div class="lightbluebar">&nbsp;</div>
  125. <div class="main">
  126. <div class="content">
  127. <h1 class="title">AntUnit</h1>
  128. <h3 class="section">
  129. <a name="AntUnit 1.0"></a>
  130. AntUnit 1.0
  131. </h3>
  132. <h3>January 8, 2007 - Apache AntUnit 1.0 Available</h3>
  133. <p>Apache AntUnit 1.0 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
  134. or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
  135. release.</p>
  136. <h3 class="section">
  137. <a name="Idea"></a>
  138. Idea
  139. </h3>
  140. <p>Initially all tests for Ant tasks were written as individual
  141. <a href="http://www.junit.org/">JUnit</a> test cases. Pretty
  142. soon it was clear that most tests needed to perform common tasks
  143. like reading a build file, initializing a project instance with
  144. it and executing a target. At this point <a href="http://svn.apache.org/viewcvs.cgi/ant/core/trunk/src/testcases/org/apache/tools/ant/BuildFileTest.java">BuildFileTest</a>
  145. was invented, a base class for almost all task test cases.</p>
  146. <p>BuildFileTest works fine and in fact has been picked up by <a href="http://ant-contrib.sf.net/">the Ant-Contrib Project</a>
  147. and others as well.</p>
  148. <p>Over time a new pattern evolved, more and more tests only
  149. executed a target and didn't check any effects. Instead that
  150. target contained the assertions as a <code>&lt;fail&gt;</code>
  151. task. This is an example taken from the build file for the
  152. ANTLR task (using Ant 1.7 features):</p>
  153. <pre class="code">
  154. &lt;target name=&quot;test3&quot; depends=&quot;setup&quot;&gt;
  155. &lt;antlr target=&quot;antlr.g&quot; outputdirectory=&quot;${tmp.dir}&quot;/&gt;
  156. &lt;fail&gt;
  157. &lt;condition&gt;
  158. &lt;!-- to prove each of these files exists;
  159. ANTLR &gt;= 2.7.6 leaves behind new (.smap) files as well. --&gt;
  160. &lt;resourcecount when=&quot;ne&quot; count=&quot;5&quot;&gt;
  161. &lt;fileset dir=&quot;${tmp.dir}&quot;&gt;
  162. &lt;include name=&quot;CalcParserTokenTypes.txt&quot; /&gt;
  163. &lt;include name=&quot;CalcParserTokenTypes.java&quot; /&gt;
  164. &lt;include name=&quot;CalcLexer.java&quot; /&gt;
  165. &lt;include name=&quot;CalcParser.java&quot; /&gt;
  166. &lt;include name=&quot;CalcTreeWalker.java&quot; /&gt;
  167. &lt;/fileset&gt;
  168. &lt;/resourcecount&gt;
  169. &lt;/condition&gt;
  170. &lt;/fail&gt;
  171. &lt;/target&gt;
  172. </pre>
  173. <p>where the corresponding JUnit testcase has been reduced
  174. to</p>
  175. <pre class="code">
  176. ...
  177. public class ANTLRTest extends BuildFileTest {
  178. private final static String TASKDEFS_DIR = &quot;src/etc/testcases/taskdefs/optional/antlr/&quot;;
  179. public ANTLRTest(String name) {
  180. super(name);
  181. }
  182. public void setUp() {
  183. configureProject(TASKDEFS_DIR + &quot;antlr.xml&quot;);
  184. }
  185. public void tearDown() {
  186. executeTarget(&quot;cleanup&quot;);
  187. }
  188. public void test3() {
  189. executeTarget(&quot;test3&quot;);
  190. }
  191. ...
  192. }
  193. </pre>
  194. <p>This approach has a couple of advantages, one of them is that
  195. it is very easy to translate an example build file from a bug
  196. report into a test case. If you ask a user for a testcase for a
  197. given bug in Ant, he now doesn't need to understand JUnit or how
  198. to fit a test into Ant's existing tests any more.</p>
  199. <p>AntUnit takes this approach to testing even further, it
  200. removes JUnit completely and it comes with a set of predefined
  201. <code>&lt;assert&gt;</code> tasks in order to reuse common kind
  202. of checks.</p>
  203. <p>It turns out that AntUnit lends itself as a solution to other
  204. problems as well. The assertions are an easy way to validate a
  205. setup before even starting the build process, for example.
  206. AntUnit could also be used for functional and integration tests
  207. outside of the scope of Ant tasks (assert contents of databases
  208. after running an application, assert contents of HTTP responses
  209. ...). This is an area that will need more research.</p>
  210. <h3 class="section">
  211. <a name="Concepts"></a>
  212. Concepts
  213. </h3>
  214. <h4 class="subsection">
  215. <a name="antunit Task"></a>
  216. antunit Task
  217. </h4>
  218. <p>The &lt;antunit&gt; task drives the tests much like
  219. &lt;junit&gt; does for JUnit tests.</p>
  220. <p>When called on a build file, the task will start a new Ant
  221. project for that build file and scan for targets with names
  222. that start with "test". For each such target it then will</p>
  223. <ol>
  224. <li>Execute the target named setUp, if there is one.</li>
  225. <li>Execute the target itself - if this target depends on
  226. other targets the normal Ant rules apply and the dependent
  227. targets are executed first.</li>
  228. <li>Execute the target names tearDown, if there is one.</li>
  229. </ol>
  230. <h4 class="subsection">
  231. <a name="Assertions"></a>
  232. Assertions
  233. </h4>
  234. <p>The base task is <code>&lt;assertTrue&gt;</code>. It
  235. accepts a single nested condition and throws a subclass of
  236. BuildException named AssertionFailedException if that
  237. condition evaluates to false.</p>
  238. <p>This task could have been implemented using
  239. <code>&lt;macrodef&gt;</code> and <code>&lt;fail&gt;</code>,
  240. but in fact it is a "real" task so that it is possible to
  241. throw a subclass of BuildException. The
  242. <code>&lt;antunit&gt;</code> task catches this exception and
  243. marks the target as failed, any other type of Exception
  244. (including other BuildException) are test errors.</p>
  245. <p>Together with <code>&lt;assertTrue&gt;</code> there are
  246. many predefined assertions for common conditions, most of
  247. these are only macros.</p>
  248. <h4 class="subsection">
  249. <a name="Other Tasks"></a>
  250. Other Tasks
  251. </h4>
  252. <p>The <code>&lt;logcapturer&gt;</code> captures all messages
  253. that pass Ant's logging system and provides them via a
  254. reference inside of the project. If you want to assert
  255. certain log messages, you need to start this task (prior to
  256. your target under test) and use the
  257. <code>&lt;assertLogContains&gt;</code> assertion.</p>
  258. <p><code>&lt;expectFailure&gt;</code> is a task container that
  259. catches any BuildException thrown by tasks nested into it. If
  260. no exception has been thrown it will cause a test failure (by
  261. throwing an AssertionFailedException).</p>
  262. <h4 class="subsection">
  263. <a name="AntUnitListener"></a>
  264. AntUnitListener
  265. </h4>
  266. <p>Part of the library is the <code>AntUnitListener</code>
  267. interface that can be used to record test results. The
  268. &lt;antunit&gt; task accepts arbitrary many listeners and
  269. relays test results to them.</p>
  270. <p>Currently two implementations -
  271. <code>&lt;plainlistener&gt;</code> and <code>xmllistener</code>
  272. modelled after the "plain" and "xml"
  273. JUnit listeners - are bundled with the library.</p>
  274. <h3 class="section">
  275. <a name="Examples"></a>
  276. Examples
  277. </h3>
  278. <p>This is a way to test that <code>&lt;touch&gt;</code>
  279. actually creates a file if it doesn't exist:</p>
  280. <pre class="code">
  281. &lt;project xmlns:au=&quot;antlib:org.apache.ant.antunit&quot;&gt;
  282. &lt;!-- is called prior to the test --&gt;
  283. &lt;target name=&quot;setUp&quot;&gt;
  284. &lt;property name=&quot;foo&quot; value=&quot;foo&quot;/&gt;
  285. &lt;/target&gt;
  286. &lt;!-- is called after the test, even if that caused an error --&gt;
  287. &lt;target name=&quot;tearDown&quot;&gt;
  288. &lt;delete file=&quot;${foo}&quot; quiet=&quot;true&quot;/&gt;
  289. &lt;/target&gt;
  290. &lt;!-- the actual test case --&gt;
  291. &lt;target name=&quot;testTouchCreatesFile&quot;&gt;
  292. &lt;au:assertFileDoesntExist file=&quot;${foo}&quot;/&gt;
  293. &lt;touch file=&quot;${foo}&quot;/&gt;
  294. &lt;au:assertFileExists file=&quot;${foo}&quot;/&gt;
  295. &lt;/target&gt;
  296. &lt;/project&gt;
  297. </pre>
  298. <p>When running a task like</p>
  299. <pre class="code">
  300. &lt;au:antunit&gt;
  301. &lt;fileset dir=&quot;.&quot; includes=&quot;touch.xml&quot;/&gt;
  302. &lt;au:plainlistener/&gt;
  303. &lt;/au:antunit&gt;
  304. </pre>
  305. <p>from a buildfile of its own you'll get a result that looks like</p>
  306. <pre class="code">
  307. [au:antunit] Build File: /tmp/touch.xml
  308. [au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec
  309. [au:antunit] Target: testTouchCreatesFile took 0.183 sec
  310. BUILD SUCCESSFUL
  311. Total time: 1 second
  312. </pre>
  313. </div>
  314. </div>
  315. <p class="copyright">
  316. <script type="text/javascript" language="JavaScript"><!--
  317. document.write(" - "+"Last Published: " + document.lastModified);
  318. // -->
  319. </script>
  320. </p>
  321. </body>
  322. </html>