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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. </ul>
  101. </div>
  102. <img style="float: left" height="10" width="10" border="0" alt="" src="../../images/menu-left.gif">
  103. <img style="float: right" height="10" width="10" border="0" alt="" src="../../images/menu-right.gif">
  104. </div>
  105. <div class="lightbluebar">&nbsp;</div>
  106. <div class="main">
  107. <div class="content">
  108. <h1 class="title">AntUnit</h1>
  109. <h3 class="section">
  110. <a name="AntUnit 1.0Beta2"></a>
  111. AntUnit 1.0Beta2
  112. </h3>
  113. <h3>October 29, 2006 - Apache AntUnit 1.0Beta2 Available</h3>
  114. <p>Apache AntUnit 1.0Beta2 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
  115. or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
  116. release.</p>
  117. <h3 class="section">
  118. <a name="Idea"></a>
  119. Idea
  120. </h3>
  121. <p>Initially all tests for Ant tasks were written as individual
  122. <a href="http://www.junit.org/">JUnit</a> test cases. Pretty
  123. soon it was clear that most tests needed to perform common tasks
  124. like reading a build file, initializing a project instance with
  125. 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>
  126. was invented, a base class for almost all task test cases.</p>
  127. <p>BuildFileTest works fine and in fact has been picked up by <a href="http://ant-contrib.sf.net/">the Ant-Contrib Project</a>
  128. and others as well.</p>
  129. <p>Over time a new pattern evolved, more and more tests only
  130. executed a target and didn't check any effects. Instead that
  131. target contained the assertions as a <code>&lt;fail&gt;</code>
  132. task. This is an example taken from the build file for the
  133. ANTLR task (using Ant 1.7 features):</p>
  134. <pre class="code">
  135. &lt;target name=&quot;test3&quot; depends=&quot;setup&quot;&gt;
  136. &lt;antlr target=&quot;antlr.g&quot; outputdirectory=&quot;${tmp.dir}&quot;/&gt;
  137. &lt;fail&gt;
  138. &lt;condition&gt;
  139. &lt;!-- to prove each of these files exists;
  140. ANTLR &gt;= 2.7.6 leaves behind new (.smap) files as well. --&gt;
  141. &lt;resourcecount when=&quot;ne&quot; count=&quot;5&quot;&gt;
  142. &lt;fileset dir=&quot;${tmp.dir}&quot;&gt;
  143. &lt;include name=&quot;CalcParserTokenTypes.txt&quot; /&gt;
  144. &lt;include name=&quot;CalcParserTokenTypes.java&quot; /&gt;
  145. &lt;include name=&quot;CalcLexer.java&quot; /&gt;
  146. &lt;include name=&quot;CalcParser.java&quot; /&gt;
  147. &lt;include name=&quot;CalcTreeWalker.java&quot; /&gt;
  148. &lt;/fileset&gt;
  149. &lt;/resourcecount&gt;
  150. &lt;/condition&gt;
  151. &lt;/fail&gt;
  152. &lt;/target&gt;
  153. </pre>
  154. <p>where the corresponding JUnit testcase has been reduced
  155. to</p>
  156. <pre class="code">
  157. ...
  158. public class ANTLRTest extends BuildFileTest {
  159. private final static String TASKDEFS_DIR = &quot;src/etc/testcases/taskdefs/optional/antlr/&quot;;
  160. public ANTLRTest(String name) {
  161. super(name);
  162. }
  163. public void setUp() {
  164. configureProject(TASKDEFS_DIR + &quot;antlr.xml&quot;);
  165. }
  166. public void tearDown() {
  167. executeTarget(&quot;cleanup&quot;);
  168. }
  169. public void test3() {
  170. executeTarget(&quot;test3&quot;);
  171. }
  172. ...
  173. }
  174. </pre>
  175. <p>This approach has a couple of advantages, one of them is that
  176. it is very easy to translate an example build file from a bug
  177. report into a test case. If you ask a user for a testcase for a
  178. given bug in Ant, he now doesn't need to understand JUnit or how
  179. to fit a test into Ant's existing tests any more.</p>
  180. <p>AntUnit takes this approach to testing even further, it
  181. removes JUnit completely and it comes with a set of predefined
  182. <code>&lt;assert&gt;</code> tasks in order to reuse common kind
  183. of checks.</p>
  184. <p>It turns out that AntUnit lends itself as a solution to other
  185. problems as well. The assertions are an easy way to validate a
  186. setup before even starting the build process, for example.
  187. AntUnit could also be used for functional and integration tests
  188. outside of the scope of Ant tasks (assert contents of databases
  189. after running an application, assert contents of HTTP responses
  190. ...). This is an area that will need more research.</p>
  191. <h3 class="section">
  192. <a name="Concepts"></a>
  193. Concepts
  194. </h3>
  195. <h4 class="subsection">
  196. <a name="antunit Task"></a>
  197. antunit Task
  198. </h4>
  199. <p>The &lt;antunit&gt; task drives the tests much like
  200. &lt;junit&gt; does for JUnit tests.</p>
  201. <p>When called on a build file, the task will start a new Ant
  202. project for that build file and scan for targets with names
  203. that start with "test". For each such target it then will</p>
  204. <ol>
  205. <li>Execute the target named setUp, if there is one.</li>
  206. <li>Execute the target itself - if this target depends on
  207. other targets the normal Ant rules apply and the dependent
  208. targets are executed first.</li>
  209. <li>Execute the target names tearDown, if there is one.</li>
  210. </ol>
  211. <h4 class="subsection">
  212. <a name="Assertions"></a>
  213. Assertions
  214. </h4>
  215. <p>The base task is <code>&lt;assertTrue&gt;</code>. It
  216. accepts a single nested condition and throws a subclass of
  217. BuildException named AssertionFailedException if that
  218. condition evaluates to false.</p>
  219. <p>This task could have been implemented using
  220. <code>&lt;macrodef&gt;</code> and <code>&lt;fail&gt;</code>,
  221. but in fact it is a "real" task so that it is possible to
  222. throw a subclass of BuildException. The
  223. <code>&lt;antunit&gt;</code> task catches this exception and
  224. marks the target as failed, any other type of Exception
  225. (including other BuildException) are test errors.</p>
  226. <p>Together with <code>&lt;assertTrue&gt;</code> there are
  227. many predefined assertions for common conditions, most of
  228. these are only macros.</p>
  229. <h4 class="subsection">
  230. <a name="Other Tasks"></a>
  231. Other Tasks
  232. </h4>
  233. <p>The <code>&lt;logcapturer&gt;</code> captures all messages
  234. that pass Ant's logging system and provides them via a
  235. reference inside of the project. If you want to assert
  236. certain log messages, you need to start this task (prior to
  237. your target under test) and use the
  238. <code>&lt;assertLogContains&gt;</code> assertion.</p>
  239. <p><code>&lt;expectFailure&gt;</code> is a task container that
  240. catches any BuildException thrown by tasks nested into it. If
  241. no exception has been thrown it will cause a test failure (by
  242. throwing an AssertionFailedException).</p>
  243. <h4 class="subsection">
  244. <a name="AntUnitListener"></a>
  245. AntUnitListener
  246. </h4>
  247. <p>Part of the library is the <code>AntUnitListener</code>
  248. interface that can be used to record test results. The
  249. &lt;antunit&gt; task accepts arbitrary many listeners and
  250. relays test results to them.</p>
  251. <p>Currently two implementations -
  252. <code>&lt;plainlistener&gt;</code> and <code>xmllistener</code>
  253. modelled after the "plain" and "xml"
  254. JUnit listeners - are bundled with the library.</p>
  255. <h3 class="section">
  256. <a name="Examples"></a>
  257. Examples
  258. </h3>
  259. <p>This is a way to test that <code>&lt;touch&gt;</code>
  260. actually creates a file if it doesn't exist:</p>
  261. <pre class="code">
  262. &lt;project xmlns:au=&quot;antlib:org.apache.ant.antunit&quot;&gt;
  263. &lt;!-- is called prior to the test --&gt;
  264. &lt;target name=&quot;setUp&quot;&gt;
  265. &lt;property name=&quot;foo&quot; value=&quot;foo&quot;/&gt;
  266. &lt;/target&gt;
  267. &lt;!-- is called after the test, even if that caused an error --&gt;
  268. &lt;target name=&quot;tearDown&quot;&gt;
  269. &lt;delete file=&quot;${foo}&quot; quiet=&quot;true&quot;/&gt;
  270. &lt;/target&gt;
  271. &lt;!-- the actual test case --&gt;
  272. &lt;target name=&quot;testTouchCreatesFile&quot;&gt;
  273. &lt;au:assertFileDoesntExist file=&quot;${foo}&quot;/&gt;
  274. &lt;touch file=&quot;${foo}&quot;/&gt;
  275. &lt;au:assertFileExists file=&quot;${foo}&quot;/&gt;
  276. &lt;/target&gt;
  277. &lt;/project&gt;
  278. </pre>
  279. <p>When running a task like</p>
  280. <pre class="code">
  281. &lt;au:antunit&gt;
  282. &lt;fileset dir=&quot;.&quot; includes=&quot;touch.xml&quot;/&gt;
  283. &lt;au:plainlistener/&gt;
  284. &lt;/au:antunit&gt;
  285. </pre>
  286. <p>from a buildfile of its own you'll get a result that looks like</p>
  287. <pre class="code">
  288. [au:antunit] Build File: /tmp/touch.xml
  289. [au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec
  290. [au:antunit] Target: testTouchCreatesFile took 0.183 sec
  291. BUILD SUCCESSFUL
  292. Total time: 1 second
  293. </pre>
  294. </div>
  295. </div>
  296. <p class="copyright">
  297. <script type="text/javascript" language="JavaScript"><!--
  298. document.write(" - "+"Last Published: " + document.lastModified);
  299. // -->
  300. </script>
  301. </p>
  302. </body>
  303. </html>